2018.4.8
仅为个人理解 不足之处欢迎指正~
数据库说明:
我们在之前的数据库中添加三列:sex major class
现有如下记录:
Mybatis进行模糊查询:
Mybatis进行模糊查询常用的方法有三种 依次介绍:
1.使用concat(str1,str2)函数将两个参数连接
在Student.xml中添加新的select方法:
在测试类中增加对于模糊查询的测试:
我们在这里通过第一个模糊查询的方式查找所有专业中带有“技术”的学生 并输出他们的姓名
根据之前的数据库信息
我们应该输出的是 林俊杰 张学友 田馥甄
运行结果与预期相同
下面解释一下xml中 concat中的#{0}:
首先我们要了解一下mysql中模糊查询的语句
1.%
%可以表示任意0个或多个字符,适用于查询数据表中任意带有所需要查询的字段的信息
比如:
select * from student where name like '%王%'
可以查询出王X 王XX X王X XX王 等等任意名字带有“王”且长度不限的同学
2._
_表示任意单个字符 它只能匹配任意单个字符 适用于需要限制长度的查询
比如:
select * from student where name like '_王_'
只能查询出 X王X 这一类同学
select * from student where name like '王_'
只能查询出 王X 这一类同学
3.[]
[]用于指定一个范围 要求所查询的对象为它们中的任意一个
比如:
select * from student where name like '[赵钱孙]小明'
将查询出名字为 赵小明 钱小明 孙小明 的同学 而不是赵钱孙小明
4.[^ ]
[^ ]与上面相反 比如:
select * from student where name like '[^赵钱孙]小明'
将查询出姓不为 赵钱孙 的其他小明
看到这里
语句中的#{0}作用就很明显了
它起到占位符的作用 类似于先把#{}变成preparedstatement中的?
但是这里用#{1}、#{2}甚至#{100}代替也可以成功起到作用
具体的原理和正确的表示方法我也没有理解
2.使用%拼接字符串
在Student.xml中添加新的select方法:
测试方法与上一方法中的相同 同样可以得到正确答案
需要注意的是:
使用%拼接字符串时应写成 "%"#{XXXX}"%"
而不是写为"%#{XXXX}%"
即"%" "%"与#{}应该相互独立 其中"%"也可以根据需求用上文中的其他三种符号代替
3.使用Mybatis提供的bind标签
先在student的pojo里添加major属性
在Student.xml中添加新的select方法:
注意测试方法也将发生改变:
获得结果
下面贴出完整代码:
Student.xml:
<?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 通常设置成pojo包名
在后续调用sql语句的时候 会使用到 -->
<mapper namespace="com.tzy.pojo">
<!-- 这条语句中 用id:listStudent进行标示 供后续代码调用
resultType表示的返回一个关联Student类的数据
在这里本来应该使用的是com.tzy.pojo.Student
但是在之前的mybatis-configuration.xml中定义了
typeAliases别名 所以可以直接使用Student-->
<select id="listStudent" resultType="Student">
select * from student
</select>
<insert id="addStudent" parameterType="Student" >
insert into student values (#{id},#{name})
</insert>
<delete id="deleteStudent" parameterType="Student" >
delete from student where id= #{id}
</delete>
<select id="getStudent" parameterType="_int" resultType="Student">
select * from student where id= #{id}
</select>
<select id="mohuchaxun1" parameterType="string" resultType="Student">
select * from student where major like concat('%',#{0},'%')
</select>
<select id="mohuchaxun2" parameterType="string" resultType="Student">
select* from student where major like "%"#{name}"%"
</select>
<select id="mohuchaxun3" parameterType="Student" resultType="Student">
<bind name="pattern1" value="'%'+major+'%'"/>
select* from student where major like #{pattern1}
</select>
<update id="updateStudent" parameterType="Student" >
update student set name=#{name} where id=#{id}
</update>
</mapper>
TestMybatis.java:
package com.tzy.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 com.tzy.pojo.Student;
public class TestMybatis {
public static void main(String[] args) throws IOException {
//mybatis配置文件
String resource = "mybatis-configuration.xml";
//得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂 传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();
//增
// Student stu=new Student();
// stu.setId(2018040701);
// stu.setName("李宗盛");
// session.insert("addStudent", stu);
//删
// Student stu=new Student();
// stu.setId(2018040701);
// session.delete("deleteStudent", stu);
//改
// Student stu=new Student();
// stu.setId(2018040501);
// stu.setName("林俊杰");
// session.delete("updateStudent", stu);
//单个查询
// Student stu=session.selectOne("getStudent",2018040501);
// System.out.println(stu.getId()+" "+stu.getName());
//多个查询
// List<Student> cs=session.selectList("listStudent");
// // 冒号是遍历的作用
// for (Student a : cs) {
// System.out.println(a.getName()+" "+a.getId());
// }
//模糊查询1
// List<Student> cs=session.selectList("mohuchaxun1","技术");
// for (Student s : cs) {
// System.out.println(s.getName());
//模糊查询2
// List<Student> cs=session.selectList("mohuchaxun2","技术");
// for (Student s : cs) {
// System.out.println(s.getName());
//模糊查询3
Student stu=new Student();
stu.setMajor("技术");
List<Student> cs=session.selectList("mohuchaxun3",stu);
for (Student s : cs) {
System.out.println(s.getName());
}
}
}