一、需要的包:mybatis,spring-jdbc,junit
二、需要添加的配置文件:主要是连接池的配置和映射文件的位置。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<!-- 设置连接池 --> <configuration> <environments default="environment"> <environment id="environment"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="system" /> <property name="password" value="921407164a" /> </dataSource> </environment> </environments> <!-- 指定映射文件的位置 --> <mappers> <mapper resource="entity/EmpMapper.xml" /> </mappers> </configuration>
三、写实体类
注意:实体类的属性名与表的字段名要一样(大小写可以忽略)
四、添加映射文件,写sql语句
映射文件格式:
<mapper namespace="dept"> <!-- id:要求唯一 parameterType:参数类型,这写实体类的完整名字 --> <insert id="insert" parameterType="entity.Dept"> insert into t_dept values(t_dept_seq.nextval,#{deptName},#{addr}) </insert> <select id="findAll" resultType="entity.Dept"> select * from t_dept </select> <select id="findByid" parameterType="int" resultType="entity.Dept"> select * from t_dept where id = #{id2} </select> <update id="update" parameterType="entity.Dept"> update t_dept SET deptName = #{deptName},addr = #{addr} WHERE id = #{id} </update> <delete id="delete" parameterType="int"> delete from t_dept where id = #{id1} </delete> </mapper>
<!-- 返回map类型的结果 --> <!-- map是java.util.Map的简写形式。 --> <select id="findById2" parameterType="int" resultType="map"> Select * From t_emp Where id = #{id1} </select> <!-- 使用resultMap解决表的字段名与实体类的属性名不一致的情况 --> <resultMap type="entity.emp" id="empResultMap"> <result property="empNo" column="id"/> <result property="ename" column="name"/> </resultMap> <select id="findByid3" parameterType="int" resultMap="empResultMap"> select * from t_emp where id = #{id} </select>
五、 调用SqlSession对象提供的方法来访问数据库
测试方法内容
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 List<Employee> list = 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 @Test 101 public void test7() { 102 String config = "SqlMapConfig.xml"; 103 SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); 104 SqlSessionFactory ssf = ssfb.build(TestCase 105 .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 @Test 121 public void test8() { 122 List<Dept> list = session.selectList("dept.findAll"); 123 System.out.println(list); 124 session.close(); 125 } 126 127 @Test 128 public void test9() { 129 Dept d = session.selectOne("dept.findByid",1); 130 System.out.println(d); 131 session.close(); 132 } 133 134 @Test 135 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 @Test 144 public void test11() { 145 session.delete("dept.delete", 2); 146 session.commit(); 147 session.close(); 148 } 149 150 @Test 151 //测试返回值是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 @Test 161 //测试 使用resultMap 162 public void test13() { 163 emp e = session.selectOne("test.findByid3",1); 164 System.out.println(e); 165 } 166 }