mabatis--使用mapper代理开发dao
1、编写mapper.xml映射文件:
<?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"> <!-- 命名空间:其作用是对sql进行分类管理,使用mapper代理开发dao时,namespace需要设置为mapper接口的全类名 --> <mapper namespace="com.tt.mybatis.mapper.CustomerMapper"> <!-- 配置sql语句 通过select执行数据库查询: id标识映射文件中的sql parameterType为输入参数的类型; resultType为输出参数的Java对象类型 --> <select id="findCustomerById" parameterType="int" resultType="com.tt.po.Customer"> <!-- #{value}表示一个占位符,其中的value可以为任何名称 --> select * from customers where id=#{id} </select> <select id="findCustomerByName" parameterType="java.lang.String" resultType="com.tt.po.Customer"> <!-- ${value}表示字符串拼接,{}中的变量只能为value,存在sql注入风险,不建议使用 --> select * from customers where name like '%${value}%'; </select> <!-- 添加用户 --> <insert id="insertCustomer" parameterType="com.tt.po.Customer"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select LAST_INSERT_ID() </selectKey> insert into customers(name, email, address) values(#{name}, #{email}, #{address}) </insert> <!-- 删除用户 --> <delete id="deleteCustomer" parameterType="java.lang.Integer"> delete from customers where id=#{id} </delete> <!-- 更新用户 --> <update id="updateCustomer" parameterType="java.util.HashMap"> update customers set name=#{name}, email=#{email},address=#{address} where id=#{id} </update> </mapper>
--注意:需要在全局配置文件(SqlMapConfig.xml)中加载该映射文件。
2、编写 mapper 接口,需要遵循一些开发规范,mybatis可以自动生成mybatis接口实现类对象:
--1) 在mapper.xml中的 namespace 等于mapper接口地址(全类名);
--2) mapper.java 接口中的方法和mapper.xml中 statement 的id一致;
--3) mapper.java 接口中的方法的输入参数类型和mapper.xml中的 statement 的 parameterType 指定的类型一致;
--4) mapper.java 接口中的方法的返回值类型和 mapper.xml 中的 statement 的resultType 指定的类型一致;
package com.tt.mybatis.mapper; import java.util.List; import com.tt.po.Customer; public interface CustomerMapper { public Customer findCustomerById(int id) throws Exception; public List<Customer> findCustomerByName(String name) throws Exception; public void insertCustomer(Customer customer) throws Exception; public void deleteCustomer(int id) throws Exception; }
3、测试:
public class CustomerMapperTest { private SqlSessionFactory sqlSessionFactory = null; @Before public void setUp() throws Exception { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testFindCustomerById() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class); Customer customer = customerMapper.findCustomerById(4); sqlSession.close(); System.out.println(customer); } @Test public void testFindCustomerByName() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class); List<Customer> customers = customerMapper.findCustomerByName("TT"); sqlSession.close(); System.out.println(customers); } @Test public void testInsertCustomer() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class); Customer customer = new Customer("TestUser", "test@gmail.com", "China"); customerMapper.insertCustomer(customer); sqlSession.commit(); sqlSession.close(); System.out.println(customer.getId()); } @Test public void testDeleteCustomer() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class); customerMapper.deleteCustomer(9); sqlSession.commit(); sqlSession.close(); } }