mybatis高级查询,批量新增
生活随笔
收集整理的這篇文章主要介紹了
mybatis高级查询,批量新增
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
review
- sql腳本
- 實(shí)體類
- 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 );實(shí)體類
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 ]]> 轉(zhuǎn)義 --><!-- 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 拼接好的參數(shù) --><select id="getStuInfoByName1" parameterType="string" resultMap="stuInfoMap"><include refid="stu_common" />where stu_name like #{stuName}</select><!-- 模糊查詢2 直接給參數(shù) ${} --><select id="getStuInfoByName2" parameterType="string" resultMap="stuInfoMap"><include refid="stu_common" />where stu_name like '%${stuName}%'</select><!-- 模糊查詢3 concat函數(shù) --><select id="getStuInfoByName3" parameterType="string" resultMap="stuInfoMap"><include refid="stu_common" />where stu_name like concat('%', #{stuName}, '%')</select><!-- 模糊查詢4 bind標(biāo)簽 value為固定寫法 stuName是查詢條件變量,換成別的變量也可。 --><select id="getStuInfoByName4" parameterType="string" resultMap="stuInfoMap"><bind name="bindStuName" value="'%' + stuName + '%'"/><include refid="stu_common" />where stu_name like #{bindStuName}</select><!-- 如果兩個(gè)給了 按姓名模糊 年齡 查 ,否則查所有--><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標(biāo)簽List<StuInfo> getStuInfoByName4(String stuName);// where if 動態(tài)sqlList<StuInfo> getStuInfoByCondition(StuInfo stuInfo);// set if 拼接 修改語句/*** 根據(jù)id修改學(xué)生信息* @param stuInfo 要修改的學(xué)生* @return 受影響的行數(shù)*/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 函數(shù)// 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);}}總結(jié)
以上是生活随笔為你收集整理的mybatis高级查询,批量新增的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 量子计算机完整的图片,记者带你走近世界首
- 下一篇: 江西计算机一级考试教程,江西省2019年