【Mybatis】(二)Mybatis开发Dao对比学习

来源:互联网 发布:node js ide 编辑:程序博客网 时间:2024/06/16 02:35

上一篇,我们讲解了Mybatis简单的一条线,今天我们来讲解一下,在实际中我们是如何使用Mybatis开发Dao的。

(一)使用原始Dao开发方法

在上一篇的博客中,我们把所有的逻辑都写在了test方法里。现在我们有了Dao,看看我们要怎么开发吧。

我们首先要创建一个Dao接口和Dao实现类,还是拿User表来说吧

我们首先要创建一个UserDao接口:

public interface UserDao {public User findUserById(int id) throws Exception;}另外创建一个UserDaoImpl类public class UserDaoImpl implements UserDao{//通过构造方法,注入SQLSessionFactoryprivate SqlSessionFactory sqlSessionFactory;public UserDaoImpl(SqlSessionFactory sqlSessionFactory){this.sqlSessionFactory = sqlSessionFactory;}@Overridepublic User findUserById(int id) throws Exception {//sqlSessionFactory创建sqlSessionSqlSession sqlSession =sqlSessionFactory.openSession();User user =sqlSession.selectOne("test.findUserById",id);//释放资源sqlSession.close();return user;}}

写个单元测试:

public class UserDaoImplTest {private SqlSessionFactory sqlSessionFactory;@Beforepublic void setUp() throws Exception{//创建sqlSessionFactory//加载Mybatis配置文件String resource = "SqlMapConfig.xml";// 得到配置文件流InputStream inputStream = Resources.getResourceAsStream(resource);//创建SqlSessionFactory,传入Mybatis配置信息sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);}@Testpublic void testFindUserById() throws Exception {//创建UserDao的对象UserDao userDao = new UserDaoImpl(sqlSessionFactory);User user = userDao.findUserById(1);System.out.println(user);}}

多敲几条线,我们会知道,Dao方法开发重复的代码太多了,是不是有了“坏味道”?下面我们看一下使用mapper代理方法开发Dao。


(二)mapper代理方法

使用mapper代理方法开发,我们要创建一个mapper接口和一个mapper.xml配置文件。并且mapper接口和xml文件要遵守一定的规则:

1.mapper.xml中的namespace等于mapper接口的地址。

2.mapper.java接口中的方法名和mapper.xml中statement的id一致;

3.mapper.java接口中的方法输入参数类型和mapper.xml中statement的parameterType指定的类型一致。

4.mapper.java接口中的方法返回值类型和mapper.xml中statement的resultType指定的类型一致。

还拿User类做例子吧:

UserMapper.xml代码如下:

<mapper namespace="cn.itcast.mybatis.mapper.UserMapper"><select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User">        SELECT * FROM USER WHERE id=#{id}</select></mapper>

UserMapper接口代码如下:

public interface UserMapper {public User findUserById(int id) throws Exception;}

另外,我们不要忘了在SqlMapConfig.xml中加载mapper.xml文件

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

最后写个单元测试:

public class UserMapperTest {private SqlSessionFactory sqlSessionFactory;@Beforepublic void setUp() throws Exception{String resource = "SqlMapConfig.xml";// 得到配置文件流InputStream inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);}@Testpublic void testFindUserById() throws Exception {SqlSession sqlSession =sqlSessionFactory.openSession();//加载UserMapper接口UserMapper userMapper = sqlSession.getMapper(UserMapper.class);User user = userMapper.findUserById(1);System.out.println(user);}}

对比两种开发方式:

1.第一种Dao开发我们需要编写Dao接口和Dao实现类。mapper代理开发,我们需要编写mapper接口和mapper.xml

2.Dao开发需要向实现类注入SQLSessionFactory,然后SQLSessionFactory在方法体内创建SQLSession。mapper代理开发,只要mapper接口和xml文件遵守一定的规则就可以实现,sqlSession在调用Mapper之前创建

3.Dao开发,方法体内存在大量模板的代码,重复率太高。




0 0
原创粉丝点击