mybatis开发dao的动态代理

Mapper动态代理方式

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

  2、Mapper.xml(映射文件)

  映射文件与原始Dao开发的映射文件相似,只需要将namespace定于为mapper接口全路径。

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

   

   

   

   

   

   

       

            select LAST_INSERT_ID()

       

        insert into user(username,birthday,sex,address) value (#{username},#{birthday},#{sex},#{address});

   

   

   

        delete from user where id=#{id}

   

   

   

        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address}

        where id=#{id}

   

3、Mapper.java(接口文件)

public interface UserMapper {

    //根据ID查询用户信息

    public User findUserById(int id) throws Exception;

    //添加用户信息

    public void insertUser(User user) throws Exception;

    //删除用户信息

    public void deleteUser(int id) throws Exception;

    //更新用户信息

    public void updateUser(User user) throws Exception;

    //根据用户名模糊查找

    public List findUserByName(String user) throws Exception;

}

接口定义有如下特点:

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

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

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

  4、加载UserMapper.xml文件

  在SqlMapConfig.xml文件中加载UserMapper.xml,如下:

1

2

3

        5、测试代码:

public class MyBatis_mapper_test {

    private SqlSessionFactory sqlSessionFactory;

    @Before

    public void init() throws IOException{

        //创建sqlSessionFactory

        //MyBatis配置文件

        String resource = "SqlMapConfig.xml";

        //得到配置文件流

        InputStream inputStream = Resources.getResourceAsStream(resource);

        //创建会话工厂,传入MyBatis的配置信息

        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    }

    @Test

    public void testFindUserById() throws Exception{

        //获取sqlSession对象

        SqlSession sqlSession = sqlSessionFactory.openSession();

        //创建UserMapper对象,MyBatis自动生成mapper代理

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

        //调用userMapper的方法

        User user = userMapper.findUserById(10);

        //关闭资源

        sqlSession.close();

        //打印客户信息

        System.out.println(user);

    }

}

 5、Mapper动态代理总结:

(1)、动态代理对象调用sqlSession.selectOne()和sqlSession.selectList()是根据mapper接口方法的返回值决定,如果返回list则调用selectList方法,如果返回单个对象则调用selectOne方法。

  (2)、使用mapper代理方法时,输入参数可以使用pojo包装对象或map对象,保证dao的通用性。在系统中,dao层的代码是被业务层公用的。即使mapper接口只有一个参数,可以使用包装类型的pojo满足不同的业务方法的需求。


注意:持久层方法的参数可以包装类型、map等,service方法中建议不要使用包装类型(不利于业务层的可扩展)。

  mybatis开发dao的方法有两种:原始Dao开发和Mapper动态代理开发,这两种各有优点。原始Dao开发:程序员要写Dao和Dao实现,需要些较多的代码,但是比较好理解。Mapper动态代理:程序员只需要写Mapper接口,然后按照规范进行配置,MyBatis就会自动实现类似Dao实现,减少模板方法。mybatis官方推荐使用mapper代理方法开发mapper接口,程序员不用编写mapper接口实现类,使用mapper代理方法时,输入参数可以使用pojo包装对象或map对象,保证dao的通用性。

你可能感兴趣的:(mybatis开发dao的动态代理)