一、需要的包:mybatis,spring-jdbc,junit
二、需要添加的配置文件:主要是连接池的配置和映射文件的位置。
三、写实体类
注意:实体类的属性名与表的字段名要一样(大小写可以忽略)
四、添加映射文件,写sql语句
映射文件格式:
insert into t_dept values(t_dept_seq.nextval,#{deptName},#{addr}) update t_dept SET deptName = #{deptName},addr = #{addr} WHERE id = #{id} delete from t_dept where id = #{id1}
五、 调用SqlSession对象提供的方法来访问数据库
测试方法内容
1 package test; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 import org.junit.Before; 10 import org.junit.Test; 11 12 import entity.Dept; 13 import entity.Employee; 14 import entity.emp; 15 import oracle.net.ns.SessionAtts; 16 17 public class TestCase { 18 SqlSession session; 19 @Before 20 public void init() { 21 String config = "SqlMapConfig.xml"; 22 SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); 23 SqlSessionFactory ssf = ssfb.build(TestCase.class.getClassLoader() 24 .getResourceAsStream(config)); 25 session = ssf.openSession(); 26 } 27 @Test 28 public void test1() { 29 String config = "SqlMapConfig.xml"; 30 //创建SqlSessionFactoryBuidler 31 SqlSessionFactoryBuilder ssfb = 32 new SqlSessionFactoryBuilder(); 33 34 //创建SqlSeeionFactory对象 35 SqlSessionFactory ssf = 36 ssfb.build(TestCase.class 37 .getClassLoader() 38 .getResourceAsStream(config)); 39 //获得SqlSession对象 40 SqlSession session = 41 ssf.openSession(); 42 //调用sqlsession对象提供的方法访问数据库 43 Employee emp = new Employee(); 44 emp.setName("lisi"); 45 emp.setAge(33); 46 session.insert("test.save", emp); 47 //添加修改删除需要提交事务 48 session.commit(); 49 //关闭SqlSession 50 session.close(); 51 } 52 53 @Test 54 public void test2() { 55 String config = "SqlMapConfig.xml"; 56 SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); 57 SqlSessionFactory ssf = ssfb.build(TestCase 58 .class.getClassLoader() 59 .getResourceAsStream(config)); 60 SqlSession session = ssf.openSession(); 61 Employee emp = new Employee(); 62 emp.setAge(21); 63 emp.setName("王五"); 64 session.update("test.insert", emp); 65 session.commit(); 66 session.close(); 67 } 68 69 @Test 70 public void test3() { 71 Listlist = session.selectList("test.findAll"); 72 System.out.println(list); 73 //session归还连接 74 session.close(); 75 } 76 77 @Test 78 public void test4() { 79 Employee e = session.selectOne("test.findByid", 1); 80 System.out.println(e); 81 session.close(); 82 } 83 84 @Test 85 public void test5() { 86 Employee e = session.selectOne("test.findByid", 1); 87 e.setAge(e.getAge()*2); 88 session.update("test.modify", e); 89 session.commit(); 90 session.close(); 91 } 92 93 @Test 94 public void test6() { 95 session.delete("test.delete",21); 96 session.commit(); 97 session.close(); 98 } 99 100 @Test101 public void test7() {102 String config = "SqlMapConfig.xml";103 SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();104 SqlSessionFactory ssf = ssfb.build(TestCase105 .class.getClassLoader()106 .getResourceAsStream(config));107 SqlSession session = ssf.openSession();108 Dept d1 = new Dept();109 d1.setDeptName("研发");110 d1.setAddr("北京");111 Dept d2 = new Dept();112 d2.setDeptName("销售");113 d2.setAddr("北京");114 session.insert("dept.insert", d1);115 session.insert("dept.insert", d2);116 session.commit();117 session.close();118 }119 120 @Test121 public void test8() {122 List list = session.selectList("dept.findAll");123 System.out.println(list);124 session.close();125 }126 127 @Test128 public void test9() {129 Dept d = session.selectOne("dept.findByid",1);130 System.out.println(d);131 session.close();132 }133 134 @Test135 public void test10() {136 Dept d = session.selectOne("dept.findByid",1);137 d.setAddr("上海");138 session.update("dept.update", d);139 session.commit();140 session.close();141 }142 143 @Test144 public void test11() {145 session.delete("dept.delete", 2);146 session.commit();147 session.close();148 }149 150 @Test151 //测试返回值是map类型的结果152 public void test12 () {153 Map date = 154 session.selectOne("test.findById2",1);155 //注意orcale数据库会将字段名统一变成大写格式156 System.out.println(date.get("NAME"));157 session.close();158 }159 160 @Test161 //测试 使用resultMap162 public void test13() {163 emp e = session.selectOne("test.findByid3",1);164 System.out.println(e);165 }166 }