IDEA-MyBatis动态sql关联映射
一、表格创建
二、表格查询
1.findall返回整张表
studentdao.java
public List<Student> findAll();
studentdao.xml
<!--把StudentDao接口里声明的7个方法 一对一地翻译成 SQL,并交给 MyBatis去执行--><select id="findAll" resultMap="demo"> select * from student </select><resultMap id="demo" type="com.qcby.entity.Student">
Teststudent.java
@Testpublic void findAll() {//代理对象调用方法List<Student> students = mapper.findAll();for (Student student : students) {System.out.println(student);}}
2.根据id查找
public Student findById(int id);
<!-- 根据id查找 -->
<select id="findById" resultType="com.qcby.entity.Student" parameterType="java.lang.Integer">
select * from student where id = #{id} </select>
//调用id查找的函数@Testpublic void findById() {Student student = mapper.findById(2);//找id是2的人System.out.println(student);//打印出来的是id=2的所有信息}
3.插入
public int insert(Student student);
<!-- 插入 --><insert id="insert" parameterType="com.qcby.entity.Student">insert into student(name,age,sex) value(#{name},#{age},#{sex}) </insert>
@Testpublic void insert() {Student student = new Student();//插入需要创建一个新对象student.setName("cici");//set设置后面跟姓名年龄性别student.setAge(20);student.setSex("女");int code = mapper.insert(student);session.commit();事物提交,只有加上这句话才能做修改System.out.println(code);}
4.删除
public int delete(int id);
<!-- 删除 --><delete id="delete" parameterType="java.lang.Integer">delete from student where id=#{id} </delete>
//删除@Testpublic void delete() {int code = mapper.delete(3);session.commit();System.out.println(code);}
5.模糊查找
#{} 占位符赋值 参数采用预编译 防止sql注入 ${} 采用sql拼接 无法防止sql注入#{} 参数是简单类型(基础类型和字符串) 变量可以任意 ${} 参数是简单类型(基础类型和字符串) 变量只能用value 如果是其他需要用@Param注入、#{}如果是字符串类型的数据会自动加上'' ${}参数是字符串 需要手动加上''如果参数是引用类型,变量是引用类型中的属性,不可任意90%的情况用#{},只有在目标里面是value的时候采用${}(模糊查找)
//根据姓名查找 findByName只按姓名模糊查public List<Student> findByName(String name);
<!-- 模糊查找 -->
<select id="findByName" resultType="com.qcby.entity.Student" parameterType="java.lang.String">
、select * from student where name like '%${value}%'
</select> <!--$只识别value,$里面不能用name-->
@Testpublic void findStudent() {List<Student> students = mapper.findByName("张");for (Student student : students) {System.out.println(student);
6. 动态 SQL 标签
//根据姓名年龄性别查找 动态查询:姓名/年龄/性别哪个字段有值就按哪个查public List<Student> search(Student student);
① <where> 智能前缀 where if
自动处理首个 and / or ,避免 WHERE AND … 语法错。
<!-- where if 针对and和or动态变化 -->select * from student<where><if test="name!=null and name!=''"> name=#{name} </if><if test="age!=null"> and age=#{age} </if><if test="sex!=null"> and sex=#{sex} </if></where></select>
② <set> 智能后缀
自动去掉尾部多余逗号,防止 SET name=?, 语法错
<!-- 修改 set if动态修改,根据传不传参做拼接 --><update id="update" parameterType="com.qcby.entity.Student"><!-- update student set name = #{name},age=#{age},sex=#{sex} where id = #{id} -->update student<set><if test="name!=null"> name = #{name}, </if><if test="age"> age=#{age}, </if><if test="sex"> sex=#{sex} </if></set>where id = #{id}</update>
③ <if> 判断
test 里写 OGNL 表达式,可用 and/or/null/'' 等。
引用类型直接写属性名;简单类型加 @Param 后写参数名。
7.批量操作<foreach>
语法:
xml
<foreach collection="名称" item="元素变量" separator="," open="(" close=")">#{元素变量}
</foreach>
场景 | collection 取值 | item 示例 |
数组 | @Param("ids") Integer[] ids → collection="ids" | #{id} |
List | @Param("students") List<Student> s → collection="students" | #{stu.name} |
collection:当前要循环的数组
item要循环的数组每一个元素
separator元素和元素之间用什么做分割
open当前循环以什么开始
close当前循环以什么结束
①批量删除
//通过数组批量删除public int deleteByArray(@Param("ids") Integer[] ids);
<delete id="deleteByArray">delete from student where id in<foreach collection="ids" item="id" separator="," open="(" close=")"> #{id}
</foreach></delete>
②批量添加
//批量添加public int insertMore(@Param("students") List<Student> students);
<!-- 批量添加 -->
<!-- insert into student(name,age,sex) values("李华",20,"女"),
("李华1",20,"女"),("李华2",20,"女") --><insert id="insertMore">insert into student(name,age,sex) values<foreach collection="students" item="stu" separator=","> (#{stu.name},#{stu.age},#{stu.sex}) </foreach></insert>
8. 常见易错点小结
1. 增删改 不要写 resultType ;查询必须写 resultType/resultMap 。
2. ${} 只能用于非用户输入场景(动态列名、排序);用户输入一律 #{} 。
3. <set> / <where> 自动处理前缀/后缀,但不会补全关键字,别漏 SET / WHERE 。
4. 接口方法与 XML id 必须一字不差,否则绑定失败报 BindingException 。