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

上一篇,我们讲解了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{

	//通过构造方法,注入SQLSessionFactory
	private SqlSessionFactory sqlSessionFactory;
	public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
		this.sqlSessionFactory = sqlSessionFactory;
	}
	
	@Override
	public User findUserById(int id) throws Exception {
		//sqlSessionFactory创建sqlSession

		SqlSession sqlSession =sqlSessionFactory.openSession();
		User user =sqlSession.selectOne("test.findUserById",id);
		//释放资源
		sqlSession.close();
		return user;
	}	
}

写个单元测试:

public class UserDaoImplTest {
	
	private SqlSessionFactory sqlSessionFactory;
	
	@Before
	public void setUp() throws Exception{
		//创建sqlSessionFactory
		//加载Mybatis配置文件
		String resource = "SqlMapConfig.xml";
		// 得到配置文件流
		InputStream inputStream = Resources.getResourceAsStream(resource);
		//创建SqlSessionFactory,传入Mybatis配置信息
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);	
	}

	@Test
	public 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;
	
	@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 =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
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值