MyBatis - Mapper动态代理开发

时间:2021-11-14 06:41:47

采用Mapper动态代理方法只需要编写相应的Mapper接口(相当于Dao接口),那么Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同Dao接口实现类方法。

 - Mapper接口开发需要遵循以下规范:

① Mapper.xml文件中的namespace与mapper接口的全类名相同。

② Mapper接口方法名和Mapper.xml中定义的statement的id相同。

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

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

 - 注意:

mapper动态代理开发中,根据返回值类型自动选择selectOne与selectList

Mapper动态代理示例演示

Mapper接口开发方法编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象。

① Mapper接口文件

- map接口中的方法名必须和配置文件中的id属性值是一致的。

package com.sikiedu.mapper;

import java.util.List;

import com.sikiedu.beans.User;
import com.sikiedu.beans.UserVo;

public interface UserMapper {

    // 根据ID查询用户
    public User selectUserById(Integer id);

    // 根据用户名模糊查询用户
    public List<User> selectUserLikeUsername(String username);

    // 同过包装类UserVo查询用户
    public User selectUserByUserVoId(UserVo vo);

    // 查询用户总条数
    public Integer selectUserCount();

}

② 定义mapper映射文件

- namespace必须对应着map接口的全类名。

<?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.sikiedu.mapper.UserMapper">
<!-- 注意这里的 namespace必须对应着map接口的全类名-->
<!-- 查询用户 --> <select id="selectUserById" parameterType="Integer" resultType="com.sikiedu.beans.User"> SELECT * FROM user WHERE id = #{id} </select> <select id="selectUserLikeUsername" parameterType="String" resultType="User"> <!-- SELECT * FROM coke.user WHERE username LIKE ‘%${value}%‘ --> SELECT * FROM user WHERE username LIKE "%"#{username}"%" </select> <!-- 同过包装类UserVo查询用户 --> <select id="selectUserByUserVoId" parameterType="UserVo" resultType="user"> SELECT * FROM user WHERE id = #{id} </select> <!-- selectUserCount --> <select id="selectUserCount" resultType="Integer"> SELECT COUNT(*) FROM user </select> </mapper>

③ 在SqlMapConfig.xml中加入映射文件

<!--配置映射器的位置 -->
<mappers>
    <!-- 单个配置 -->
    <!-- <mapper resource="com/sikiedu/mapper/UserMapper.xml" /> -->
    <!-- <mapper url="file:\D:JavaWorkSpaceJavaEE_SSM_Testssm_mybatissrccomsikiedumapperUserMapper.xml" /> -->
    <!-- <mapper class="com.sikiedu.mapper.UserMapper" /> -->
    <!-- 多配置-以包的形式;推荐使用 -->
    <package name="com.sikiedu.mapper" />
</mappers>

④ 测试代码

package com.sikiedu.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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 com.sikiedu.beans.Role;
import com.sikiedu.beans.RoleVo;
import com.sikiedu.beans.User;
import com.sikiedu.beans.UserVo;
import com.sikiedu.mapper.RoleMapper;
import com.sikiedu.mapper.UserMapper;

public class MapperTest {

    private SqlSessionFactory ssf;

    @Before
    public void getSqlSessionFactory() throws Exception {

        // 读取配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);

        // 需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();

        // 创建sqlSessionFactory
        ssf = ssfb.build(in);
    }

    @Test // 根据ID查询用户
    public void Test_selectUserById() throws IOException {

        // 生产一个sqlSession
        SqlSession sqlSession = ssf.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.selectUserById(3);
        System.out.println(user);

    }

    @Test // 根据用户名模糊查询用户
    public void Test_selectUserLikeUsername() throws IOException {

        // 生产一个sqlSession
        SqlSession sqlSession = ssf.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.selectUserLikeUsername("");
        for (User user : users) {
            System.out.println(user);
        }

    }

    @Test // 同过包装类UserVo查询用户
    public void Test_selectUserByUserVoId() throws IOException {

        // 生产一个sqlSession
        SqlSession sqlSession = ssf.openSession();
        // 操作数据库
        UserVo vo = new UserVo();
        // vo.setId(44);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectUserByUserVoId(vo);
        System.out.println(user);
    }

    @Test // 查询用户总条数
    public void Test_selectUserCount() throws IOException {

        // 生产一个sqlSession
        SqlSession sqlSession = ssf.openSession();
        // 操作数据库
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Integer userCount = mapper.selectUserCount();
        System.out.println(userCount);
    }

}