mybatis模糊查詢以及結(jié)果封裝詳解
創(chuàng)建maven項目:項目結(jié)構(gòu)如圖所示
準備數(shù)據(jù)庫表:
準備pom.xml所需的依賴:
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.etime</groupId>
<artifactId>day09</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 添加lombok依賴工具使用其中的注解方法可以讓實體類中get,set,以及實體類中操作少一些-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<!-- 添加和引入mybatis的版本號等依賴-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 這里添加mysql依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
<!-- 如果不需要也可不進行單元測試的依賴引入-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
編寫核心配置文件加載所需要的資源
編寫config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置 mybatis的環(huán)境-->
<environments default="development">
<!-- 配置環(huán)境-->
<environment id="development">
<!-- 配置事物類型-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置連接數(shù)據(jù)庫的信息:用的是數(shù)據(jù)源[連接池]-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!-- jdbc:mysql://localhost:3306/db_school?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC-->
<!-- 和javaWeb servlet三層架構(gòu)中的區(qū)別這里是只需要設置時區(qū)就可以了-->
<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>
<!-- 注冊StudentDao接口映射文件位置-->
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
創(chuàng)建工廠連接數(shù)據(jù)處理工具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();
}
//用于讀取配置文件內(nèi)容,生成SqlSessionFactory
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
//獲取SqlSession對象
sqlSession = sqlSessionFactory.openSession();
}
public SqlSession getSqlSession(){
return sqlSession;
}
}
創(chuàng)建學生實體類對象Student.java
package com.etime.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//添加無參構(gòu)造函數(shù)
@NoArgsConstructor
//添加全參數(shù)構(gòu)造函數(shù)
@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;
}
模糊查詢在開發(fā)中是一項不可缺少的重要內(nèi)容。對于mybatis實現(xiàn)模糊查詢有三種方式:
1、添加模糊查詢的接口方法:getStudentSname;
//模糊查詢根據(jù)學生名子姓或者名查詢所有學生信息
List<Student> getStudentBySname1(String sname);
List<Student> getStudentBySname2(String sname);
List<Student> 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函數(shù)式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="%高%";
List<Student> list = studentDao.getStudentBySname1(sname);
System.out.println(list);
sqlSession.close();
}
@Test
public void t10(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
Map<String,Object> map = new HashMap<>();
List<Student> list = studentDao.getStudentBySname2("高");
System.out.println(list);
sqlSession.close();
}
@Test
public void t11(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
List<Student> list = studentDao.getStudentBySname3("高");
System.out.println(list);
sqlSession.close();
}
配置占位符方式#{}:
配置拼接字符串方式${}
配置mysql函數(shù)式concat
三次測試結(jié)果一樣,說明三種方式都是ok的
mybatis結(jié)果封裝
resultMap標簽介紹
resultMap 標簽可以建立查詢的列名和實體類的屬性名稱不一致時建立對應關系。從而實現(xiàn)封裝。在 select 標簽中使用 resultMap 屬性指定引用即可。同時 resultMap 可以實現(xiàn)將查詢結(jié)果映射為復雜類型的 pojo,比如在查詢結(jié)果映射對象中包括 pojo 和 list 實現(xiàn)一對一查詢和一對多查詢。
resultMap的使用
在接口中定義方法:
List<Student> 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中編寫測試方法文章來源:http://www.zghlxwxcb.cn/news/detail-421816.html
@Test
public void t013(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
//獲取StudentDao對象
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
//調(diào)用方法獲取結(jié)果
List<Student> list = studentDao.getAllStudentsInfo();
for (Student student:list
) {
System.out.println(student);
}
sqlSession.close();
}
運行結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-421816.html
到了這里,關于mybatis模糊查詢以及結(jié)果封裝詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!