本例在【Mybatis】MyBatis配置文件的使用(二)基础上继续学习对表执行CRUD操作
使用MyBatis对表执行CRUD操作
1、定义sql映射xml文件(EmployeeMapper.xml)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <!-- 4 namespace:名称空间 5 id:唯一标识 6 resultType:返回值类型 7 #{id}:从传过来的参数中取出id值 8 --> 9 <mapper namespace="com.hd.test.mapper.EmployeeMapper"> 10 <select id="getEmployeeById" 11 resultType="com.hd.test.pojo.Employee"> 12 select id, last_name lastName, gender, email from employee where id = 13 #{id} 14 </select> 15 16 <!-- public Long insertEmployee(Employee employee); --> 17 <!-- parameterType 可写可不写 --> 18 <insert id="insertEmployee" parameterType="com.hd.test.pojo.Employee" > 19 insert into employee(last_name, email, gender) values(#{lastName}, #{email}, #{gender}) 20 </insert> 21 22 <!-- public boolean updateEmployee(Employee employee); --> 23 <update id="updateEmployee"> 24 update employee 25 set last_name = #{lastName}, email = #{email}, gender = #{gender} 26 where 27 id = #{id} 28 </update> 29 30 <!-- public Integer deleteEmployeeById(Integer id); --> 31 <delete id="deleteEmployeeById"> 32 delete from employee where id = #{id} 33 </delete> 34 35 </mapper>
2、在mybatis-config.xml文件中注册这个映射文件EmployeeMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test_mybatis?allowPublicKeyRetrieval=true" /> <property name="username" value="admin" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <!-- 添加sql射文件到Mybatis的全局配置文件中 --> <mapper resource="mapper/EmployeeMapper.xml" /> </mappers> </configuration>
3、编写一个EmployeeMapper接口
1 package com.hd.test.mapper; 2 3 import com.hd.test.pojo.Employee; 4 5 public interface EmployeeMapper { 6 7 public Employee getEmployeeById(Integer id); 8 9 // 新增 10 public Long insertEmployee(Employee employee); 11 12 // 修改 13 public boolean updateEmployee(Employee employee); 14 15 // 删除 16 public Integer deleteEmployeeById(Integer id); 17 18 }
4、编写mybatis单元测试类(TestMybatis.java)
1 package com.hd.test.mybatis; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import javax.sound.midi.Soundbank; 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 com.hd.test.mapper.EmployeeMapper; 15 import com.hd.test.pojo.Employee; 16 17 import sun.print.resources.serviceui; 18 19 20 21 /** 22 * 1、SqlSession代表和数据库的一次会话,用完必须关闭 23 2、SqlSession和connection一样都是非线程安装的,每次使用都应该去获取新对象 24 3、mapper接口没有实现累,但是mybatis会为这个接口生成一个代理对象 25 (将接口和xml进行绑定) 26 EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class) 27 4、连个重要的配置文件: 28 mybatis的全局配置文件:包含数据库连接信息,事物管理等系统环境信息 29 sql映射文件:保存了每一个sql语句的映射信息: 30 将sql抽取出来。 31 * 32 */ 33 public class TestMybatis { 34 35 /** 36 * 测试增删改 37 * 1、mybatis允许增删改直接定义一下类型返回值 38 * Ingteger、Long、Boolean 39 * 2、需要手动提交数据 40 * SqlSession session = sqlSessionFactory.openSession(); ==> 手动提交 41 * SqlSession session = sqlSessionFactory.openSession(true); ==> 自动提交 42 * @throws IOException 43 */ 44 45 46 // 插入 47 @Test 48 public void test1() throws IOException { 49 50 // 获取SqlSessionFactory 51 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); 52 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 53 // 获取的sqlsession不会自动提交数据 54 SqlSession session = sqlSessionFactory.openSession(); 55 56 try { 57 EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); 58 59 // 1、插入数据 60 Employee employee = new Employee("小红", "1", "xiaohong@163.com"); 61 Long returnValue = mapper.insertEmployee(employee); 62 System.out.println("插入返回值:" + returnValue); 63 64 // 手动提交数据 65 session.commit(); 66 67 } finally { 68 session.close(); 69 } 70 } 71 72 // 修改 73 @Test 74 public void test2() throws IOException { 75 76 // 获取SqlSessionFactory 77 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); 78 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 79 80 // 获取的sqlsession 自动提交数据 81 SqlSession session = sqlSessionFactory.openSession(true); 82 try { 83 EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); 84 85 // 2、更新数据 86 Employee employee = new Employee(1, "小红", "0", "xiaohong@163.com"); 87 boolean returnValue = mapper.updateEmployee(employee); 88 System.out.println("更新返回值:" + returnValue); 89 90 } finally { 91 session.close(); 92 } 93 } 94 95 // 删除 96 @Test 97 public void test3() throws IOException { 98 99 // 获取SqlSessionFactory 100 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); 101 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 102 103 // 获取的sqlsession 自动提交数据 104 SqlSession session = sqlSessionFactory.openSession(true); 105 try { 106 EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); 107 108 // 3、删除数据 109 Integer returnValue = mapper.deleteEmployeeById(8); 110 System.out.println("删除返回值:" + returnValue); 111 112 } finally { 113 session.close(); 114 } 115 } 116 117 118 119 /** 120 * 查询 121 * @throws IOException 122 */ 123 @Test 124 public void test() throws IOException { 125 // 1、根据mybatis全局配置文件,获取SqlSessionFactory 126 String resource = "mybatis-config.xml"; 127 // 使用MyBatis提供的Resources类加载mybatis的配置文件,获取输入流 128 InputStream inputStream = Resources.getResourceAsStream(resource); 129 // 构建sqlSession的工厂 130 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 131 132 // 2、从SqlSession工厂中,获取sqlsession,用来执行sql 133 SqlSession session = sqlSessionFactory.openSession(); 134 try { 135 // 查询selectOne 136 // @param statement Unique identifier matching the statement to use. 一个唯一标识 137 // @param parameter A parameter object to pass to the statement. 参数 138 EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); 139 Employee employee = mapper.getEmployeeById(1); 140 // 输出信息 141 System.out.println("查询返回值:" + employee); 142 } finally { 143 // 关闭session 144 session.close(); 145 } 146 } 147 148 149 }
5、运行单元测试类,结果如下: