MyBatis——MyBatis的原始Dao开发(了解)

发布时间:2023年12月20日

MyBatis的原始Dao开发-了解

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

拷贝之前的工程:

在这里插入图片描述

1.创建接口

public interface UserDao {
    //查询所有User信息
    public List<User> findAll();

    //根据id查询User
    public User findUserById(Integer id);

    //保存用户信息
    public void saveUser(User user);

    //修改用户
    public void updateUser(User user);

    //删除用户
    public void deleteUserById(Integer id);
}

2.创建接口实现类

public class UserDaoImpl implements UserDao {

    private SqlSession sqlSession;

    public UserDaoImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<User> selectAll() {
        List<User> userList = sqlSession.selectList("com.wt.dao.UserDao.findAll");
        sqlSession.close();
        return userList;
    }

    public User selectUserById(Integer id) {
        User user = sqlSession.selectOne("com.wt.dao.UserDao.findUserById", id);
        sqlSession.close();
        return user;
    }

    public void saveUser(User user) {
        sqlSession.insert("com.wt.dao.UserDao.saveUser", user);
        sqlSession.commit();
        sqlSession.close();
    }

    public void updateUser(User user) {
        sqlSession.update("com.wt.dao.UserDao.updateUser", user);
        sqlSession.commit();
        sqlSession.close();
    }

    public void deleteUserById(Integer id) {
        sqlSession.delete("com.wt.dao.UserDao.deleteUserById", id);
        sqlSession.commit();
        sqlSession.close();
    }
}

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="com.wt.dao.UserDao">

    <!--查询所有用户信息-->
    <select id="findAll" resultType="com.wt.pojo.User">
        select * from user
    </select>

    <!--根据id查询用户信息-->
    <select id="findUserById" parameterType="int" resultType="com.wt.pojo.User">
        select * from user where id=#{id}
    </select>

    <!--保存用户信息-->
    <insert id="saveUser" parameterType="com.wt.pojo.User">
        insert into user(username,password,birthday,sex,address) 
        values(#{username},#{password},#{birthday},#{sex},#{address});
    </insert>

    <!--
     修改用户信息
    -->
    <update id="updateUser" parameterType="com.wt.pojo.User">
        update user set username=#{username},birthday=#{birthday},
        sex=#{sex},address=#{address} where id=#{id}
    </update>

    <!--删除用户-->
    <delete id="deleteUserById" parameterType="int">
       delete from user where id=#{id}
    </delete>
</mapper>

4.测试

public class MyBatisTest {

    private InputStream inputStream;
    //数据库会话实例
    private SqlSession sqlSession;

    @Before
    public void createSqlSession() throws IOException {
        //1.Resources:加载配置文件
        String resource = "SqlMapConfig.xml";
        inputStream = Resources.getResourceAsStream(resource);

        //2.SqlSessionFactoryBuilder:构建SqlSessionFactory工厂
        //3.sqlSessionFactory:创建SqlSession对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //4.SqlSession:创建代理对象
        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void testFindAll() throws Exception{
        UserDao userDao = new UserDaoImpl(sqlSession);
        List<User> userList = userDao.findAll();
        for(User user : userList){
            System.out.println(user);
        }
    }

    @Test
    public void testFindUserById(){
        UserDao userDao = new UserDaoImpl(sqlSession);
        User user = userDao.findUserById(42);
        System.out.println(user);
    }

    @Test
    public void testSaveUser(){
        UserDao userDao = new UserDaoImpl(sqlSession);
        User user = new User();
        user.setUsername("张宇");
        user.setPassword("111");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("台湾");
        userDao.saveUser(user);

    }

    @Test
    public void testUpdateUser(){
        UserDao userDao = new UserDaoImpl(sqlSession);
        User user = new User();
        user.setUsername("张二宇");
        user.setPassword("222");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("香港");
        user.setId(50);
        userDao.updateUser(user);
    }

    @Test
    public void testDeleteUser(){
        UserDao userDao = new UserDaoImpl(sqlSession);
        userDao.deleteUserById(50);
    }

    @After
    public void closeSqlSession() throws IOException {
        sqlSession.close();
        inputStream.close();
    }
}
文章来源:https://blog.csdn.net/wan_m_m/article/details/135116073
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。