Mybatis dynamic agent development method and dynamic sql statement understanding

Dynamic agent development method

Using Mybatis's agent development method to realize the development of the DAO layer, this method is the mainstream for us to enter the enterprise later.

The Mapper interface development method only requires programmers to write the Mapper interface (equivalent to the Dao interface), and the Mybatis framework creates the dynamic proxy object of the interface according to the interface definition. The method body of the proxy object is the same as the Dao interface implementation class method above.

 

Mapper interface development needs to follow the following specifications:

1) The namespace in the Mapper.xml file is the same as the fully qualified name of the mapper interface

2) The Mapper interface method name is the same as the id of each statement defined in Mapper.xml

3) The input parameter type of the Mapper interface method is the same as the parameterType of each SQL defined in mapper.xml

4) The output parameter type of the Mapper interface method is the same as the resultType of each SQL defined in mapper.xml

As shown below:

 @Test
    //查询操作
    public void test1() throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得session回话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作  参数:namespace+id
        List<User> userList = sqlSession.selectList("userMapper.findAll");
        //打印数据
        System.out.println(userList);
        //释放资源
        sqlSession.close();
    }

The test code we wrote before is like this: List<User> userList = sqlSession.selectList("userMapper.findAll"); It is not necessary to specify whether it is an update, a query, or a deletion. Using dynamic proxy, let mybatis to dynamically generate UserMapper for us userMapper = sqlSession.getMapper(UserMapper.class);

 public static void main(String[] args) throws IOException {

        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        System.out.println(all);
    }

MyBatis mapping file in-depth dynamic sql statement

1. The <if> of dynamic SQL makes our sql statement flexible, and it is no longer hard to write.

For example: if id is not empty, you can query according to id, if username is not empty, add username as a condition. Implemented without changing the code.

We can achieve this by using if in UserMapper.xml. When you send the entity User, if there is only id, then only query by id, if there are id and username, query both together.

<select id="findByCondition" parameterType="user" resultType="user">
    select * from User
    <where>
        <if test="id!=0">
            and id=#{id}
        </if>
        <if test="username!=null">
            and username=#{username}
        </if>
    </where>
</select>

2. Dynamic SQL <foreach>

For example: we want to query how to query id=1 or id=2 or id=3

<select id="findByIds" parameterType="list" resultType="user">
        <include refid="selectUser"></include>
        <where>
            <foreach collection="list" open="id in(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>

The attributes of the foreach tag have the following meanings:

The <foreach> tag is used to traverse the collection, its attributes:

•Collection: Represents the collection elements to be traversed, pay attention not to write #{} when writing

•Open: represents the beginning of the statement

•Close: represents the end part

•Item: represents each element of the traversal collection, the variable name generated

•Sperator: represents the separator

public static void main(String[] args) {
        
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);

        List<User> userList = mapper.findByIds(ids);
        System.out.println(userList);
    } 


 

Guess you like

Origin blog.csdn.net/weixin_44126152/article/details/108516150