有勇气的牛排博客

javaWeb MyBatis(三) Mapper动态代理方式

有勇气的牛排 1103 Java 2021-10-22 09:36:50

1 实现原理

Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由MyBatis框架根据解耦定义创建的动态代理对象,代理对象的方法同上边Dao解耦实现类方法。

Mapper接口开发需要遵循的规范:

1 、Mapper.xml文件中的namespace与mapper接口的类路径相同。

2、Mapper接口方法名和Mapper.xml中定义的每个statement的id相同

3、Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同

4、Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同

Mapper.xml映射文件

定义mapper映射文件UserMapper.xml(内容同Users.xml),需要修改namespace的值为 UserMapper接口路径。将UserMapper.xml放在classpath 下mapper目录 下。

idea:直接在resource创建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="net920vip.mapper.UserMapper"> <!-- 根据id查询user --> <select id="findUserById" parameterType="int" resultType="net920vip.bean.UserBean"> select * from user where id = #{id} </select> <!-- 根据用户名模糊查询 --> <select id="findUserByUsername" parameterType="java.lang.String" resultType="net920vip.bean.UserBean"> select * from user where username like '%${value}%' </select> <!-- 新增用户 --> <insert id="insertUser" parameterType="net920vip.bean.UserBean"> insert into user(username,sex,birthday,address) values(#{username},#{sex},#{birthday},#{address}) </insert> <!-- 删除用户 --> <delete id="deleteUserById" parameterType="int"> delete from user where id = #{id} </delete> <!-- 修改用户 --> <update id="updateUserById" parameterType="net920vip.bean.UserBean"> update user set username=#{username}, sex=#{sex}, birthday=#{birthday}, address=#{address} where id = #{id} </update> </mapper>

3 UserMapper.java 接口文件

接口定义有如下特点:

  • Mapper解耦方法和Mapper.xml中定义的statement的id相同

  • Mapper接口方法的输入参数类型和mapper.xml中定义的statement的parameter的类型相同

  • Mapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同

package net920vip.mapper; import net920vip.bean.UserBean; import java.util.List; /** * 接口文件 对应UserMapper.xml文件 * 用户管理mapper * */ public interface UserMapper { // 根据id查询user public UserBean findUserById(int id) throws Exception; // 根据用户名模糊查询 public List<UserBean> findUserByUsername(String username)throws Exception; }

4 接在UserMapper.xml文件

<mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers>

5 测试

// 配置文件 String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); // 使用SqlSessionFactoryBuilder从xml配置文件中创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 创建数据库会话实例sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); // ---------------Mappper接口开发测试------------- // 使用动态代理获得UserMapper接口的代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 查询 //UserBean user = userMapper.findUserById(10); //System.out.println(user); // 新增 //UserBean user1 = new UserBean(20,"李白", "2",new Date(), "666"); //userMapper.insertUser(user1); //sqlSession.commit(); //sqlSession.close(); // 修改 UserBean user1 = new UserBean(26,"李白", "2",new Date(), "666"); userMapper.updateUserById(user1); sqlSession.commit(); sqlSession.close(); // 删除 // userMapper.deleteUserById(1);

输出:

UserBean{id=10, username='Charles', sex='1', birthday=Thu Oct 21 00:00:00 GMT+08:00 2021, address='中国'}

gitee地址:

https://gitee.com/net920vip/mybatis-demo01.git

留言

专栏
文章
加入群聊