【mybatis基础】mybatis开发dao两种方法

mybatis是一个支持普通SQL查询,存储过程和高级映射的优秀的持久层的框架,是apache下的顶级项目。mybatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。mybatis可以使用简单的XML或注解用于配置和原始映射,将接口和JavaPOJO映射成数据库中的记录。

其中,开发dao有两种方法,一种原始的dao开发方法,程序员需要写dao接口和dao实现类。另一种是mapper代理方法,程序员只需要写mapper接口相当于dao接口。

 

原始dao开发方法

1.编写dao接口(UserDao

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

2.编写dao实现类

public class UserDaoImpl implements UserDao {

	// 需要向dao实现类中注入SqlSessionFactory
	// 这里通过构造方法注入
	private SqlSessionFactory sqlSessionFactory;

	public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

	@Override
	public User findUserById(int id) throws Exception {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		//这里的test.findUserById是与下面的映射文件user.xml中的namespace+id有关。
		User user = sqlSession.selectOne("test.findUserById", id);

		// 释放资源
		sqlSession.close();

		return user;

	}
}

3.编写映射文件(user.xml

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

4.编写测试类

public class UserDaoImplTest {
	private SqlSessionFactory sqlSessionFactory;

	// 此方法是在执行testFindUserById之前执行
	@Before
	public void setUp() throws Exception {
		// 创建sqlSessionFactory

		// mybatis配置文件
		String resource = "SqlMapConfig.xml";
		// 得到配置文件流
		InputStream inputStream = Resources.getResourceAsStream(resource);

		// 创建会话工厂,传入mybatis的配置文件信息
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}

	@Test
	public void testUserDaoImpl() throws Exception{
		// 创建UserDao的对象
		UserDao userDao = new UserDaoImpl(sqlSessionFactory);

		// 调用UserDao的方法
		User user = userDao.findUserById(1);
				
		System.out.println(user);
	}
}
测试结果:



mapper代理方法

1.编写mapper.javaUserMapper.java相当于java接口)

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

2.编写mapper.xml(UserMapper.xml)

<mapper namespace="cn.itcast.mybatis.mapper.UserMapper">

	<!--通过id查询用户表的记录 -->
	<select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User">
		SELECT * FROM USER WHERE id=#{value}
	</select>

</mapper>

注意:

1)在mapper.xmlnamespace等于mapper接口地址

2mapper.java接口中的方法名和mapper.xmlstatementid一致

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

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

3.编写测试类

public class UserMapperTest {

	private SqlSessionFactory sqlSessionFactory;

	// 此方法是在执行testFindUserById之前执行
	@Before
	public void setUp() throws Exception {
		// 创建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=sqlSessionFactory.openSession();
		
		//创建UserMapper对象,mybatis自动生成mapper代理对象
		UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
		
		//调用UserMapper的方法
		User user=userMapper.findUserById(1);
		
		System.out.println(user);
		
		sqlSession.close();
	}

}
测试结果:



现在最常用的方法是第二种使用mapper代理的方法,不过第一种也有公司在用所以两种都要知道。


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值