【MyBatis】基础篇(七) MyBatis基于传统DAO层开发

本文介绍使用MyBatis进行传统DAO层开发的方法,包括编写Dao接口、实现类及映射配置,通过实例演示了增删改查等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MyBatis·基础篇(七) MyBatis基于传统DAO层开发


使用 Mybatis 开发 Dao,通常有两个方法,即原始 Dao 开发方式和 Mapper 接口代理开发方式。而现在主流的开发方式是接口代理开发方式,这种方式总体上更加简便。我们的课程讲解也主要以接口代理开发方式为主。在之前已经给大家介绍了基于代理方式的 dao 开发,现在给大家介绍一下基于传统编写 Dao 实现类的开发方式。

1.编写持久层 Dao 接口

/**
 * @author: LzCc
 * @blog: https://blog.youkuaiyun.com/qq_41744145
 * @description: 接口
 */
public interface IUserDao {
/**
* 查询所有用户
* @return
*/
List<User> findAll();
/**
* 根据 id 查询
* @param userId
* @return
*/
User findById(Integer userId);
/**
* 保存用户
* @param user
* @return 影响数据库记录的行数
*/
int saveUser(User user);
/**
* 更新用户
* @param user
* * @return 影响数据库记录的行数
*/
int updateUser(User user);
/**
* 根据 id 删除用户
* @param userId
* @return
*/
int deleteUser(Integer userId);
/**
* 查询总记录条数
* @return
*/
int findTotal();
}

2. 编写持久层 Dao 实现类

/**
 * @author: LzCc
 * @blog: https://blog.youkuaiyun.com/qq_41744145
 * @description: UserDaoImpl
 */
public class UserDaoImpl implements IUserDao {
	private SqlSessionFactory factory;
	public UserDaoImpl(SqlSessionFactory factory) {
		this.factory = factory;
	}
	@Override
	public List<User> findAll() {
		SqlSession session = factory.openSession();
		List<User> users = session.selectList("top.lzchao.dao.IUserDao.findAll");
		session.close();
		return users;
	}
	@Override
	public User findById(Integer userId) {
		SqlSession session = factory.openSession();
		User user = session.selectOne("top.lzchao.dao.IUserDao.findById",userId);
		session.close();
		return user;
	}
	@Override
	public int saveUser(User user) {
		SqlSession session = factory.openSession();
		int res = session.insert("top.lzchao.dao.IUserDao.saveUser",user);
		session.commit();
		session.close();
		return res;
	}
	@Override
	public int updateUser(User user) {
		SqlSession session = factory.openSession();
		int res = session.update("top.lzchao.dao.IUserDao.updateUser",user);
		session.commit();
		session.close();
		return res;
	}
	@Override
	public int deleteUser(Integer userId) {
		SqlSession session = factory.openSession();
		int res = session.delete("top.lzchao.dao.IUserDao.deleteUser",userId);
		session.commit();
		session.close();
		return res;
	}
	@Override
	public int findTotal() {
		SqlSession session = factory.openSession();
		int res = session.selectOne("top.lzchao.dao.IUserDao.findTotal");
		session.close();
		return res;
	}
}

3.编写持久层映射配置

<?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="top.lzchao.dao.IUserDao">
	<!-- 配置查询所有操作 -->
	<select id="findAll" resultType="top.lzchao.domain.User">
		select * from user
	</select>
	<!-- 根据 id 查询 -->
	<select id="findById" resultType="top.lzchao.domain.User" parameterType="int">
		select * from user where id = #{uid}
	</select>
	<!-- 保存用户
	ognl 表达式:它是 apache 提供的一种表达式语言,在 struts2 中也有应用。
	Object Graphic Navigation Language 对象图导航语言
	它是按照一定的语法格式来获取数据的。
	语法格式就是使用 #{对象.对象}的方式
	#{user.username}它会先去找 user 对象,然后在 user 对象中找到 username 属性,并把值取
	出来
	-->
	<insert id="saveUser" parameterType="top.lzchao.domain.User">
		<!-- 配置保存时获取插入的 id -->
		<selectKey keyColumn="id" keyProperty="id" resultType="int">
		select last_insert_id();
		</selectKey>
		insert into user(username,birthday,sex,address)
		values(#{username},#{birthday},#{sex},#{address})
	</insert>
	<!-- 更新用户 -->
	<update id="updateUser" parameterType="top.lzchao.domain.User">
		update user 
		set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} 
		where id=#{id}
	</update>
	<!-- 删除用户 -->
	<delete id="deleteUser" parameterType="java.lang.Integer">
	delete from user where id = #{uid}
	</delete>
	<!-- 查询总记录条数 -->
	<select id="findTotal" resultType="int">
	select count(*) from user;
	</select>
</mapper>

~~ 编写测试类

/**
 * @author: LzCc
 * @blog: https://blog.youkuaiyun.com/qq_41744145
 * @description: MyBatis测试类
 */
public class MybastisCRUDTest {
	private InputStream in ;
	private SqlSessionFactory factory;
	private IUserDao userDao;
	@Test
	public void testFindAll() {
		List<User> users = userDao.findAll();
		for(User user : users) {
		System.out.println(user);
		}
	}
	@Test
	public void testFindOne() {
		//6.执行操作
		User user = userDao.findById(56);
		System.out.println(user);
	}
	@Test
	public void testSaveUser() throws Exception {
		User user = new User();
		user.setUsername("mybatis dao user");
		//6.执行操作
		int res = userDao.saveUser(user);
		System.out.println(res);
		System.out.println(user.getId());
	}
	@Test
	public void testUpdateUser()throws Exception{
	//1.根据 id 查询
		User user = userDao.findById(41);
		//2.更新操作
		user.setAddress("洛杉矶");
		int res = userDao.updateUser(user);
		System.out.println(res);
	}
	@Test
	public void testDeleteUser() throws Exception {
		//6.执行操作
		int res = userDao.deleteUser(56);
		System.out.println(res);
	}
	@Test
	public void testFindTotal() throws Exception {
		//6.执行操作
		int res = userDao.findTotal();
		System.out.println(res);
	}
	@Before//在测试方法执行之前执行
	public void init()throws Exception {
		//1.读取配置文件
		in = Resources.getResourceAsStream("SqlMapConfig.xml");
		//2.创建构建者对象
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		//3.创建 SqlSession 工厂对象
		factory = builder.build(in);
		//4.创建 Dao 接口的实现类
		userDao = new UserDaoImpl(factory);
	}
	@After//在测试方法执行完成之后执行
	public void destroy() throws Exception{
		//7.释放资源
		in.close();
	}
}

本篇文章只是带大家了解MyBatis对如何进行开发,不做重点学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值