有勇气的牛排博客
☰

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

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

知止自律打卡-微信小程序APP
一款微信自律打卡小程序,支持自定义打卡项目、习惯记录、项目日记、专注计时与数据统计。自律打卡、学习打卡、读书打卡、健身打卡、运动打卡、减肥打卡、戒色打卡、养生打卡、戒烟、戒酒、戒熬夜等多种场景。万能打卡模板自由创建。可自定义每日打卡目标,记录习惯连续天数,可视化追踪成长进度。
了解产品详情 →
微信搜索「知止自律打卡」
知止自律打卡微信小程序

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 接口文件

接口定义有如下特点:

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

标签:

×
×