Mybatis学习笔记3 - 增删改查示例
1、接口定义
package com.mybatis.dao; import com.mybatis.bean.Employee; public interface EmployeeMapper { public Employee getEmpById(Integer id); public void addEmp(Employee employee); public void updateEmp(Employee employee); public void deleteEmpById(Integer id); }
2、mapper编写
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mybatis.dao.EmployeeMapper"> <!--public Employee getEmpById(Integer id);--> <select id="getEmpById" resultType="com.mybatis.bean.Employee"> select * from tbl_employee where id = #{id} </select> <!--public void addEmp(Employee employee);--> <!-- parameterType:参数类型,可以省略--> <insert id="addEmp" parameterType="com.mybatis.bean.Employee"> insert into tbl_employee(last_name, gender, email) values (#{lastName},#{gender},#{email}) </insert> <!--public void updateEmp(Employee employee);--> <update id="updateEmp"> update tbl_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id} </update> <!--public void deleteEmpById(Integer id);--> <delete id="deleteEmpById"> delete from tbl_employee where id=#{id} </delete> </mapper>
3、测试代码
package com.mybatis.demo; import java.io.IOException; import java.io.InputStream; import com.mybatis.bean.Employee; import com.mybatis.dao.EmployeeMapper; 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 org.junit.Test; public class MyTest { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } /** * 测试增删改 * 1、mybatis允许增删改直接定义以下类型返回值 * Integer、Long、Boolean、void * 2、需要手动提交数据 * sqlSessionFactory.openSession();===》手动提交 * sqlSessionFactory.openSession(true);===》自动提交 */ @Test public void testSelect() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Employee employee = mapper.getEmpById(1); System.out.println(employee); } finally { openSession.close(); } } @Test public void testInsert() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //此处获取到的SqlSession不会自动提交数据 SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); mapper.addEmp(new Employee("jetty", "jetty@126.com", 1)); //手动提交数据 openSession.commit(); } finally { openSession.close(); } } @Test public void testUpdate() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); mapper.updateEmp(new Employee(2, "jerry", "jerry@qq.com", 1)); openSession.commit(); } finally { openSession.close(); } } @Test public void testDelete() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //自动提交 SqlSession openSession = sqlSessionFactory.openSession(true); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); mapper.deleteEmpById(2); } finally { openSession.close(); } } }