Mybatis(二)--Mapper代理开发

    xiaoxiao2022-07-14  169

    一、Mapper代理

    传统开发方式跟数据连接使用dao接口,dao接口的实现类存在大量重复语句,且存在硬编码,如Mybatis(一)中的类似语句

    sqlSession.delete("cn.cf.domain.User.deleteUserById", 3);

    传入的变量因为sqlSession使用泛型,无法判断是否正确。因此对程序的开发会有些不利。

    mapper代理

    因此使用mapper代理改进。以下列子将使用mapper代理

    二、mapper代理实例

    2.1 环境搭建

    2.2 实体类

    package cn.cf.domain; /** * Product的实体类 * * @author cf * */ public class Product { private int p_id; private String p_name; private Double p_price; private String p_category; public int getP_id() { return p_id; } public void setP_id(int p_id) { this.p_id = p_id; } public String getP_name() { return p_name; } public void setP_name(String p_name) { this.p_name = p_name; } public Double getP_price() { return p_price; } public void setP_price(Double p_price) { this.p_price = p_price; } public String getP_category() { return p_category; } public void setP_category(String p_category) { this.p_category = p_category; } @Override public String toString() { return "Product [p_id=" + p_id + ", p_name=" + p_name + ", p_price=" + p_price + ", p_category=" + p_category + "]"; } }

    2.3 mapper包的接口和配置

    ProductMapper.java

    package cn.cf.domain; /** * Product的实体类 * * @author cf * */ public class Product { private int p_id; private String p_name; private Double p_price; private String p_category; public int getP_id() { return p_id; } public void setP_id(int p_id) { this.p_id = p_id; } public String getP_name() { return p_name; } public void setP_name(String p_name) { this.p_name = p_name; } public Double getP_price() { return p_price; } public void setP_price(Double p_price) { this.p_price = p_price; } public String getP_category() { return p_category; } public void setP_category(String p_category) { this.p_category = p_category; } @Override public String toString() { return "Product [p_id=" + p_id + ", p_name=" + p_name + ", p_price=" + p_price + ", p_category=" + p_category + "]"; } }

    productMapper.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"> <mapper namespace="cn.cf.mapper.ProductMapper"> <!-- 动态SQL : 片段(提取共同的sql语句)--> <sql id="selector"> select p_id, p_name, p_price, p_category from products </sql> <!-- //根据id查询 public Product findById(int id); --> <select id="findById" parameterType="int" resultType="Product"> <include refid="selector" /> where p_id = #{id} </select> <!-- findAllProduct --> <select id="findAllProduct" resultType="Product"> <include refid="selector"/> </select> <!--//更新产品信息 public Product updatePById(int id);--> <update id="updateById" parameterType="Product"> update products set p_name = #{p_name} , p_price = #{p_price}, p_category = #{p_category} where p_id = #{p_id} </update> </mapper>

    2.4 sqlMapConfig.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> <!-- 配置别名 --> <typeAliases> <package name="cn.cf.domain"/> </typeAliases> <!-- 环境配置 --> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3305/mybatisdemo" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!-- 引入mapper文件的位置 --> <mappers> <mapper resource="cn/cf/mapper/ProductMapper.xml"/> </mappers> </configuration>

    2.5 测试

    TestDemo.java

    package cn.cf.test; import java.io.InputStream; import java.util.List; 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.Before; import org.junit.Test; import cn.cf.domain.Product; import cn.cf.mapper.ProductMapper; public class TestDemo { private SqlSessionFactory sqlSessionFactory; @Before public void Before() throws Exception { String resource = "sqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } //测试根据id查找 @Test public void testfindById() { SqlSession sqlSession = sqlSessionFactory.openSession(); ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class); Product product = productMapper.findById(1); System.out.println(product); } //测试查找全部 @Test public void testfindAllProduct() { SqlSession sqlSession = sqlSessionFactory.openSession(); ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class); List<Product> list = productMapper.findAllProduct(); for (Product product : list) { System.out.println(product); } } //测试根据id修改 @Test public void testupdateById() { SqlSession sqlSession = sqlSessionFactory.openSession(); ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class); Product product = productMapper.findById(1); System.out.println(product); product.setP_name("红心火龙果"); product.setP_price(30.5); System.out.println(product); productMapper.updateById(product); //修改提交事务 sqlSession.commit(); } }

    以上都测试ok,不贴结果了。对数据库进行修改,记得要有提交事务的操作。

    三、总结

    使用mapper代理,代理对象是通过sqlSession的getMapper方法获取的。在上例中,dao层接口名相当于改为了ProductMapper,并有配置文件UserMapper.xml添加数据库操作语句。

    因为相当于使用接口中的方法执行操作,传入参数和返回值的参数也有了设置。

    最新回复(0)