MyBatis开发Dao层的两种方式(Mapper动态代理方式)

  MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发)

  接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式

  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的类型相同

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

<?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.xyfer.mapper.UserMapper">
    <!-- 根据id查询用户 -->
    <select id="getUserById" parameterType="int" resultType="com.xyfer.po.User">
        select * from user where id = #{id}
    </select>
    <!-- 添加用户 -->
    <insert id="insertUser" parameterType="com.xyfer.po.User">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
        select LAST_INSERT_ID() 
    </selectKey>
      insert into user(username,birthday,sex,address) 
      values(#{username},#{birthday},#{sex},#{address})
    </insert>
    <!-- 修改用户 -->
    <update id="updateUser" parameterType="com.xyfer.po.User">
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address}
        where id=#{id}
    </update>
    <!-- 删除用户 -->
    <delete id="deleteUserById" parameterType="int">
        delete from user where id=#{id}
    </delete>
    
</mapper>

  2、UserMapper.java接口文件

package com.xyfer.mapper;

import com.xyfer.po.User;

public interface UserMapper {
    
    public User getUserById(int id);   //根据id值查询一个用户
    public void insertUser(User user); //新增一个用户
    public void updateUser(User user); //修改一个用户
    public void deleteUser(int id);    //删除一个用户

}

  接口定义有如下特点:

    (1)UserMapper接口方法名和Mapper.xml中定义的statement的id相同

    (2)UserMapper接口方法的输入参数类型和mapper.xml中定义的statement的parameterType的类型相同

    (3) UserMapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同

  3、在SqlMapConfig.xml文件中加载UserMapper.xml文件

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

  4、测试

package com.xyfer.mapper;

import static org.junit.Assert.*;

import java.io.InputStream;
import java.util.Date;

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.xyfer.po.User;

import junit.framework.TestCase;

public class UserMapperTest extends TestCase{

    private SqlSessionFactory sqlSessionFactory;
    
    protected void setUp() throws Exception {
        SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
    }


    @Test
    public void testGetUserById() {
        //获取sqlsession
        SqlSession sqlsession = sqlSessionFactory.openSession();
        //获取UserMapper接口代理对象
        UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
        //调用代理对象方法
        User user = userMapper.getUserById(10);
        //打印结果
        System.out.println(user);
        //关闭sqlsession
        sqlsession.close();
    }

    @Test
    public void testInsertUser() {
        //获取sqlsession
        SqlSession sqlsession = sqlSessionFactory.openSession();
        //获取UserMapper接口代理对象
        UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
        //新建一个对象
        User user = new User();
        user.setUsername("小谢");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setAddress("浙江省杭州市");
        //调用代理对象方法
        userMapper.insertUser(user);
        //提交事务
        sqlsession.commit();
        //关闭sqlsession
        sqlsession.close();
    }

    @Test
    public void testUpdateUser() {
        //获取sqlsession
        SqlSession sqlsession = sqlSessionFactory.openSession();
        //获取UserMapper接口代理对象
        UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
        //新建一个对象
        User user = new User();
        user.setUsername("小谢");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setAddress("上海市");
        //调用代理对象方法
        userMapper.updateUser(user);
        //提交事务
        sqlsession.commit();
        //关闭sqlsession
        sqlsession.close();
    }

    @Test
    public void testDeleteUser() {
        //获取sqlsession
        SqlSession sqlsession = sqlSessionFactory.openSession();
        //获取UserMapper接口代理对象
        UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
        //调用代理对象方法
        userMapper.deleteUser(6);
        //提交事务
        sqlsession.commit();
        //关闭sqlsession
        sqlsession.close();
    }

}

  以上步骤,完成mybatis框架以Mapper动态代理方式开发Dao层,并对数据库进行增删改查操作。

  需要注意的是,上文提到的config文件夹和sqlmapper文件夹均为资源文件夹(source folder),默认会加载该路径下的文件。

posted @ 2018-12-13 23:43  侧身左睡  阅读(3823)  评论(0编辑  收藏  举报