Explain Mybatis in simple terms (3) to develop the original dao

Use the original dao development method to develop a dao.

First you need to define a dao interface

public interface UserDao {
    /**
    *   @author:kevin
    * @Description: Query user information based on id
    *   @Date:20:33 2018/3/28
    */
    public User findUserById(int id);

    /**
     *   @author:kevin
     * @Description: Insert user information
     *   @Date:20:33 2018/3/28
     */
    public void insertUser(User user);

    /**
     *   @author:kevin
     * @Description: delete user information based on id
     *   @Date:20:33 2018/3/28
     */
    public void dalateUser(int id);

    /**
     *   @author:kevin
     * @Description: Modify user information
     *   @Date:20:33 2018/3/28
     */
    public void updateUserBy(User user);
}

Due to the thread insecurity of SqlSession, we need to define it inside the method and define the implementation class of the interface.

public class UserDaoImpl implements UserDao {
    private SqlSessionFactory sqlSessionFactory;
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
        this.sqlSessionFactory = sqlSessionFactory;
    }
    @Override
    public User findUserById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = sqlSession.selectOne("selectUser",id);
        sqlSession.close();
        return user;
    }

    @Override
    public void insertUser(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.insert("test.insertUser",user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void dalateUser(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //Operate the database through sqlsession
        sqlSession.delete("test.deleteUserById",39);
        // commit the transaction
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateUserBy(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.update("test.updateUser",user);
        // commit the transaction
        sqlSession.commit();
        sqlSession.close();
    }

Next, we use junit to test the method of dao.

public class UserDaoTest {
    private SqlSessionFactory factory;
    private UserDao dao;
    @Before
    public void loadFactory() throws Exception{
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //create session factory
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void testfindUserById(){
        dao = new UserDaoImpl(factory);
        User user = dao.findUserById(1);
        System.out.println(user);
    }
    @Test
    public void testInsertUser(){
        dao = new UserDaoImpl(factory);
        User user = new User();
        user.setUsername("King Qin");
        user.setBirthday(new Date());
        user.setSex("1");
        user.setAddr("Chengdu, Sichuan");
        dao.insertUser(user);
    }
    @Test
    public void testDalateUser(){
        dao = new UserDaoImpl(factory);
        User user = new User();
        user.setId(38);
        user.setUsername("Qingyun");
        user.setBirthday(new Date());
        user.setSex("1");
        user.setAddr("Chengdu, Sichuan");
        dao.updateUserBy(user);
    }

Running each test case program is output correctly. At this point, the original dao development method ends.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325474759&siteId=291194637