講義:
一、動態代理:
1、動態代理存在的意義:
在三層架構中,業務邏輯層要通過介面訪問資料訪問層的功能,動態代理可以實作,
2、動態代理的實作規范:
1)UsersMapper.xml 檔案與 UsersMapper.java 介面必須在同一個目錄下,
2)UsersMapper.xml 檔案與 UsersMapper.java 的介面的檔案名必須一致,后綴不管,
3)UsersMapper.xml 檔案中標簽的 id 值與 UsersMapper.java 的介面中方法的名稱完全一致,
4)UsersMapper.xml 檔案中標簽的 parameterType 屬性值與 UsersMapper.java 的介面中方法的引數型別完全一致,
5)UsersMapper.xml 檔案中標簽的 resultType 值與 UsersMapper.java 的介面中方法的回傳值型別完全一致,
6)UsersMapper.xml 檔案中 namespace 屬性必須是介面的完全限定名稱 com.baidu.mapper.UsersMapper(例子),
7)SqlMapConfig.xml 檔案中注冊 mapper 檔案時,使用 class 屬性等于介面的完全限定名稱 com.baiud.mapper.UsersMapper(例子),或者直接批量注冊 mapper.xml檔案,
3、動態代理訪問的步驟(舉栗子):
1)建表 Users
2)新建 maven工程,重繪資料庫可視化
3)修改目錄
4)修改pom.xml檔案,添加依賴
5)添加jdbc.propertis檔案到resources目錄下
6)添加SqlMapConfig.xml檔案
7)添加物體類
8)新建mapper檔案夾,在其下新建UsersMapper介面
9)在mapper檔案夾下,新建UsersMapper.xml檔案,完成增刪改查功能
10)添加測驗類,測驗功能
二、占位符:
1、#{}占位符:
1)傳參大部分使用 #{} ,它的底層使用的是 PreparedStatement 物件,是安全的資料庫訪問 ,可以防止sql注入,
2)#{}里如何寫,看parameterType引數的型別:
- 如果 parameterType 的型別是簡單型別(8種基本【封裝】+ String),則#{}里隨便寫,
- 如果 parameterType 的型別是物體類的型別,則#{}里只能是類中成員變數的名稱,而且區分大小寫.,
2、${}字串:
1)${}字串可以用于字串拼接,一般用于模糊查詢中(因為有sql注入的風險,很少用),
2)${}里如何寫,也分兩種情況,同樣看 parameterType 的型別:
- 如果 parameterType 的型別是簡單型別,則${}里隨便寫,但是分版本,如果是 3.5.1 及以下的版本,只以寫value,
- 如果 parameterType 的型別是物體類的型別,則${}里只能是類中成員變數的名稱(很少用),
3)字串替換:
- 需求:根據用戶名或者地址查詢 user 資訊:
預想執行的sql陳述句1或者陳述句2:
select * from users where username like '%小%';
select * from users where address like '%市%';
UsersMapper 介面中的方法(通過@Param 可以使UsersMapper.xml 檔案取出對應的 columName 和 columnValue):
//模糊用戶名和地址查詢
List<User> selectByNameOrAddress(
@Param("columnName")
String columnName,
@Param("columnValue")
String columnValue);
UsersMapper.xml 中對應的select:
<!--如果引數超過一個,則parameterType不寫-->
<select id="selectByNameOrAddress" resultType="user">
select id,username,birthday,sex,address
from users
where ${columnName} like concat('%',#{columnValue},'%')
</select>
這樣就可以呼叫一個方法,通過輸入不同的欄位名稱,來進行不同的欄位模糊查詢,
@Test
public void testSelectByNameOrAddress() {
List<User> userList1 = usersMapper.selectByNameOrAddress(
"username","小");
List<User> userList2 = usersMapper.selectByNameOrAddress(
"address","上");
userList1.forEach(user -> System.out.println(user));
userList2.forEach(user -> System.out.println(user));
}
三、執行增刪改陳述句后回傳主鍵值:
- 在插入陳述句結束后,回傳自增的主鍵值到入參的user象的id屬性中(之后就可以通過#{id}來獲取屬性id了),
1、UsersMapper.xml 檔案中的栗子:
<insert id="insertPro" parameterType="user">
<selectKey keyProperty="id" resultType="int" order="AFTER">
select last_insert_id()
</selectKey>
insert into users (username,birthday,sex,address) values(#{userName},#{birthday},#{sex},#{address})
</insert>
2、<selectKey>標簽中的引數詳解:
keyProperty:user 物件的哪個屬性來接回傳的主鍵值,
resultType:回傳的主鍵的型別,
order:在插入陳述句執行前(BEFORE),還是執行后(AFTER)回傳主鍵的值,
四、UUID:
- 這是一個全球唯一隨機字串,由36個字母數字中劃線組成,Java 中有UUID類,mysql 中也有相應的函式,
java 栗子:
@Test
public void testUUID() {
UUID uuid = UUID.randomUUID();
//36位帶橫線
System.out.println(uuid);
//32位不帶橫線
System.out.println(uuid.toString().replace("-",""));
//12位不帶橫線
System.out.println(uuid.toString().replace("-","").substring(20));
}
sql 栗子:
select UUID();
一堆栗子:
1、module 目錄結構:

2、建表陳述句:
use ssm;
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`userName` varchar(255) COLLATE utf8_bin NOT NULL,
`password` varchar(255) COLLATE utf8_bin NOT NULL,
`userIdentity` tinyint(1) NOT NULL,
`balance` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin
3、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_002_users</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>
4、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
5、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="https://www.cnblogs.com/Burning-youth/archive/2022/05/08/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="https://www.cnblogs.com/Burning-youth/archive/2022/05/08/${jdbc.driverClassName}"/>
<property name="url" value="https://www.cnblogs.com/Burning-youth/archive/2022/05/08/${jdbc.url}"/>
<property name="username" value="https://www.cnblogs.com/Burning-youth/archive/2022/05/08/${jdbc.username}"/>
<property name="password" value="https://www.cnblogs.com/Burning-youth/archive/2022/05/08/${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--注冊mapper.xml檔案-->
<mappers>
<!--單個注冊-->
<!--<mapper ></mapper>-->
<!--批量注冊-->
<package name="org.burning.mapper"/>
</mappers>
</configuration>
6、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 + '\'' +
'}';
}
}
7、UsersMapper.java:
package org.burning.mapper;
import org.apache.ibatis.annotations.Param;
import org.burning.entity.User;
import java.util.List;
/**
* 資料訪問層的介面,規定的資料庫中可進行的各種操作
*/
public interface UsersMapper {
//查詢用戶全部資訊
List<User> getAll();
//根據用戶主鍵查用戶
User selectById(Integer id);
//根據用戶名模糊查詢用戶
List<User> selectByName(String userName);
//用戶的更新
int updateById(User user);
//增加用戶
int insert(User user);
//根據主鍵洗掉用戶
int deleteById(Integer id);
//優化后的模糊查詢
List<User> selectByNamePro(String userName);
//模糊用戶名和地址查詢
List<User> selectByNameOrAddress(
@Param("columnName")
String columnName,
@Param("columnValue")
String columnValue);
//增加用戶,并獲得新增用戶的id到入參中
int insertPro(User user);
}
8、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">
<select id="getAll" resultType="user">
select id,username,birthday,sex,address
from users
</select>
<select id="selectById" parameterType="int" resultType="user">
select id,username,birthday,sex,address
from users
where id=#{id}
</select>
<select id="selectByName" parameterType="string" resultType="user">
select id,username,birthday,sex,address
from users
where username like '%${userName}%'
</select>
<update id="updateById" parameterType="user">
update users set username=#{userName},birthday=#{birthday},sex=#{sex},address=#{address}
where id=#{id}
</update>
<insert id="insert" parameterType="user">
insert into users (username,birthday,sex,address) values(#{userName},#{birthday},#{sex},#{address})
</insert>
<delete id="deleteById" parameterType="int">
delete from users
where id=#{id}
</delete>
<select id="selectByNamePro" parameterType="string" resultType="user">
select id,username,birthday,sex,address
from users
where username like concat('%',#{userName},'%')
</select>
<select id="selectByNameOrAddress" resultType="user">
select id,username,birthday,sex,address
from users
where ${columnName} like concat('%',#{columnValue},'%')
</select>
<insert id="insertPro" parameterType="user">
<selectKey keyProperty="id" resultType="int" order="AFTER">
select last_insert_id()
</selectKey>
insert into users (username,birthday,sex,address) values(#{userName},#{birthday},#{sex},#{address})
</insert>
</mapper>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/470607.html
標籤:其他
