Mybatis framework III: DAO layer development, Mapper dynamic agent development

Here is the basic construction: http://www.cnblogs.com/xuyiqing/p/8600888.html

Next, we did simple addition, deletion, modification and query: http://www.cnblogs.com/xuyiqing/p/8601506.html

 

But we found that there were too many duplicate codes

Next, integrate and implement DAO development:

 

1: Original DAO development:

package dao;

import pojo.User;

public interface UserDao {
    public User selectUserById(Integer id);
}
package dao;

import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import pojo.User;

public class UserDaoImpl implements UserDao {

    //injection
    private SqlSessionFactory sqlSessionFactory;
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }
    
    //Through user ID Query a user
    public User selectUserById(Integer id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession.selectOne("test.findUserById", id);
    }
    //Fuzzy query by user name
    public List<User> selectUserByUsername(Integer id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession.selectList("test.findUserById", id);
    }
}

Test class:

package junit;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import dao.UserDao;
import dao.UserDaoImpl;
import pojo.User;

public class DaoTest {
    public SqlSessionFactory sqlSessionFactory;
    @Before
    public void before() throws Exception {
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
    }
    @Test
    public void testDao() throws Exception {
        UserDao userDao = new UserDaoImpl(sqlSessionFactory);
        User user = userDao.selectUserById(10);
        System.out.println(user);
    }
}

 

 

But we still find problems such as code duplication and waste of resources

So I think of Mapper dynamic agent development:

package mapper;

import pojo.User;

public interface UserMapper {

    
    //Follow four principles
    //Interface method name  == User.xml in id name
    //Return value type and  Mapper.xml The return value type in the file should be consistent
    //The type of input parameter of method and Mapper.xml The type of the middle input parameter should be consistent
    //Namespace binding this interface
    
    //Here, if an object is returned, the selectOne Method
    //If it is List,Then call selectlist Method
    public User findUserById(Integer id);
    
}

 

UserMapper.xml

<?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">
<!-- write Sql Sentence   -->
<mapper namespace="mapper.UserMapper">
    <!-- adopt ID Query a user -->
    <select id="findUserById" parameterType="Integer" resultType="pojo.User">
        select * from user where id = #{v}
    </select>
    
    <!-- //Fuzzy query user list based on user name
    #{}    select * from user where id = ?     Placeholder? = = 'five'
    ${}    select * from user where username like '%Five%'  String splicing  
    
     -->
    <select id="findUserByUsername" parameterType="String" resultType="pojo.User">
        select * from user where username like "%"#{haha}"%"
    </select>
    
    <!-- Add user -->
    <insert id="insertUser" parameterType="pojo.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>
    
    <!-- To update -->
    <update id="updateUserById" parameterType="pojo.User">
        update user 
        set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address}
        where id = #{id}
    </update>
    
    <!-- delete -->
    <delete id="deleteUserById" parameterType="Integer">
        delete from user 
        where id = #{vvvvv}
    </delete>


</mapper>

 

Main configuration file sqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url"
                    value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
                <property name="username" value="root" />
                <property name="password" value="xuyiqing" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/> 
    </mappers>
</configuration>

 

 

Test class:

package junit;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import mapper.UserMapper;
import pojo.User;

public class MybatisMapperTest {

    
    @Test
    public void testMapper() throws Exception {
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //SqlSession Automatically generate an implementation class for the interface 
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.findUserById(10);
        System.out.println(user);
    }
}

 

 

In general, Mapper dynamic agent development is recommended

Keywords: Java xml Apache Session Mybatis

Added by jomofee on Tue, 31 Mar 2020 22:11:25 +0300