Mybatis入门笔记(1)——基于原始dao实现CRUD
基于原始dao实现CRUD
需要程序员编写Dao接口和Dao实现类,即就是UserDao接口和UserDaoImpl实现类
原始Dao开发中存在以下问题:
- Dao方法体存在重复代码:通过SqlSessionFactory创建SqlSession,调用SqlSession的数据库操作方法
- 调用sqlSession的数据库操作方法需要指定statement的id,这里存在硬编码,不得于开发维护。
项目结构:
持久层dao接口
UserDao.java
public interface UserDao{
// 通过ID查询一个用户
public User findUserById(Integer id);
// 根据用户名模糊查询用户列表
public List<User> findUserByUserName(String userName);
// 添加用户
public int insertUser(User user);
// 更新用户
public void updateUserById(User user);
// 删除用户
public void deleteUserById(Integer id);
}
持久层dao接口实现类
UserDaoImpl.java
/**
* @ClassName: 持久层实现类
* @author: benjamin
* @createTime: 2019/07/14/20:22
*/
public class UserDaoImpl implements UserDao{
private SqlSessionFactory sqlSessionFactory;
// 通过构造方法注入
public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
this.sqlSessionFactory = sqlSessionFactory;
}
public User findUserById(Integer id) {
//sqlSession是线程不安全的,所以它的最佳使用范围在方法体内
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = sqlSession.selectOne("test.findUserById",id);
sqlSession.close();
return user;
}
public List<User> findUserByUserName(String userName) {
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> list = sqlSession.selectList("test.findUserByUsername", userName);
sqlSession.close();
return list;
}
// 插入用户
public int insertUser(User user){
SqlSession sqlSession = sqlSessionFactory.openSession();
int res = sqlSession.insert("test.insertUser", user);
sqlSession.commit();
sqlSession.close();
return res;
}
// 更新用户
public void updateUserById(User user){
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.update("test.updateUserById",user);
sqlSession.commit();
sqlSession.close();
}
// 删除用户
public void deleteUserById(Integer id){
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.delete("test.deleteUserById",id);
sqlSession.commit();
sqlSession.close();
}
}
持久层映射
<?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">
<!--namespace:用来区别不同的类的名字 -->
<mapper namespace="test">
<!-- 通过Id查询一个用户 -->
<select id="findUserById" parameterType="Integer" resultType="com.ben.domain.User">
select * from user where id = #{v}
</select>
<!-- 根据用户名模糊查询用户列表 -->
<!-- select * from user where username like '%${value}%'-->
<!-- select * from user where username like "%"#{value}"%"-->
<select id="findUserByUsername" parameterType="String" resultType="com.ben.domain.User">
select * from user where username like "%"#{value}"%"
</select>
<!-- 添加用户 -->
<insert id="insertUser" parameterType="com.ben.domain.User">
<selectKey keyProperty="id" resultType="Integer" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into user (username,birthday,address,sex) values(#{username},#{birthday},#{address},#{sex})
</insert>
<!-- 更新用户 -->
<update id="updateUserById" parameterType="com.ben.domain.User">
update user
set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address}
where id = #{id}
</update>
<!-- 删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user
where id = #{vvvvv}
</delete>
</mapper>
测试类
UserDaoTest.java
/**
* @ClassName: UserDaoTest
* @author: benjamin
* @version: 1.0
* @description: TODO
* @createTime: 2019/07/14/20:38
*/
public class UserDaoTest {
private SqlSessionFactory factory;
private UserDao userdao;
private InputStream in;
// 作用:在测试方法前执行这个方法
@Before
public void setUp() throws Exception{
//1.读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
factory = new SqlSessionFactoryBuilder().build(in);
}
@After//在测试方法执行完成之后执行
public void destroy() throws IOException {
in.close();
}
//通过Id查询一个用户
@Test
public void testFindUserById() throws Exception{
// 将初始化好的工厂注入到实现类中
UserDao userdao = new UserDaoImpl(factory);
User user = userdao.findUserById(34);
System.out.println(user);
//User [id=34, username=小小, sex=1, birthday=Mon Jul 15 00:00:00 CST 2019, address=西安市]
}
//根据用户名模糊查询用户列表
@Test
public void testFindUserByUserName() throws Exception{
UserDao userdao = new UserDaoImpl(factory);
List<User> list = userdao.findUserByUserName("王");
for (User user:list) {
System.out.println(user);
}
}
//添加用户
@Test
public void testInsertUser() throws IOException {
UserDao userdao = new UserDaoImpl(factory);
User user = new User();
user.setUsername("小王");
user.setBirthday(new Date());
user.setAddress("sadfsafsafs");
user.setSex("2");
int i = userdao.insertUser(user);
System.out.println("插入id:"+user.getId());//插入id:35
}
//更新用户
@Test
public void testUpdateUserById() throws IOException {
UserDao userdao = new UserDaoImpl(factory);
User user = new User();
user.setId(35);
user.setUsername("小小");
user.setBirthday(new Date());
user.setAddress("西安市");
user.setSex("1");
userdao.updateUserById(user);
System.out.println(user.getId());
}
//删除用户
@Test
public void testDeleteUserById() throws IOException {
UserDao userdao = new UserDaoImpl(factory);
userdao.deleteUserById(34);
}
}