Dynamic sql and paging

  • Dynamic SQL in mybatis
  • Fuzzy query in mybatis
  • Result mapping in mybatis

1.Dynamic SQL in mybatis

Mybatis dynamic SQL is a function used to dynamically generate SQL statements based on different conditions. It can achieve:

1. Conditional query
Mybatis dynamic SQL allows developers to dynamically construct query statements based on different conditions. For example, by using tags, you can include or exclude certain parts of your query based on certain criteria, allowing for more flexible queries.

2. Splicing SQL statements
Dynamic SQL can also be used to splice SQL statements. This allows developers to build SQL statements based on specific needs at runtime, thereby adapting to more complex business logic.

3. Improve code reusability
By using dynamic SQL, repeated SQL codes can be reduced and code reusability improved. For example, you can define reusable SQL fragments and reuse them in different queries through tags.

4. Enhance the flexibility of SQL statements.
Mybatis dynamic SQL provides fine control of SQL statements by using tags such as,,, etc. This enhances the flexibility of SQL statements, allowing developers to precisely control the behavior of queries based on specific business needs.

5. Avoid the risk of SQL injection.
Correct use of Mybatis dynamic SQL can reduce the risk of SQL injection. Application security can be enhanced by using parameterized queries and avoiding string concatenation.

MyBatis' dynamic SQL refers to the flexible operation of SQL statements; it is based on OGNL expressions and flexibly splices and assembles SQL statements through if, choose, when, otherwise, trim, where, set, and foreach tags, thereby improving development Personnel efficiency.

<?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.zlj.mapper.BookMapper">
  <resultMap id="BaseResultMap" type="com.zlj.model.Book">
    <constructor>
      <idArg column="bid" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="bname" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="price" javaType="java.lang.Float" jdbcType="REAL" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List">
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlj.model.Book">
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.Book">
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        bid,
      </if>
      <if test="bname != null">
        bname,
      </if>
      <if test="price != null">
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null">
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlj.model.Book">
    update t_mvc_book
    <set>
      <if test="bname != null">
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlj.model.Book">
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="com.zlj.model.Book">
    <constructor>
      <idArg column="bid" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="bname" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="price" javaType="java.lang.Float" jdbcType="REAL" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List">
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlj.model.Book">
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.Book">
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        bid,
      </if>
      <if test="bname != null">
        bname,
      </if>
      <if test="price != null">
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null">
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlj.model.Book">
    update t_mvc_book
    <set>
      <if test="bname != null">
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlj.model.Book">
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
 <!-- <resultMap id="BaseResultMap" type="com.Book">
    <constructor>
      <idArg column="bid" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="bname" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="price" javaType="java.lang.Float" jdbcType="FLOAT" />
    </constructor>
  </resultMap>-->
  <sql id="Base_Column_List">
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    *
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlj.model.Book">
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.Book">
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        bid,
      </if>
      <if test="bname != null">
        bname,
      </if>
      <if test="price != null">
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null">
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlj.model.Book">
    update t_mvc_book
    <set>
      <if test="bname != null">
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlj.model.Book">
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="com.zlj.model.Book">
    <constructor>
      <idArg column="bid" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="bname" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="price" javaType="java.lang.Float" jdbcType="REAL" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List">
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultType="com.zlj.model.Book">
    select 
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlj.model.Book">
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.Book">
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        bid,
      </if>
      <if test="bname != null">
        bname,
      </if>
      <if test="price != null">
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null">
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlj.model.Book">
    update t_mvc_book
    <set>
      <if test="bname != null">
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlj.model.Book">
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <select id="selectByBids" parameterType="java.util.List" resultType="com.zlj.model.Book" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid =
    <foreach collection="bids" item="bid" open="(" close=")" separator=",">
        #{bid}
    </foreach>
  </select>

</mapper>
package com.zlj.mapper;

import com.zlj.model.Book;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookMapper {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);

    List<Book> selectByBids(@Param("bids") List bids);

}
package com.zlj.biz.impl;

import com.zlj.biz.BookBiz;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;

import java.util.List;

/**
 * @author zlj
 * @create 2023-08-21 8:49
 */
public class BookBizImpl implements BookBiz {

    private  BookMapper bookMapper;

    public BookMapper getBookMapper() {
        return bookMapper;
    }

    @Override
    public int deleteByPrimaryKey(Integer bid) {
        return bookMapper.deleteByPrimaryKey(bid);
    }

    @Override
    public int insert(Book record) {
        return bookMapper.insert(record);
    }

    @Override
    public int insertSelective(Book record) {
        return bookMapper.insertSelective(record);
    }

    @Override
    public Book selectByPrimaryKey(Integer bid) {
        return bookMapper.selectByPrimaryKey(bid);
    }

    @Override
    public int updateByPrimaryKeySelective(Book record) {
        return bookMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Book record) {
        return bookMapper.updateByPrimaryKey(record);
    }

    @Override
    public BookMapper setBookMapper(BookMapper mapper) {
        return this.bookMapper = mapper;
    }

    @Override
    public List<Book> selectByBids(List bids) {
        return bookMapper.selectByBids(bids);
    }

}
package com.zlj.biz;

import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;

import java.util.List;

public interface BookBiz {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);

    BookMapper setBookMapper(BookMapper mapper);

    List<Book> selectByBids(List bids);

}
package com.zlj.demo;


import com.zlj.biz.impl.BookBizImpl;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import com.zlj.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @author zlj
 * @create 2023-08-21 9:07
 */

public class demo1 {
    private SqlSession sqlSession;
    private BookBizImpl bookBiz;
    @Before
    public void a(){
        System.out.println("执行测试方法之前会执行的初始化代码块");
        sqlSession= SessionUtil.openSession();
        BookBizImpl bookBiz=new BookBizImpl();
        BookMapper mapper=sqlSession.getMapper(BookMapper.class);
        bookBiz.setBookMapper(mapper);
        this.bookBiz=bookBiz;
    }
    @After
    public void b(){
        System.out.println("执行测试方法之后会执行");
        sqlSession.commit();
    }
    @Test
    public void test1(){
        System.out.println("测试方法。。。");
        Book book = bookBiz.selectByPrimaryKey(33);
        System.out.println(book);
    }
@Test
    public void test2(){
        bookBiz.deleteByPrimaryKey(34);
    }
@Test
public void testById(){
    List<Integer> bids = Arrays.asList(new Integer[]{56, 57, 58, 59, 60});
    bookBiz.selectByBids(bids).forEach(System.out::print);//jdk1.8新特性
//    for (Book selectByBid : bookBiz.selectByBids(bids)) {
//        System.out.println(selectByBid);
//    }
}
   
}

 

 2. Fuzzy query in mybatis (three types: like1, like2, like3)

The difference between # and $ in mybatis

1.$ is placeholder parameter passing (like2), # is preprocessing SQL (like1, like3)

2. External form, $ is passed without quotation marks, # is passed with quotation marks.

3.$Pass parameter exists sql injection, # does not exist

4.$ can be used to make dynamic columns and complete the development of dynamic sql

<?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.zlj.mapper.BookMapper">
  <resultMap id="BaseResultMap" type="com.zlj.model.Book">
    <constructor>
      <idArg column="bid" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="bname" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="price" javaType="java.lang.Float" jdbcType="REAL" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List">
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlj.model.Book">
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.Book">
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        bid,
      </if>
      <if test="bname != null">
        bname,
      </if>
      <if test="price != null">
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null">
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlj.model.Book">
    update t_mvc_book
    <set>
      <if test="bname != null">
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlj.model.Book">
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="com.zlj.model.Book">
    <constructor>
      <idArg column="bid" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="bname" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="price" javaType="java.lang.Float" jdbcType="REAL" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List">
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlj.model.Book">
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.Book">
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        bid,
      </if>
      <if test="bname != null">
        bname,
      </if>
      <if test="price != null">
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null">
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlj.model.Book">
    update t_mvc_book
    <set>
      <if test="bname != null">
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlj.model.Book">
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
 <!-- <resultMap id="BaseResultMap" type="com.Book">
    <constructor>
      <idArg column="bid" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="bname" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="price" javaType="java.lang.Float" jdbcType="FLOAT" />
    </constructor>
  </resultMap>-->
  <sql id="Base_Column_List">
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    *
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlj.model.Book">
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.Book">
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        bid,
      </if>
      <if test="bname != null">
        bname,
      </if>
      <if test="price != null">
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null">
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlj.model.Book">
    update t_mvc_book
    <set>
      <if test="bname != null">
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlj.model.Book">
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="com.zlj.model.Book">
    <constructor>
      <idArg column="bid" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="bname" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="price" javaType="java.lang.Float" jdbcType="REAL" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List">
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultType="com.zlj.model.Book">
    select 
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlj.model.Book">
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.Book">
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        bid,
      </if>
      <if test="bname != null">
        bname,
      </if>
      <if test="price != null">
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null">
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlj.model.Book">
    update t_mvc_book
    <set>
      <if test="bname != null">
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlj.model.Book">
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <select id="selectByBids" parameterType="java.util.List" resultType="com.zlj.model.Book" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid =
    <foreach collection="bids" item="bid" open="(" close=")" separator=",">
        #{bid}
    </foreach>
  </select>
<select id="like1" resultType="com.zking.model.Book" parameterType="java.lang.String">
  select
  <include refid="Base_Column_List" />
  from t_mvc_book
  where bname like #{bname}
</select>
  <select id="like2" resultType="com.zking.model.Book" parameterType="java.lang.String">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bname like '${bname}'
  </select>
  <select id="like3" resultType="com.zking.model.Book" parameterType="java.lang.String">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bname like concat('%',#{bname},'%')
  </select>
</mapper>

package com.zlj.mapper;

import com.zlj.model.Book;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookMapper {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);

    List<Book> selectByBids(@Param("bids") List bids);

    List<Book> like1(@Param("bname") String bname);

    List<Book> like2(@Param("bname") String bname);

    List<Book> like3(@Param("bname") String bname);
}
package com.zlj.biz;

import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;

import java.util.List;

public interface BookBiz {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);

    BookMapper setBookMapper(BookMapper mapper);

    List<Book> selectByBids(List bids);

    List<Book> like1(String bname);
    List<Book> like2(String bname);
    List<Book> like3(String bname);
}
package com.zlj.biz.impl;

import com.zlj.biz.BookBiz;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;

import java.util.List;

/**
 * @author zlj
 * @create 2023-08-21 8:49
 */
public class BookBizImpl implements BookBiz {

    private  BookMapper bookMapper;

    public BookMapper getBookMapper() {
        return bookMapper;
    }

    @Override
    public int deleteByPrimaryKey(Integer bid) {
        return bookMapper.deleteByPrimaryKey(bid);
    }

    @Override
    public int insert(Book record) {
        return bookMapper.insert(record);
    }

    @Override
    public int insertSelective(Book record) {
        return bookMapper.insertSelective(record);
    }

    @Override
    public Book selectByPrimaryKey(Integer bid) {
        return bookMapper.selectByPrimaryKey(bid);
    }

    @Override
    public int updateByPrimaryKeySelective(Book record) {
        return bookMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Book record) {
        return bookMapper.updateByPrimaryKey(record);
    }

    @Override
    public BookMapper setBookMapper(BookMapper mapper) {
        return this.bookMapper = mapper;
    }

    @Override
    public List<Book> selectByBids(List bids) {
        return bookMapper.selectByBids(bids);
    }

    @Override
    public List<Book> like1(String bname) {
        return bookMapper.like1(bname);
    }

    @Override
    public List<Book> like2(String bname) {
        return bookMapper.like2(bname);
    }

    @Override
    public List<Book> like3(String bname) {
        return bookMapper.like3(bname);
    }
}
package com.zlj.demo;


import com.zlj.biz.impl.BookBizImpl;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import com.zlj.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @author zlj
 * @create 2023-08-21 9:07
 */

public class demo1 {
    private SqlSession sqlSession;
    private BookBizImpl bookBiz;
    @Before
    public void a(){
        System.out.println("执行测试方法之前会执行的初始化代码块");
        sqlSession= SessionUtil.openSession();
        BookBizImpl bookBiz=new BookBizImpl();
        BookMapper mapper=sqlSession.getMapper(BookMapper.class);
        bookBiz.setBookMapper(mapper);
        this.bookBiz=bookBiz;
    }
    @After
    public void b(){
        System.out.println("执行测试方法之后会执行");
        sqlSession.commit();
    }
    @Test
    public void test1(){
        System.out.println("测试方法。。。");
        Book book = bookBiz.selectByPrimaryKey(33);
        System.out.println(book);
    }
@Test
    public void test2(){
        bookBiz.deleteByPrimaryKey(34);
    }
@Test
public void testById(){
    List<Integer> bids = Arrays.asList(new Integer[]{56, 57, 58, 59, 60});
    bookBiz.selectByBids(bids).forEach(System.out::print);//jdk1.8新特性
//    for (Book selectByBid : bookBiz.selectByBids(bids)) {
//        System.out.println(selectByBid);
//    }
}
    @Test
    public void testlike1(){
        bookBiz.like1("%圣墟%").forEach(System.out::print);//jdk1.8新特性
    }
    @Test
    public void testlike2(){
        bookBiz.like2("%圣墟%").forEach(System.out::print);//jdk1.8新特性
    }
    @Test
    public void testlike3(){
        bookBiz.like3("%圣墟%").forEach(System.out::print);//jdk1.8新特性
    }


}

 

 

3.Result mapping in mybatis

In various scenarios using mybatis, the returned results are diverse, resultType/resultMa


p

1. Return the entity class corresponding to a single table. There is only one query result. You can use resultType or resultMap (selectByPrimaryKey).

2. Return the entity class corresponding to a single table. If there are multiple query results, you can use resultType or resultMap (list01, list02)

3. Return the entity class corresponding to multiple tables. There is only one query result. Usually resultType is used, but resultMap(list03) can also be used.

4. Return entity classes corresponding to multiple tables. There are multiple query results. Usually resultType is used, but resultMap (list04) can also be used.

5. To return a single column segment and only one query result, use resultType(bname1)

6. To return a single column segment, if there are multiple query results, use resultType(bname2)

<?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.zlj.mapper.BookMapper">
  <resultMap id="BaseResultMap" type="com.zlj.model.Book">
    <constructor>
      <idArg column="bid" javaType="java.lang.Integer" jdbcType="INTEGER" />
      <arg column="bname" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="price" javaType="java.lang.Float" jdbcType="REAL" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List">
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlj.model.Book">
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.Book">
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        bid,
      </if>
      <if test="bname != null">
        bname,
      </if>
      <if test="price != null">
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="bid != null">
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null">
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlj.model.Book">
    update t_mvc_book
    <set>
      <if test="bname != null">
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlj.model.Book">
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <select id="selectByBids" parameterType="java.lang.String" resultType="com.zlj.model.Book">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
<select id="like1" parameterType="java.lang.String" resultType="com.zlj.model.Book">
  select
  <include refid="Base_Column_List" />
  from t_mvc_book
  where bname like #{bname}
</select>
  <select id="like2" parameterType="java.lang.String" resultType="com.zlj.model.Book">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bname like '${bname}'
  </select>
  <select id="like3" parameterType="java.lang.String" resultType="com.zlj.model.Book">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bname like concat('%',#{bname},'%')
  </select>
  <select id="list01" resultType="com.zlj.model.Book" >
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
  </select>
  <select id="list02" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
  </select>

<select id="list03" resultType="java.util.Map" parameterType="java.util.Map">
  select s.*,sc.cid,sc.score from t_mysql_student s,t_mysql_score sc where s.sid=sc.sid and sc.sid=#{sid} and sc.cid=#{cid}
</select>
  <select id="list04" resultType="java.util.Map" parameterType="java.util.Map">
  select s.*,sc.cid,sc.score from t_mysql_student s,t_mysql_score sc where s.sid=sc.sid
</select>
  <select id="bname1" parameterType="java.lang.Integer" resultType="java.lang.String">
    select
    bname
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <select id="bname2" parameterType="java.lang.String" resultType="java.lang.String">
    select
    bname
    from t_mvc_book
    where bname like concat('%',#{bname},'%')
  </select>
</mapper>
package com.zlj.mapper;

import com.zlj.model.Book;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface BookMapper {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);

    List<Book> selectByBids(@Param("bids") List bids);

    List<Book> like1(@Param("bname") String bname);

    List<Book> like2(@Param("bname") String bname);

    List<Book> like3(@Param("bname") String bname);

    List<Book> list01();

    List<Book> list02();

    Map list03(Map map);

    List<Map> list04(Map map);

    String bname1(@Param("bid") Integer bid);

    List<String> bname2(@Param("bname") String bname);


}
package com.zlj.biz;

import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;

import java.util.List;
import java.util.Map;

public interface BookBiz {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);

    BookMapper setBookMapper(BookMapper mapper);

    List<Book> selectByBids(List bids);

    List<Book> like1(String bname);
    List<Book> like2(String bname);
    List<Book> like3(String bname);

    List<Book> list01();
    List<Book> list02();

    Map list03(Map map);

    List<Map> list04(Map map);

    String bname1(Integer bid);

    List<String> bname2(String bname);
}
package com.zlj.biz.impl;

import com.zlj.biz.BookBiz;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;

import java.util.List;
import java.util.Map;

/**
 * @author zlj
 * @create 2023-08-21 8:49
 */
public class BookBizImpl implements BookBiz {

    private  BookMapper bookMapper;

    public BookMapper getBookMapper() {
        return bookMapper;
    }

    @Override
    public int deleteByPrimaryKey(Integer bid) {
        return bookMapper.deleteByPrimaryKey(bid);
    }

    @Override
    public int insert(Book record) {
        return bookMapper.insert(record);
    }

    @Override
    public int insertSelective(Book record) {
        return bookMapper.insertSelective(record);
    }

    @Override
    public Book selectByPrimaryKey(Integer bid) {
        return bookMapper.selectByPrimaryKey(bid);
    }

    @Override
    public int updateByPrimaryKeySelective(Book record) {
        return bookMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Book record) {
        return bookMapper.updateByPrimaryKey(record);
    }

    @Override
    public BookMapper setBookMapper(BookMapper mapper) {
        return this.bookMapper = mapper;
    }

    @Override
    public List<Book> selectByBids(List bids) {
        return bookMapper.selectByBids(bids);
    }

    @Override
    public List<Book> like1(String bname) {
        return bookMapper.like1(bname);
    }

    @Override
    public List<Book> like2(String bname) {
        return bookMapper.like2(bname);
    }

    @Override
    public List<Book> like3(String bname) {
        return bookMapper.like3(bname);
    }

    @Override
    public List<Book> list01() {
        return bookMapper.list01();
    }

    @Override
    public List<Book> list02() {
        return bookMapper.list02();
    }

    @Override
    public Map list03(Map map){
    return bookMapper.list03(map);
    }
    @Override
    public List<Map> list04(Map map){
    return bookMapper.list04(map);
    }
    @Override
    public String bname1(Integer bid) {
        return bookMapper.bname1(bid);
    }
    @Override
    public List<String> bname2(String bname) {
        return bookMapper.bname2(bname);
    }

}
package com.zlj.demo;


import com.zlj.biz.impl.BookBizImpl;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import com.zlj.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zlj
 * @create 2023-08-21 9:07
 */

public class demo1 {
    private SqlSession sqlSession;
    private BookBizImpl bookBiz;
    @Before
    public void a(){
        System.out.println("执行测试方法之前会执行的初始化代码块");
        sqlSession= SessionUtil.openSession();
        BookBizImpl bookBiz=new BookBizImpl();
        BookMapper mapper=sqlSession.getMapper(BookMapper.class);
        bookBiz.setBookMapper(mapper);
        this.bookBiz=bookBiz;
    }
    @After
    public void b(){
        System.out.println("执行测试方法之后会执行");
        sqlSession.commit();
    }
    @Test
    public void test1(){
        System.out.println("测试方法。。。");
        Book book = bookBiz.selectByPrimaryKey(33);
        System.out.println(book);
    }
@Test
    public void test2(){
        bookBiz.deleteByPrimaryKey(34);
    }
@Test
public void testById(){
    List<Integer> bids = Arrays.asList(new Integer[]{56, 57, 58, 59, 60});
    bookBiz.selectByBids(bids).forEach(System.out::print);//jdk1.8新特性
//    for (Book selectByBid : bookBiz.selectByBids(bids)) {
//        System.out.println(selectByBid);
//    }
}
    @Test
    public void testlike1(){
        bookBiz.like1("%圣墟%").forEach(System.out::print);//jdk1.8新特性
    }
    @Test
    public void testlike2(){
        bookBiz.like2("%圣墟%").forEach(System.out::print);//jdk1.8新特性
    }
    @Test
    public void testlike3(){
        bookBiz.like3("%圣墟%").forEach(System.out::print);//jdk1.8新特性
    }

    @Test
    public void testlist01(){
        bookBiz.list01().forEach(System.out::print);//jdk1.8新特性
    }
    @Test
    public void testlist02(){
        bookBiz.list02().forEach(System.out::print);//jdk1.8新特性
    }


    @Test
    public void testlist03(){
        Map map=new HashMap();
        map.put("sid","01");
        map.put("cid","01");
        System.out.println(bookBiz.list03(map));
    }
    @Test
    public void testlist04(){
        Map map=new HashMap();
        map.put("sid","01");
        map.put("cid","01");
        bookBiz.list04(map).forEach(System.out::print);
    }
    @Test
    public void testbname1(){
        System.out.println(bookBiz.bname1(66));
    }
    @Test
    public void testbname2(){
        bookBiz.bname2("圣墟").forEach(System.out::print);
    }

}

おすすめ

転載: blog.csdn.net/weixin_73471776/article/details/132462942