mybatis开发dao的方法

作用范围

SqlSessionFactoryBuilder是以工具类方式来使用,需要创建sqlSessionFactory就new一个SqlSessionFactoryBuilder。

正常开发时,以单例方式管理sqlSessionFactory,整个系统运行过程中sqlSessionFactory只有一个实例,将来和spring整合后由spring以单例方式管理sqlSessionFactory。

由于sqlSession是线程不安全,所以sqlSession最佳应用范围在方法体内,在方法体内定义局部变量使用sqlSession。

(一)原始开发DAO方式

程序员需要写dao接口和dao 的实现类

MyBatis学习(二)-LMLPHP

DAO接口

public interface UserDao {
    //根据id查询用户信息
    public User findUserById(int id) throws Exception;

}

Dao接口实现

public class UserDaoImpl implements UserDao {
    //注入sqlSessionFactory
    private SqlSessionFactory sqlSessionFactory;
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public User findUserById(int id) throws Exception {
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //根据id查询用户信息
        User user = sqlSession.selectOne("test.findUserById", id);
        sqlSession.close();

        return user;
    }

}

Junit Test Case

public class UserDaoImplTest {
    // 会话工厂
    private SqlSessionFactory sqlSessionFactory;
    //创建工厂
    @Before
    public void setUp() throws Exception {
        //配置文件
        String resource = "SqlMapConfig.xml";
        //加载配置文件到输入流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //创建回话工厂
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void testFindUserById() throws Exception {
        UserDao userDao = new UserDaoImpl(sqlSessionFactory);
        User user = userDao.findUserById(33);
        System.out.println(user);
    }

}

原始dao开发方式的问题

dao的实现类中存在重复代码,整个mybatis操作的过程代码模板重复(先创建sqlsession、调用sqlsession的方法、关闭sqlsession)

dao的实现类中存在硬编码,调用sqlsession方法时将statement的id硬编码。

(二)mapper代理的方式

程序员只需要写dao接口,dao接口实现对象由mybatis自动生成代理对象。

mapper开发规范

要想让mybatis自动创建dao接口实现类的代理对象,必须遵循一些规则:

1、mapper.xml中namespace指定为mapper接口的全限定名。此步骤目的:通过mapper.xml和mapper.java进行关联。

2、mapper.xml中statement的id就是mapper.java中方法名

3、mapper.xml中statement的parameterType和mapper.java中方法输入参数类型一致

4、mapper.xml中statement的resultType和mapper.java中方法返回值类型一致.

MyBatis学习(二)-LMLPHP

UserMapper.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">
<mapper namespace="com.cxz.mybatis.mapper.UserMapper">
    <select id="findUserById" parameterType="int" resultType="com.cxz.mybatis.pojo.User" >
        SELECT * FROM USER WHERE id= #{id}
    </select>
</mapper>

UserMapper.java

package com.cxz.mybatis.mapper;

import com.cxz.mybatis.pojo.User;

public interface UserMapper {
    //根据id查询用户信息
    public User findUserById(int id) throws Exception;

}

将UserMapper.xml在SqlMapConfig.xml中加载

<mappers>
        <package name="com.cxz.mybatis.mapper"/>
    </mappers>

测试程序

public class UserMapperTest {
    //会话工厂
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void setUp() throws Exception {
        //配置文件
        String resource = "SqlMapConfig.xml";
        //加载配置文件到输入流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //创建会话工厂
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void testFindUserById() throws Exception {
        //创建sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.findUserById(33);
        System.out.println(user);
    }

}
05-11 12:50
查看更多