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,一经查实,立即删除!

相关文章

Java对存储过程的调用方法

本文将介绍Java怎样实现对存数过程的调用方法&#xff0c;作者用了几个样例进行了具体的说明&#xff0c;简单明了&#xff0c;很适合刚開始学习的人。 一、Java怎样实现对存储过程的调用&#xff1a; A&#xff1a;不带输出參数的 create procedure getsum n int 0<--此处…

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的定义、作用以及丢失的原因&#…

windows下面虚拟主机

1. wondows xp apmserv 5.2.6 端口8088 2.httpd.config文件 1. ServerRoot      "D:/APMServ/APMServ5.2.6/Apache" Listen        8088 ServerName     127.0.0.1:8088 DocumentRoot   "D:/APMServ/APMServ5.2.6/www/" (原文未变)…

QueryWrapper查询

QueryWrapper, mybatisplus 中封装了大量的查询方法。用于高级查询。传入表的列和查询的值&#xff0c;就能反射对应的sql. 简化了查询。 更多api&#xff0c;查询使用&#xff0c;看&#xff1a; https://baomidou.com/guide/wrapper.html package cn.bitqian;import cn.bit…

Smooks转换设计

Smooks转换设计 背景 不同的合作银行对应的外部接口是不一样的,我们需要把外部这些变化不定的接口格式,转换为我们银保通系统可以识别的内部接口.Smooks可以很好的解决这一问题.并且,当合作银行的接口随着业务的变化而发生变化时,smooks只需要通过变更转换模板,就可以实现对变化…

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;比如老式电…

ssm整合,使用maven分模块

linux, docker&#xff0c;redis&#xff0c;rabbit兔子&#xff0c;es… juc… 还有最近的项目&#xff0c;单线程快block了。 代码&#xff1a;整合 https://github.com/sevenyoungairye/ssm-demo/tree/master/ssm_maven 你需要&#xff1a; git clone gitgithub.com:seve…

Linux基本命令 (一)

linux is not unix … 命令使用描述pwdpwd查看当前的目录路径whowho 或者 who am i 或者 whoami查看当前用户cdcd /usr/bin 或者 cd / 或者 cd ../进入usr的bin目录下&#xff1b;进入根目录;或者进入上一个目录cdcd directorycd 相对路径或者绝对路径lsls -a 或者 ls - l 或者…

【JSP】jsp报错:Syntax error, insert } to complete MethodBody

使用MyEclipse编写JSP的时候有时会报错误如下 Syntax error, insert "}" to complete MethodBody 大体意思就是说方法体缺少缺少一个"}"&#xff0c;但是一般都找不到到底是哪里缺少了"}"&#xff0c;而且一般情况下不影响程序的正常运行&#x…

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

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

思维探索者:从问题到答案的思维过程 像侦探一样思考

http://www.nowamagic.net/librarys/veda/detail/1710目前几乎所有的算法书的讲解方式都是欧几里德式的、瀑布式的、自上而下的、每一个推导步骤都是精准制导直接面向目标的。由因到果&#xff0c;定义、引理、定理、证明 一样不少&#xff0c;井井有条一丝不乱毫无赘肉。而实际…