//增删改查
1 package TestDemo; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Date; 6 import java.util.List; 7 8 import org.apache.ibatis.io.Resources; 9 import org.apache.ibatis.session.SqlSession;10 import org.apache.ibatis.session.SqlSessionFactory;11 import org.apache.ibatis.session.SqlSessionFactoryBuilder;12 import org.junit.Test;13 14 import cn.jy.mybatis.pojo.User;15 16 public class MybatisMapperTest {17 //@Test18 //public void fun() throws IOException{19 // //1加载核心配置文件20 // String re="sqlMapConfig.xml";21 // InputStream resourceAsStream = Resources.getResourceAsStream(re);22 // //创建SqlSessionFactory23 // SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);24 // //创建回话25 // SqlSession openSession = build.openSession();26 // Object selectOne = openSession.selectOne("test.findUserById",10);27 // System.out.println(selectOne);28 //}29 30 31 // @Test32 // public void fun() throws IOException{33 // //1加载核心配置文件34 // String re="sqlMapConfig.xml";35 // InputStream resourceAsStream = Resources.getResourceAsStream(re);36 // //创建SqlSessionFactory37 // SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);38 // //创建回话39 // SqlSession openSession = build.openSession();40 // ListselectList = openSession.selectList("test.findUserByUserName","小明");41 // for(User a:selectList){42 // System.out.println(a);43 // }44 // 45 // }46 //插入一条数据47 // @Test48 // public void fun() throws IOException{49 // //1加载核心配置文件50 // String re="sqlMapConfig.xml";51 // InputStream resourceAsStream = Resources.getResourceAsStream(re);52 // //创建SqlSessionFactory53 // SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);54 // //创建回话55 // SqlSession openSession = build.openSession();56 // User u=new User();57 // u.setUsername("张无忌");58 // u.setBirthday(new Date());59 // u.setAddress("郑州杨金路");60 // u.setSex("男");61 // int insert = openSession.insert("test.insertUser",u);62 // openSession.commit();63 // System.out.println(insert);64 // }65 66 //更新数据67 // @Test68 // public void fun() throws IOException{69 // //1加载核心配置文件70 // String re="sqlMapConfig.xml";71 // InputStream resourceAsStream = Resources.getResourceAsStream(re);72 // //创建SqlSessionFactory73 // SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);74 // //创建回话75 // SqlSession openSession = build.openSession();76 // User u=new User();77 // u.setId(27);78 // u.setUsername("张无忌666");79 // u.setBirthday(new Date());80 // u.setAddress("郑州杨金路qy95");81 // u.setSex("男");82 // int update = openSession.update("test.updateUserById",u);83 // openSession.commit();84 // System.out.println(update);85 // }86 //删除数据87 @Test88 public void fun() throws IOException{89 //1加载核心配置文件90 String re="sqlMapConfig.xml";91 InputStream resourceAsStream = Resources.getResourceAsStream(re);92 //创建SqlSessionFactory93 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);94 //创建回话95 SqlSession openSession = build.openSession();96 openSession.delete("test.deleteUserById",27);97 openSession.commit();98 }99 }
//sqlMap文件下User.xml
1 2 56 9 12 13 insert into user(username,birthday,address,sex)values(#{username},#{birthday},#{address},#{sex})14 1516 update user17 set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id=#{id}18 1920 delete from user21 where id=#{id}22 23
//日志文件log4j.properties
1 # Global logging configuration2 log4j.rootLogger=DEBUG, stdout3 # Console output...4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
//主映射文件 加下面连接配置文件sqlMapConfig.xml
1 2 56 7 8 9 10 12 1311 14 2715 16 2617 18 19 2520 22 23 24 28 3029