Le précédent est plus dispersé, alors utilisez-le vous-même. . .
script 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
);
Classe d'entité
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;
}
attention sql
<?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_info
where 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>
mappeur
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 动态sql
List<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);
}
test des dossiers
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;
@BeforeClass
public static void beforeClass() {
try {
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
@Before
public void before() {
sqlSession = sqlSessionFactory.openSession(true);
}
@After
public void after() {
if (sqlSession != null) {
sqlSession.close();
}
}
@AfterClass
public static void afterClass() {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test1() {
StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);
List<StuInfo> stuInfos = mapper.getStuInfoByAge(15);
stuInfos.forEach(System.out::println);
}
@Test
public 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");
// bind
List<StuInfo> stuInfos = mapper.getStuInfoByName4("jack");
stuInfos.forEach(System.out::println);
}
@Test
public 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);
}
@Test
public void test4() {
StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);
StuInfo stu = new StuInfo();
stu.setStuId(1);
stu.setStuBirth(new Date());
mapper.updateStuInfoById(stu);
}
@Test
public void test5() {
StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);
List<StuInfo> stuInfos = mapper.getStuInfoByChoseWhen(null);
stuInfos.forEach(System.out::println);
}
// foreach
@Test
public 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
@Test
public 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);
}
}