講義: 動態sql可以定義代碼片斷,可以進行邏輯判斷,可以進行迴圈處理(批量處理),使條件判斷更為簡單。 一、動態sql核心標簽: 1、<sql>:當多種類型的查詢語句的查詢欄位或者查詢條件相同時,可以將其定義為常量,方便調用。 2、<include>:用來引用<sql>定義的代碼片斷。 <!--定 ...
講義:
- 動態sql可以定義代碼片斷,可以進行邏輯判斷,可以進行迴圈處理(批量處理),使條件判斷更為簡單。
一、動態sql核心標簽:
1、<sql>:當多種類型的查詢語句的查詢欄位或者查詢條件相同時,可以將其定義為常量,方便調用。
2、<include>:用來引用<sql>定義的代碼片斷。
<!--定義代碼片斷-->
<sql id="allColumns">
id,username,birthday,sex,address
</sql>
<!--引用定義好的代碼片斷-->
<select id="getAll" resultType="users" >
select <include refid="allColumns"></include>
from users
</select>
3、<if>:進行條件判斷。
test 屬性:if 執行條件(條件判斷的取值可以是實體類的成員變數,可以是map的key,可以是@Param註解的名稱)。
4、<where>:
特性:標簽可以自動的將第一個條件前面的邏輯運算符 (or ,and) 去掉,比如 id 查詢條件前面是有“and”關鍵字的,但是在列印出來的 SQL 中卻沒有。
<select id="getByCondition" parameterType="users" resultType="users">
select <include refid="allColumns"></include>
from users
<where>
<if test="userName != null and userName != ''">
and username like concat('%',#{userName},'%')
</if>
<if test="birthday != null">
and birthday = #{birthday}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
<if test="address != null and address != ''">
and address like concat('%',#{address},'%')
</if>
</where>
</select>
5、<set>:使用見下麵的慄子。切記,至少更新一列(負責拋出異常)。
需求:使用 if+set 標簽進行update操作時,哪個欄位中有值才去更新,如果某項為 null 則不進行更新,而是保持資料庫原值。
慄子:
<update id="updateBySet" parameterType="users">
update users
<set>
<if test="userName != null and userName != ''">
username = #{userName},
</if>
<if test="birthday != null">
birthday = #{birthday},
</if>
<if test="sex != null and sex != ''">
sex = #{sex},
</if>
<if test="address != null and address != ''">
address =#{address} ,
</if>
</set>
where id = #{id}
</update>
6、<foreach>:用來進行迴圈遍歷,完成迴圈條件查詢,批量刪除,批量增加,批量更新。
1)collection 屬性:用來指定入參的類型,如果是List集合,則為list,如果是Map集合,則為map,如果是數組,則為array。
2)item 屬性 :迴圈體中的具體對象。支持屬性的點路徑訪問,如 item.age,item.info.details;在list和數組中是其中的對象,在map中是value。
3)index 屬性 :在list和數組中,index是元素的序號,在map中,index是元素的key,該參數可不寫。
4)separator 屬性:多個值或對象或語句之間的分隔符。
5)open 屬性 :表示該語句以什麼開始。
6)close 屬性 :表示該語句以什麼結束。
註意:要使用批量更新,必須在jdbc.properties屬性文件中的url中添加&allowMultiQueries=true,才允許多行操作。
二、通過指定下標來進行傳參:
可以不使用對象的屬性名進行參數值綁定,使用下標值。 mybatis-3.3 版本和之前的版本使用#{0},#{1}方式, 從 mybatis3.4 開始使用#{arg0},#{arg1}的方式。
三、map在動態sql中的使用:
- 如果入參超過一個以上,使用map封裝查詢條件,更有語義,查詢條件更明確。
1、入參是map:
因為當傳遞的數據有多個,不適合使用指定下標或指定名稱的方式來進行傳參,又加上參數不一定與對象的成員變數一致,考慮使用map集合來進行傳遞,map使用的是鍵值對的方式.當在sql語句中使用的時候#{鍵名},${鍵名},{ }的是鍵的名稱。
2、返回值是map:
返回值是map的適用場景,如果的數據不能使用對象來進行封裝,可能查詢的數據來自多張表中的某些列,這種情況下,使用map,但是map的返回方式破壞了對象的封裝,返回來的數據是一個一個單獨的數據, 彼此之間不相關,map使用表中的列名或別名作為鍵名(key)進行返回數據。
四、列名與類中成員變數名稱不一致:
解決方案一:
使用列的別名,別名與類中的成員變數名一樣,即可完成註入。
解決方案二:
使用<resultMap>標簽進行映射。
property 屬性:為成員變數名
column 屬性:為列的別名
一堆慄子:
一、module 目錄結構:
二、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>org.burning</groupId>
<artifactId>mybatis_003_dynamicsql</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--添加mybatis依賴-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--添加mysql依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<!--指定資源文件位置-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
三、jdbc.properties:
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=888
四、SqlMapConfig.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>
<!--讀取jdbc.properties屬性-->
<properties resource="jdbc.properties"></properties>
<!--設置日誌輸出-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--註冊實體類別名-->
<typeAliases>
<package name="org.burning.entity"/>
</typeAliases>
<!--配置環境變數-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--註冊mapper.xml文件-->
<mappers>
<!--優化mapper.xml文件註冊-->
<!--絕對路徑註冊-->
<!--<mapper url="/////"></mapper>-->
<!--非動態代理方式下的註冊-->
<!--<mapper resource="StudentMapper.xml"></mapper>-->
<!--單個註冊-->
<!--<mapper class="org.burning.mapper.UsersMapper"></mapper>-->
<!--批量註冊-->
<package name="org.burning.mapper"/>
</mappers>
</configuration>
五、建表語句:
CREATE TABLE `student` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`email` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`age` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin
CREATE TABLE `books` (
`book_id` int NOT NULL AUTO_INCREMENT,
`book_name` varchar(45) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin
六、User.java:
package org.burning.entity;
import java.util.Date;
public class User {
private Integer id;
private String userName;
private Date birthday;
private String sex;
private String address;
public User() {
}
public User(Integer id, String userName, Date birthday, String sex, String address) {
this.id = id;
this.userName = userName;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
public User(String userName, Date birthday, String sex, String address) {
this.userName = userName;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
七、Book.java:
package org.burning.entity;
public class Book {
private Integer id;
private String name;
public Book() {
}
public Book(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
八、UsersMapper.java:
package org.burning.mapper;
import org.apache.ibatis.annotations.Param;
import org.burning.entity.User;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 數據訪問層的介面,規定的資料庫中可進行的各種操作
*/
public interface UsersMapper {
//查詢用戶全部信息
List<User> getAll();
//按指定的條件進行多條件查詢
List<User> selectByCondition(User user);
//有選擇的更新
int updateBySet(User user);
//查詢多個指定id的用戶信息
List<User> selectByIds(Integer[] arr);
//批量刪除
int deleteBatch(Integer[] arr);
//批量增加
int insertBatch(List<User> users);
//查詢生日在兩個日期間的所有學生信息
List<User> selectByTwoBirthday(Date begin,Date end);
//入參是map
List<User> selectByMap(Map map);
//返回值是一行的map
Map returnMap(Integer id);
//返回多行的map
List<Map> returnMaps();
}
九、UsersMapper.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="org.burning.mapper.UsersMapper">
<!--定義代碼片段-->
<sql id="allcolumns">
id,username,birthday,sex,address
</sql>
<!--查詢users中所有的學生信息-->
<select id="getAll" resultType="user">
select <include refid="allcolumns"></include>
from users
</select>
<!--動態sql實現:
根據多個欄位進行查詢操作(可以通過判斷user對象的實例變數是否“有意義”而進行sql的拼接)
【無意義值指的是:比如userName是字元串類型,而它為null或者為空字元串,那它就是無意義的】
-->
<select id="selectByCondition" parameterType="User" resultType="User">
select <include refid="allcolumns"></include>
from users
<where>
<if test="userName != null and userName != ''">
and username like concat('%',#{userName},'%')
</if>
<if test="birthday != null">
and birthday = #{birthday}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
<if test="address != null and address != ''">
and address like concat('%',#{address},'%')
</if>
</where>
</select>
<!--通過動態sql實現:
根據入參user對象的實例變數是否有“意義”,而進行相應的更新處理
-->
<update id="updateBySet" parameterType="user">
update users
<set>
<if test="userName != null and userName != ''">
userName = #{userName},
</if>
<if test="birthday != null">
birthday = #{birthday},
</if>
<if test="sex != null and sex != ''">
sex = #{sex},
</if>
<if test="address != null and address != ''">
address = #{address},
</if>
</set>
where id=#{id}
</update>
<!--通過動態sql實現:(當入參是一個以上的時候,不需要寫parameterType)
根據入參的id數組,來進行相應id多條信息查詢
-->
<select id="selectByIds" resultType="user">
select <include refid="allcolumns"></include>
from users
where id in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
<!--通過動態sql實現:
批量刪除
-->
<delete id="deleteBatch">
delete from users
where id in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<!--通過動態sql實現:
批量增加
-->
<insert id="insertBatch">
insert into users (username,birthday,sex,address)
values
<foreach collection="list" item="u" separator=",">
(#{u.userName},#{u.birthday},#{u.sex},#{u.address})
</foreach>
</insert>
<!--通過指定參數位置,來獲取入參值的慄子:
-->
<select id="selectByTwoBirthday" resultType="user">
select <include refid="allcolumns"></include>
from users
where birthday between #{arg0} and #{arg1}
</select>
<!--通過入參為Map類型,來進行多個數據的傳遞
-->
<select id="selectByMap" resultType="user">
select <include refid="allcolumns"></include>
from users
where birthday between #{birthdayBegin} and #{birthdayEnd}
</select>
<!--將查出來的數據封進Map里,欄位名(可以使用別名)就是key,列值就是value
-->
<select id="returnMap" parameterType="int" resultType="map">
select id,username as name,address
from users
where id=#{id}
</select>
<!--將查出來的數據,封進Map里,同為一行的數據為一個Map
最後返回一個裝著很多Map的List集合
-->
<select id="returnMaps" resultType="map">
select username as name,address
from users
</select>
</mapper>
十、UserTest.java:
package org.burning;
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.burning.entity.Book;
import org.burning.entity.User;
import org.burning.mapper.UsersMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class UsersTest {
SqlSession sqlSession;
//動態代理對象
UsersMapper usersMapper;
//日期的格式化刷子
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Before
public void openSqlSession() throws IOException {
//讀取核心配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//創建工廠對象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//取出sqlSession
sqlSession = factory.openSession();
//取出動態代理對象,完成介面方法的調用,實則是調用xml文件中相應的標簽的功能
usersMapper = sqlSession.getMapper(UsersMapper.class);
}
@After
public void closeSqlsession() {
sqlSession.close();
}
@Test
public void testGetAll() {
List<User> users = usersMapper.getAll();
users.forEach(user -> System.out.println(user));
}
@Test
public void testSelectByCondition() throws ParseException {
User u = new User();
u.setSex("1");
u.setUserName("小");
u.setAddress("河");
u.setBirthday(sdf.parse("1999-02-22"));
List<User> users = usersMapper.selectByCondition(u);
users.forEach(user -> System.out.println(user));
}
@Test
public void testUpdateBySet() throws ParseException {
User u = new User();
u.setId(3);
u.setUserName("小明的新名字");
u.setBirthday(sdf.parse("1999-02-22"));
int num = usersMapper.updateBySet(u);
System.out.println(num);
sqlSession.commit();
}
@Test
public void testSelectByIds() {
Integer[] array = {1,4,5};
List<User> users = usersMapper.selectByIds(array);
users.forEach(user -> System.out.println(user));
}
@Test
public void testDeleteBatch() {
Integer[] array = {11,12,13};
int num = usersMapper.deleteBatch(array);
System.out.println(num);
sqlSession.commit();
}
@Test
public void testBatch() throws ParseException {
User user1 = new User("王1",sdf.parse("2020-01-01"),"2","大錘島分島A");
User user2 = new User("王2",sdf.parse("2020-01-02"),"2","大錘島分島B");
User user3 = new User("王3",sdf.parse("2020-01-03"),"2","大錘島分島C");
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
int num = usersMapper.insertBatch(users);
System.out.println(num);
sqlSession.commit();
}
@Test
public void testSelectByTwoBirthday() throws ParseException {
List<User> users = usersMapper.selectByTwoBirthday(
sdf.parse("1900-12-12"),
sdf.parse("3000-01-01")
);
users.forEach(user -> System.out.println(user));
}
@Test
public void testSelectByMap() throws ParseException {
Map userMap = new HashMap();
Date begin = sdf.parse("1900-12-12");
Date end = sdf.parse("3000-01-01");
userMap.put("birthdayBegin",begin);
userMap.put("birthdayEnd",end);
List<User> users = usersMapper.selectByMap(userMap);
users.forEach(user -> System.out.println(user));
}
@Test
public void testReturnMap() {
Map map = usersMapper.returnMap(1);
System.out.println(map);
}
@Test
public void testReturnMaps() {
List<Map> mapList = usersMapper.returnMaps();
mapList.forEach(map -> System.out.println(map));
}
}
十一、BooksMapper.java:
package org.burning.mapper;
import org.burning.entity.Book;
import java.util.List;
public interface BooksMapper {
//查詢全部圖書(別名)
List<Book> selectBooks();
//查詢全部圖書(resultMap)
List<Book> selectBooksPro();
}
十二、BooksMapper.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="org.burning.mapper.BooksMapper">
<!--使用resultMap手工完成映射-->
<resultMap id="bookMap" type="book">
<!--主鍵綁定-->
<id property="id" column="book_id"></id>
<!--非主鍵綁定-->
<result property="name" column="book_name"></result>
</resultMap>
<!--通過起別名的方案解決欄位名和成員變數名不一致的問題-->
<select id="selectBooks" resultType="book">
select book_id id,book_name name
from books
</select>
<!--通過resultMap方案解決欄位名和成員變數名不一致的問題-->
<select id="selectBooksPro" resultMap="bookMap">
select book_id,book_name
from books
</select>
</mapper>
十三、BookTest.java:
package org.burning;
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.burning.entity.Book;
import org.burning.mapper.BooksMapper;
import org.burning.mapper.UsersMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.security.spec.PSSParameterSpec;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class BooksTest {
SqlSession sqlSession;
//動態代理對象
BooksMapper booksMapper;
//日期的格式化刷子
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Before
public void openSqlSession() throws IOException {
//讀取核心配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//創建工廠對象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//取出sqlSession
sqlSession = factory.openSession();
//取出動態代理對象,完成介面方法的調用,實則是調用xml文件中相應的標簽的功能
booksMapper = sqlSession.getMapper(BooksMapper.class);
}
@After
public void closeSqlsession() {
sqlSession.close();
}
@Test
public void testSelectBooks(){
List<Book> bookList = booksMapper.selectBooks();
bookList.forEach(book -> System.out.println(book));
}
@Test
public void testSelectBooksPro(){
List<Book> bookList = booksMapper.selectBooksPro();
bookList.forEach(book -> System.out.println(book));
}
}