1. 程式人生 > >02.MyBatis在DAO層開發使用的Mapper動態代理方式

02.MyBatis在DAO層開發使用的Mapper動態代理方式

.get div 技術 before nco mes session list http

  在實際開發中,Mybatis作用於DAO層,那麽Service層該如何調用Mybatis

  Mybatis鼓勵使用Mapper動態代理的方式

  Mapper接口開發方法只需要程序員編寫Mapper接口(相當於Dao接口),由Mybatis框架根據接口定義創建接口的動態代理對象,代理對象的方法體等於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"
> <!-- namespace必須和Mapper接口類路徑一致 --> <mapper namespace="cn.mybatis.mapper.UserMapper"> <!-- 通過ID查詢一個結果 --> <select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User"> select * from user where id = #{id} </select> <!--
通過用戶名模糊查詢 --> <select id="findUserByUsername" parameterType="String" resultType="cn.itcast.mybatis.po.User"> select * from user where username like "%"#{username}"%" </select> <!-- 添加用戶 --> <insert id="insert" parameterType="cn.itcast.mybatis.po.User"
> insert into user(username,sex,birthday,address) values(#{username},#{sex},#{birthday},#{address}) </insert> </mapper>

2.編寫Mapper.java接口文件

public interface UserMapper {

    /**
     * 通過ID查詢一個結果
     * @param id
     * @return
     */
    public User findUserById(int id);
    
    /**
     * 通過用戶名模糊查詢
     * @param username
     * @return
     */
    public List<User> findUserByUsername(String username);
    
    /**
     * 添加用戶
     * @param user
     */
    public void insert(User user);
    
}

3.Mapper接口開發必須遵循的規範

  • 1.Mapper.xml文件中的【namespace】必須與Mapper.java接口【類路徑】相同
  • 2.Mapper.java接口中的方法名】必須與Mapper.xml中對應的id】相同
  • 3.Mapper.java接口中的入參】必須與Mapper.xml中對應的parameter】相同
  • 4.Mapper.java接口中的返回類型】必須與Mapper.xml中對應的resultType】相同

4.在SqlMapConfig.xml中加載Mapper.xml映射文件

技術分享

5.測試

public class UserServiceImpl {

    private SqlSessionFactory sqlSessionFactory;

    /**
     * 初始化工廠
     * 
     * @throws Exception
     */
    @Before
    public void init() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    /**
     * 通過ID查詢一個結果
     */
    @Test
    public void m01() {
        // 獲取sqlSession,和Spring整理後由Spring管理
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        // 從sqlSession中獲取Mapper接口的代理對象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 執行查詢
        User user = userMapper.findUserById(10);
        System.out.println(user);

        // 和Spring整理後由Spring管理
        sqlSession.close();
    }

    /**
     * 通過用戶名模糊查詢
     */
    @Test
    public void m02() {
        // 獲取sqlSession,和Spring整理後由Spring管理
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        // 從sqlSession中獲取Mapper接口的代理對象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 執行查詢
        List<User> list = userMapper.findUserByUsername("王五");
        for (User user : list) {
            System.out.println(user);
        }

        // 和Spring整理後由Spring管理
        sqlSession.close();
    }

    /**
     * 添加用戶
     */
    @Test
    public void m03() {
        // 獲取sqlSession,和Spring整理後由Spring管理
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        // 從sqlSession中獲取Mapper接口的代理對象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 執行查詢
        User user = new User();
        user.setUsername("添加User");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setAddress("未知區域");
        
        userMapper.insert(user);
        
        // 和Spring整理後由Spring管理
        sqlSession.commit();
        sqlSession.close();

    }
}

02.MyBatis在DAO層開發使用的Mapper動態代理方式