mybatis模糊查询以及结果封装详解
创建maven项目:项目结构如图所示
准备数据库表:
准备pom.xml所需的依赖:
project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
modelVersion>4.0.0modelVersion>
groupId>com.etimegroupId>
artifactId>day09artifactId>
version>1.0-SNAPSHOTversion>
properties>
maven.compiler.source>8maven.compiler.source>
maven.compiler.target>8maven.compiler.target>
properties>
dependencies>
dependency>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
version>1.18.16version>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatisartifactId>
version>3.5.1version>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
version>8.0.11version>
scope>runtimescope>
dependency>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
version>4.12version>
scope>testscope>
dependency>
dependencies>
project>
编写核心配置文件加载所需要的资源
编写config.xml文件
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
configuration>
environments default="development">
environment id="development">
transactionManager type="JDBC">transactionManager>
dataSource type="POOLED">
property name="driver" value="com.mysql.cj.jdbc.Driver"/>
property name="url" value="jdbc:mysql://localhost:3306/db_school?serverTimezone=UTC"/>
property name="username" value="root"/>
property name="password" value="h123456"/>
dataSource>
environment>
environments>
mappers>
mapper resource="mapper/StudentMapper.xml"/>
mappers>
configuration>
创建工厂连接数据处理工具SqlSessionUtil.java
SqlSessionUtil.java
package com.etime.util;
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 java.io.IOException;
import java.io.InputStream;
public class SqlSessionUtil {
private static SqlSession sqlSession =null;
static {
//加载配置文件
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream("config.xml");
} catch (IOException e) {
e.printStackTrace();
}
//用于读取配置文件内容,生成SqlSessionFactory
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
//获取SqlSession对象
sqlSession = sqlSessionFactory.openSession();
}
public SqlSession getSqlSession(){
return sqlSession;
}
}
创建学生实体类对象Student.java
package com.etime.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//添加无参构造函数
@NoArgsConstructor
//添加全参数构造函数
@AllArgsConstructor
//添加所有需要的get,set等方法
@Data
public class Student {
private int sid;
private String sname;
private String sgender;
private int sage;
private String semail;
private String sphoto;
}
模糊查询在开发中是一项不可缺少的重要内容。对于mybatis实现模糊查询有三种方式:
1、添加模糊查询的接口方法:getStudentSname;
//模糊查询根据学生名子姓或者名查询所有学生信息
ListStudent> getStudentBySname1(String sname);
ListStudent> getStudentBySname2(String sname);
ListStudent> getStudentBySname3(String sname);
2、配置接口方法对应的sql文件
1、配置占位符方式#{}
select id="getStudentBySname1" parameterType="String" resultType="com.etime.pojo.Student">
select * from student where sname like #{sname}
select>
2、配置拼接字符串方式${}
select id="getStudentBySname2" parameterType="String" resultType="com.etime.pojo.Student">
select * from student where sname like '%${value}%'
select>
我们在上面将原来的#{}占位符,改成了
v
a
l
u
e
。注意如果用模糊查询的这种写法,那么
{value}。注意如果用模糊查询的这种写法,那么
value。注意如果用模糊查询的这种写法,那么{value}的写法就是固定的,不能写成其它名字。
3、配置mysql函数式concat
select id="getStudentBySname3" parameterType="String" resultType="com.etime.pojo.Student">
select * from student where sname like concat('%',#{sname},'%')
select>
测试:
@Test
public void t09(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
String sname="%高%";
ListStudent> list = studentDao.getStudentBySname1(sname);
System.out.println(list);
sqlSession.close();
}
@Test
public void t10(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
MapString,Object> map = new HashMap>();
ListStudent> list = studentDao.getStudentBySname2("高");
System.out.println(list);
sqlSession.close();
}
@Test
public void t11(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
ListStudent> list = studentDao.getStudentBySname3("高");
System.out.println(list);
sqlSession.close();
}
配置占位符方式#{}:
配置拼接字符串方式${}
配置mysql函数式concat
三次测试结果一样,说明三种方式都是ok的
mybatis结果封装
resultMap标签介绍
resultMap 标签可以建立查询的列名和实体类的属性名称不一致时建立对应关系。从而实现封装。在 select 标签中使用 resultMap 属性指定引用即可。同时 resultMap 可以实现将查询结果映射为复杂类型的 pojo,比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询。
resultMap的使用
在接口中定义方法:
ListStudent> getAllStudentsInfo();
在StudentMapper.xml中用resultMap编写
resultMap id="stuMap" type="com.etime.pojo.Student">
id property="sid" column="sid">id>
result property="sname" column="sname">result>
result property="sgender" column="sgender">result>
result property="sage" column="sage">result>
result property="semail" column="semail">result>
result property="sphoto" column="sphoto">result>
resultMap>
测试类MybatisTest.java中编写测试方法
@Test
public void t013(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
//获取StudentDao对象
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
//调用方法获取结果
ListStudent> list = studentDao.getAllStudentsInfo();
for (Student student:list
) {
System.out.println(student);
}
sqlSession.close();
}
运行结果:
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net