mybatis高级查询,批量新增

review

    • sql脚本
    • 实体类
    • sql watch out
    • mapper
    • mapper test

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

sql脚本

-- auto-generated definition
create table stu_info
(stu_id     int auto_incrementprimary 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" resultMap="stuInfoMap"><include refid="stu_common" />where stu_name like '%${stuName}%'</select><!--  模糊查询3 concat函数 --><select id="getStuInfoByName3" parameterType="string" resultMap="stuInfoMap"><include refid="stu_common" />where stu_name like concat('%', #{stuName}, '%')</select><!-- 模糊查询4 bind标签 value为固定写法 stuName是查询条件变量,换成别的变量也可。 --><select id="getStuInfoByName4" parameterType="string" resultMap="stuInfoMap"><bind name="bindStuName" value="'%' + stuName + '%'"/><include refid="stu_common" />where stu_name like #{bindStuName}</select><!-- 如果两个给了 按姓名模糊 年龄 查 ,否则查所有--><select id="getStuInfoByCondition" parameterType="StuInfo" resultMap="stuInfoMap">select * from stu_info<where><if test="stuName != null and stuName != '' "><bind name="bindStuName" value="'%' + stuName + '%'"/>and stu_name like #{bindStuName}</if><if test="stuAge != null">and stu_age = #{stuAge}</if></where></select><!-- set and if to update --><update id="updateStuInfoById" parameterType="StuInfo">update stu_info<!-- 会自动拼set 并且去掉 逗号 --><set><if test="stuName != null and stuName != '' ">stu_name = #{stuName},</if><if test="stuAge != null ">stu_age = #{stuAge},</if><if test="stuGender != null and stuGender != '' ">stu_gender = #{stuGender},</if><if test="stuBirth != null ">stu_birth = #{stuBirth},</if></set>where stu_id = #{stuId}</update><select id="getStuInfoByChoseWhen" parameterType="string" resultMap="stuInfoMap">select * from stu_info<choose><when test="stuGender != null ">where stu_name = 'jack'</when><otherwise>where stu_gender = '女'</otherwise></choose></select><!-- foreach批量查询 --><select id="getStuInfoByIds" parameterType="list" resultMap="stuInfoMap">select * from stu_infowhere stu_id in<foreach collection="list" open="(" close=")" item="id" separator=",">#{id}</foreach></select><!-- foreach 批量新增 --><!-- insert into stu_info values (null, ?, ?, ?, ?) , (null, ?, ?, ?, ?) , (null, ?, ?, ?, ?) --><insert id="addBatchStuInfo" parameterType="list">insert into stu_info values<foreach collection="list" separator=", " item="stu">(null, #{stu.stuName}, #{stu.stuAge}, #{stu.stuGender}, #{stu.stuBirth})</foreach></insert></mapper>

mapper

package cn.bitqian.mapper;import cn.bitqian.entity.StuInfo;import java.util.List;/*** @author echo lovely* @date 2020/11/16 19:31*/public interface StuInfoMapper {// 查询年龄小于小于 多少多少岁的List<StuInfo> getStuInfoByAge(int age);// 模糊查询List<StuInfo> getStuInfoByName1(String stuName);// '%${stuName}%'List<StuInfo> getStuInfoByName2(String stuName);// concat('%', #{stuName}, '%')List<StuInfo> getStuInfoByName3(String stuName);// bind标签List<StuInfo> getStuInfoByName4(String stuName);// where if 动态sqlList<StuInfo> getStuInfoByCondition(StuInfo stuInfo);// set if 拼接 修改语句/*** 根据id修改学生信息* @param stuInfo 要修改的学生* @return 受影响的行数*/int updateStuInfoById(StuInfo stuInfo);// chose when 查询List<StuInfo> getStuInfoByChoseWhen(String gender);// for 批量新增int addBatchStuInfo(List<StuInfo> list);// for in查询List<StuInfo> getStuInfoByIds(List<Integer> ids);}

mapper test

package cn.bitqian.mapper;import cn.bitqian.entity.StuInfo;
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.*;import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;/*** 测试类 stuInfo* @author echo lovely* @date 2020/11/16 19:36*/public class StuInfoMapperTest {static InputStream inputStream = null;static SqlSessionFactory sqlSessionFactory = null;private SqlSession sqlSession = null;@BeforeClasspublic static void beforeClass() {try {inputStream = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}@Beforepublic void before() {sqlSession = sqlSessionFactory.openSession(true);}@Afterpublic void after() {if (sqlSession != null) {sqlSession.close();}}@AfterClasspublic static void afterClass() {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}@Testpublic void test1() {StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);List<StuInfo> stuInfos = mapper.getStuInfoByAge(15);stuInfos.forEach(System.out::println);}@Testpublic void test2() {StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);/*String stuName = "%jack%";List<StuInfo> stuInfos = mapper.getStuInfoByName1(stuName);*/// sql注入有 ${}// List<StuInfo> stuInfos = mapper.getStuInfoByName2("jack");// concat 函数// List<StuInfo> stuInfos = mapper.getStuInfoByName3("jack");// bindList<StuInfo> stuInfos = mapper.getStuInfoByName4("jack");stuInfos.forEach(System.out::println);}@Testpublic void test3() {StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);StuInfo stuInfo = new StuInfo();stuInfo.setStuName("j");stuInfo.setStuAge(14);List<StuInfo> stuInfos = mapper.getStuInfoByCondition(stuInfo);stuInfos.forEach(System.out::println);}@Testpublic void test4() {StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);StuInfo stu = new StuInfo();stu.setStuId(1);stu.setStuBirth(new Date());mapper.updateStuInfoById(stu);}@Testpublic void test5() {StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);List<StuInfo> stuInfos = mapper.getStuInfoByChoseWhen(null);stuInfos.forEach(System.out::println);}// foreach@Testpublic void test6() {StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);List<StuInfo> stuInfos = mapper.getStuInfoByIds(Arrays.asList(1, 2, 3));stuInfos.forEach(System.out::println);}// 批量新增 addBatchStuInfo// foreach@Testpublic void test7() {StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);List<StuInfo> stuList = new ArrayList<>();stuList.add(new StuInfo(null, "a", null, null, new Date()));stuList.add(new StuInfo(null, "b", null, null, new Date()));stuList.add(new StuInfo(null, "c", null, null, new Date()));mapper.addBatchStuInfo(stuList);}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mybatis-plus 代码生成器

生成entity -> mapper-> service ->controller所有的接口&#xff0c;实现&#xff0c;一键完成。 1. 轮子 <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>…

电脑报错找不到msvcr100.dll,无法继续执行代码如何修复

在电脑使用过程中&#xff0c;我们经常会遇到一些错误提示&#xff0c;其中之一就是MSVCR100.dll丢失。那么&#xff0c;MSVCR100.dll到底是什么&#xff1f;它的作用是什么&#xff1f;为什么会丢失呢&#xff1f;本文将详细介绍MSVCR100.dll的定义、作用以及丢失的原因&#…

mybatis一级,二级缓存。缓存带来的脏读问题

title1. 关于缓存的介绍2. 一级缓存&#xff0c;默认开启&#xff0c;session级别3. 二级缓存&#xff0c;mapper 的namespace级别1. 关于缓存的介绍 Mybatis一级缓存的作用域是同一个SqlSession&#xff0c;在同一个sqlSession中两次执行相同的sql语句&#xff0c;第一次执行完…

接口限流实践

http://www.cnblogs.com/LBSer/p/4083131.html 一、问题描述 某天A君突然发现自己的接口请求量突然涨到之前的10倍&#xff0c;没多久该接口几乎不可使用&#xff0c;并引发连锁反应导致整个系统崩溃。如何应对这种情况呢&#xff1f;生活给了我们答案&#xff1a;比如老式电…

linux中的文件,文件夹,链接的权限划分

title权限代号与分组如何修改权限&#xff1a;权限代号与分组 当你敲下ll时 ll 箭头所指就表示这个文件的权限和所有者 最前面的, 以access这个文件夹为例分析。 drwxr-xr-x 分组 d rwx r-x r-x 将rwx, r-x, r-x 分为三组。 d表示是个目录。 其中rwx表示属于当前用户的权限…

centos7,google身份验证

为了安全&#xff0c;登录时需要google的动态验证码验证。当你输入服务器的登录密码后&#xff0c;输入动态验证码。这个验证码是google app 身份验证器根据当前时间算出来的。所有centos上的时间必须和当前时间同步。 在centos上安装google的身份验证器&#xff0c;结果会出现…

nginx 反向proxy多个 tomcat,负载均衡

负载均衡一&#xff0c;正向proxy与反向proxynginx二&#xff0c;nginx安装三&#xff0c;nginx负载均衡场景四&#xff0c;配置nginx访问代理多个tomcat一&#xff0c;正向proxy与反向proxynginx 正向proxy 正向proxy返向proxy nginx 客户端发送请求到服务器&#xff08;客户…

android-波浪效果ripple-background

能产生波浪效果的背景图片控件&#xff0c;可以自定义颜色&#xff0c;波浪扩展的速度&#xff0c;波浪的圈数。运行效果&#xff1a; 下载地址&#xff1a;http://jcodecraeer.com/a/opensource/2014/1110/1946.html转载于:https://www.cnblogs.com/noodlesonce/p/4090163.htm…