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);}}

mybatis高级查询,批量新增相关推荐

  1. 基于ruoyi+vue+elementUI实现列表,新增,附件上传,tab+springBoot+mybatis+oracle序列+批量新增

    基于ruoyi+vue+elementUI实现列表,新增,附件上传,tab+springBoot+mybatis+oracle序列+批量新增 页面效果 列表页面 新增页面 详情页面 代码实现 列表+新 ...

  2. 2.4.3 Mybatis 高级查询, 复杂映射, 返回主键, 动态SQL if, set, foreach, 核心配置文件深入,plugins标签, 多表查询, 嵌套查询

    目录 Mybatis 复杂映射&配置文件深入 一 Mybatis高级查询 1.1 ResutlMap属性 1.2 多条件查询(三种) 1.3 模糊查询 二 Mybatis映射文件深入 2.1 ...

  3. MyBatis + Oracle 实现批量新增和批量修改

    MyBatis + Oracle 实现批量新增(基于序列化自增长主键) mapper 接口 void batchInsert(List<ASingleProject> list); map ...

  4. oracle批量新增字段工具,mybatis 中oracle 批量新增三种方法

    第一种 < insert  id =" insert_table "  parameterClass ="java.util.List" > ins ...

  5. Mybatis Plus重写批量新增和批量删除

    批量新增: import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus. ...

  6. mybatis高级查询

    mapper.xml中的全部代码 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapp ...

  7. Java后端--53--Mybatis2:Mybatis的查询和新增操作

    一.增加调试日志 1.添加pom.xml依赖,导入slf4j框架 <!-- 日志依赖包 --> <dependency><groupId>org.slf4j< ...

  8. mybatis动态查询(分页排序搜索)+分解关联查询+Logback 日志配置(打印sql到控制台)+mybatis新增记录后返回自增的id。批量=11/2~11/20

    一.mybatis动态查询(分页排序搜索) mybatis框架分页实现,有几种方式,最简单的就是利用原生的sql关键字limit来实现,还有一种就是利用interceptor来拼接sql,实现和lim ...

  9. java回顾:MyBatis参数、sql片段、动态sql、高级查询

    目录 一.MyBatis参数 SqlSessiong工具类 1.映射文件配置-入参 1.1 parameterType入参 1.2 单个入参,变量名任意定义: 1.3 多个入参,解决方案: 1.4 p ...

最新文章

  1. 把磁盘崩溃了,总是进去维护模式,却删除不了fstab文件中的内容
  2. 东方卫视演得泰坦机器人_机器人“舞林大会”燃爆全民定向运动大赛
  3. 一种创建进程间COM来启动IE的方式
  4. Java中的队列同步器AQS
  5. python模块编程教程_python进阶教程之模块(module)介绍
  6. mysql 日均pv100w_日均百万PV架构第四弹(分布式监控)_MySQL
  7. Django的学习(六)————templates过滤器、Django shell、admin
  8. 基本功 | Java即时编译器原理解析及实践
  9. ZABBIX各版本之间的兼容性​
  10. 路由器工作模式Classless与Classful实验分析
  11. Java打印9*9乘法表
  12. COMSOL—— LiveLink for MATLAB学习1
  13. centos7.4上tecplot 奔溃 用python时
  14. 订餐系统(饿了某)java程序实现
  15. Genymotion常见问题解决方案
  16. mysql不能使用 mysql -u root -p 启动报错解决
  17. 全局记录RabbitMQ的消费者消息日志
  18. 第 11 章 基于小波技术进行图像融合--MATLAB人工智能深度学习模块
  19. 关于软考证书专项扣除填报抵扣个税
  20. python 爬取微信朋友圈的一些信息

热门文章

  1. 前端学习(769):new关键字执行过程
  2. flume简介(大数据技术)
  3. 怎没用计算机算e的,小E教你们如何用计算机算虚数
  4. 怎样用php写入数据库表,PHP如何将数据写入到MYSQL数据库
  5. 工业机器人工具中心点标定的意义_如何理解工业机器人的工具中心点
  6. centos7安装svn客户端和使用
  7. 统计学习方法 学习笔记(五):支持向量机(下)
  8. mysql的命令行安装,忘记密码,密码重置问题
  9. 各种推荐资料汇总。。。
  10. nHibernate Mapping By Code - Introduction