【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开发,方法体内存在大量模板的代码,重复率太高。
- 【Mybatis】(二)Mybatis开发Dao对比学习
- Mybatis学习记录(二)--Mybatis开发DAO方式
- Mybatis学习记录(二)--Mybatis开发DAO方式
- Mybatis学习记录(二)--Mybatis开发DAO方式
- MyBatis (二) Dao层开发
- MyBatis学习--mybatis开发dao的方法
- Mybatis框架学习(二)—DAO开发方式和Mapper代理的开发方式
- MyBatis(5)MyBatis DAO接口开发
- Mybatis学习(九)mybatis原始dao接口与dao实现的开发。
- mybatis学习笔记(4)-开发dao方法
- Mybatis学习笔记(五)【开发dao方法】
- Mybatis 开发dao层
- mybatis开发dao方法
- 原始DAO开发mybatis
- 使用Mybatis开发Dao
- mybatis开发dao方法
- 使用Mybatis开发Dao
- Mybatis(原始dao开发)
- 数据库连接池
- 统计IP个数
- 【Codeforces Round #398 (Div. 2)】Codeforces 767B The Queue
- Python 3 UnicodeEncodeError: 'gbk' codec can't encode错误的解决
- leetcode110~Balanced Binary Tree
- 【Mybatis】(二)Mybatis开发Dao对比学习
- 常见前端面试题及答案(下)
- Hibernate的HQL和Restrictions运算符
- Python中的GIL问题
- 行人检测论文笔记:Fast Feature Pyramids for Object Detection?
- 使用google注册教程
- 【机器学习实战-python3】决策树ID3
- 计算机的启动过程
- 行人检测论文笔记:Histograms of Oriented Gradients for Human Detection