package com.mybatis.test;
import com.mybatis.interfaces.IUserOperation;
import com.mybatis.model.User;
import com.mybatis.model.Article;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.util.List;
public class Test {
private static SqlSessionFactory sqlFactory;
private static Reader reader;
private static User user;
private static int Ret;
/*static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一般用来初始化静态变量和调用静态方*/
static {
try {
reader = Resources.getResourceAsReader(“Configuration.xml“);
sqlFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSession() {
return sqlFactory;
}
//获取用户列表
public static void GetUserList(List UserList)
{
if(UserList.size()>0){
for(User u:UserList){
System.out.println(“Id:“+u.getId()+“;UserName:“+u.getUserName()+“;UserAge:“+u.getUserAge()+
“;UserAddress:“+u.getUserAddress());
}
}
else{
System.out.println(“用户不存在!“);
}
}
//获取用户文章列表
public static void GetUserArticle(List articleList)
{
if(articleList.size()>0){
for(Article a:articleList){
System.out.println(“UserName:“+a.getUser().getUserName()+“;UserAddress:“+a.getUser().getUserAddress()+
“;Articletitle:“+a.gettitle()+“;ArticleContent:“+a.getContent());
}
}
else{
System.out.println(“用户不存在!“);
}
}
public static void main(String[] args) {
SqlSession session = sqlFactory.openSession();
try {
IUserOperation operation=session.getMapper(IUserOperation.class);
//新增
/*user=new User();
user.setUserName(“Amyli“);
user.setUserAge(“20“);
user.setUserAddress(“Ludun BaLin“);
Ret=operation.insertUserInfo(user);
if(Ret>0){
System.out.println(“新增成功!“);
}
else{
System.out.println(“新增失败!“);
}*/
//删除
/*Ret=operation.deleteUserInfo(27);
if(Ret>0){
System.out.println(“删除成功!“);
}
else{
System.out.println(“删除失败!“);
}*/
//修改
user = operation.selectUserByID(24);
user.setUserAge(“23“);
user.setUserAddress(“Chendu SiChuan“);
Ret=operation.updateUserInfo(user);
if(Ret>0){
System.out.println(“修改成功!“);
}
else{
System.out.println(“修改失败!“);
}
session.commit(); //记住,做增删改操作,一定要记得调用commit提交,否则无效。
//查询
user = operation.selectUserByID(24);
System.out.println(user.getId());
System.out.println(user.getUserName());
System.out.println(user.getUserAge());
System.out.println(user.getUserAddress());
System.out.println(““);
Test.GetUserList(operation.getUserList(“DaLei“));
System.out.println(“文章:“);
Test.GetUserArticle(operation.getUserAticles(24));
} finally {
session.close();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 990 2017-05-04 16:29 MybBatisStudy\.classpath
文件 1046 2017-05-04 16:29 MybBatisStudy\.project
文件 503 2017-05-04 16:29 MybBatisStudy\.settings\.jsdtscope
文件 364 2017-05-04 16:29 MybBatisStudy\.settings\org.eclipse.jdt.core.prefs
文件 577 2017-05-04 16:29 MybBatisStudy\.settings\org.eclipse.wst.common.component
文件 345 2017-05-04 16:29 MybBatisStudy\.settings\org.eclipse.wst.common.project.facet.core.xm
l
文件 49 2017-05-04 16:29 MybBatisStudy\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2017-05-04 16:29 MybBatisStudy\.settings\org.eclipse.wst.jsdt.ui.superType.name
文件 551 2017-05-04 16:30 MybBatisStudy\build\classes\com\mybatis\interfaces\IUserOperation.class
文件 1187 2017-05-04 16:30 MybBatisStudy\build\classes\com\mybatis\model\Article.class
文件 1122 2017-05-04 16:30 MybBatisStudy\build\classes\com\mybatis\model\User.class
文件 3268 2017-05-04 16:29 MybBatisStudy\build\classes\com\mybatis\model\User.xml
文件 4214 2017-05-04 16:30 MybBatisStudy\build\classes\com\mybatis\test\Test.class
文件 928 2017-05-04 16:29 MybBatisStudy\build\classes\Configuration.xml
文件 3132 2017-05-04 16:29 MybBatisStudy\Tester_Src\com\mybatis\test\Test.java
文件 436 2017-05-04 16:29 MybBatisStudy\User_Src\com\mybatis\interfaces\IUserOperation.java
文件 591 2017-05-04 16:29 MybBatisStudy\User_Src\com\mybatis\model\Article.java
文件 667 2017-05-04 16:29 MybBatisStudy\User_Src\com\mybatis\model\User.java
文件 3268 2017-05-04 16:29 MybBatisStudy\User_Src\com\mybatis\model\User.xml
文件 928 2017-05-04 16:29 MybBatisStudy\User_Src\Configuration.xml
文件 39 2017-05-04 16:29 MybBatisStudy\WebContent\meta-INF\MANIFEST.MF
文件 712734 2017-05-04 16:29 MybBatisStudy\WebContent\WEB-INF\lib\mybatis-3.2.7.jar
文件 832960 2017-05-04 16:29 MybBatisStudy\WebContent\WEB-INF\lib\mysql-connector-java-5.1.22-bin.jar
文件 718 2017-05-04 16:29 MybBatisStudy\WebContent\WEB-INF\web.xml
目录 0 2017-05-04 16:30 MybBatisStudy\build\classes\com\mybatis\interfaces
目录 0 2017-05-04 16:30 MybBatisStudy\build\classes\com\mybatis\model
目录 0 2017-05-04 16:30 MybBatisStudy\build\classes\com\mybatis\test
目录 0 2017-05-04 16:30 MybBatisStudy\build\classes\com\mybatis
目录 0 2017-05-04 16:29 MybBatisStudy\Tester_Src\com\mybatis\test
目录 0 2017-05-04 16:29 MybBatisStudy\User_Src\com\mybatis\interfaces
............此处省略19个文件信息
评论
共有 条评论