mybatis高级查询,批量新增

本文详细探讨了Mybatis中如何进行高级查询和批量新增操作,从编写sql脚本、创建实体类到设置mapper和进行mapper测试,全方位解析了相关步骤与注意事项。
摘要由CSDN通过智能技术生成


之前的比较分散,自己用。。。

sql脚本

-- auto-generated definition
create table stu_info
(
    stu_id     int auto_increment
        primary key,
    stu_name   varchar(255) null,
    stu_age    int(255)     null,
    stu_gender varchar(4)   null,
    stu_birth  date         null
);

实体类

package cn.bitqian.entity;

import java.util.Date;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author echo lovely
 * @date 2020年11月16日 下午7:13:32
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class StuInfo {
   
	
	private Integer stuId;
	private String stuName;
	private Integer stuAge;
	private String stuGender;
	private Date stuBirth;

}

sql watch out

<?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="cn.bitqian.mapper.StuInfoMapper">

    <resultMap id="stuInfoMap" type="StuInfo">
        <id property="stuId" column="stu_id" />
        <result property="stuName" column="stu_name" />
        <result property="stuAge" column="stu_age" />
        <result property="stuGender" column="stu_gender" />
        <result property="stuBirth" column="stu_birth" />
    </resultMap>

    <!-- 使用 <![CDATA[ sql ]]> 转义 -->
    <!--  select * from stu_info where stu_age  <![CDATA[ < ]]> #{stuAge} -->
    <select id="getStuInfoByAge" parameterType="int" resultMap="stuInfoMap">
        <![CDATA[
            select * from stu_info where stu_age  < #{stuAge}
        ]]>
    </select>


    <!-- sql片段抽取 -->
    <sql id="stu_common"> select * from stu_info </sql>

    <!-- 模糊查询1 拼接好的参数 -->
    <select id="getStuInfoByName1" parameterType="string" resultMap="stuInfoMap">
        <include refid="stu_common" />
        where stu_name like #{stuName}
    </select>

    <!-- 模糊查询2 直接给参数 ${} -->
    <select id="getStuInfoByName2" parameterType="string" resultMa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值