1. 程式人生 > 實用技巧 >【狂神說Java】Spring5筆記存檔

【狂神說Java】Spring5筆記存檔

視訊:https://www.bilibili.com/video/BV1WE411d7Dv


1、Spring

1.1、簡介


spring官網: https://spring.io/projects/spring-framework#overview

官方下載: https://repo.spring.io/release/org/springframework/spring/

GitHub: https://github.com/spring-projects/spring-framework

Spring Web MVC: spring-webmvc最新版

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>

1.2、優點

  • Spring是一個開源的免費框架(容器)!
  • Spring是一個輕量級的非入侵式的框架
  • 控制反轉(IOC),面向切面程式設計(AOP)!
  • 支援事務的處理,對框架整合的支援

開源免費容器,輕量級非侵入式,控制反轉,面向切面,支援事務,支援框架整合

Spring是一個輕量級的控制反轉(IOC)和麵向切面(AOP)程式設計的框架

1.3、組成

1.4、擴充套件

現代化的java開發 -> 基於Spring的開發


2、IOC理論推導

傳統的呼叫

  1. UserDao

    package dao;
    public interface UserDao {
    	void getUser();
    }
    
  2. UserDaoImp

    package dao;
    public class UserDaoImpl implements UserDao{
    	public void getUser() {
    		System.out.println("預設獲取使用者資料");	
    	}
    }
    
  3. UserSevice

    package Service;
    public interface UserService {
    	void getUser();
    }
    
  4. UserServiceImp

    package Service;
    import dao.UserDao;
    import dao.UserDaoImpl;
    
    public class UserServiceImpl implements UserService{
    		UserDao userDao = new UserDaoImpl();		
    		public void getUser(){
    			userDao.getUser();
    		}	
    }
    

測試

package holle0;
import Service.UserService;
import Service.UserServiceImpl;

public class MyTest0 {
	public static void main(String[] args) {
		// 使用者實際呼叫的是業務層,dao層他們不需要接觸
		UserService userService = new UserServiceImpl();
		userService.getUser();
	}
}

在我們之前的業務中,使用者的需求可能會影響我們原來的程式碼,我們需要根據使用者的需求去修改原始碼!如果程式程式碼量十分大,修改一次的成本代價十分昂貴!


改良:我們使用一個Set介面實現。已經發生了革命性的變化!

//在Service層的實現類(UserServiceImpl)增加一個Set()方法
//利用set動態實現值的注入!
private UserDao userDao;
public void setUserDao(UserDao userDao){
    this.userDao = userDao;
}

set() 方法實際上是動態改變了 UserDao userDao 的 初始化部分(new UserDaoImpl()

測試中加上

((UserServiceImpl)userService).setUserDao(new UserDaoImpl());
  • 之前,程式是主動建立物件!控制權在程式猿手上
  • 使用了set注入後,程式不再具有主動性,而是變成了被動的接受物件!(主動權在客戶手上

本質上解決了問題,程式設計師不用再去管理物件的建立

系統的耦合性大大降低,可以更專注在業務的實現上

這是IOC(控制反轉)的原型,反轉(理解):主動權交給了使用者

IOC本質




3、HolleSpring

在父模組中匯入jar包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>5.2.7.RELEASE</version>
</dependency>

pojo的Hello.java

package pojo;

public class Hello {

	private String str;
	
	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
	
	@Override
	public String toString() {
		return "Holle [str=" + str + "]";
	}
}

在resource裡面的xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--在Spring中建立物件,在Spring這些都稱為bean
    	型別 變數名 = new 型別();
    	Holle holle = new Holle();
    	
    	bean = 物件(holle)
    	id = 變數名(holle)
    	class = new的物件(new Holle();)
    	property 相當於給物件中的屬性設值,讓str="Spring"
    -->
    
    <bean id="hello" class="pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>
</beans>

測試類MyTest

package holle1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import pojo.Hello;

public class MyTest {

	public static void main(String[] args) {
		//獲取Spring的上下文物件
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		//我們的物件下能在都在spring·中管理了,我們要使用,直接取出來就可以了
		Hello holle = (Hello) context.getBean("hello");
		System.out.println(holle.toString());
	}

}

核心用set注入,所以必須要有下面的se()方法

//Hello類
public void setStr(String str) {
		this.str = str;
}

思考:


IOC:物件由Spring 來建立,管理,裝配!

彈幕評論裡面的理解:

原來這套程式是:你寫好選單買好菜,客人來了自己把菜炒好招待,就相當於你請人吃飯
現在這套程式是:你告訴樓下餐廳,你要哪些菜,客人來的時候,餐廳把做好的你需要的菜送上來
IoC:炒菜這件事,不再由你自己來做,而是委託給了第三方__餐廳來做

此時的區別就是,如果我還需要做其他的菜,我不需要自己搞菜譜買材料再做好,而是告訴餐廳,我要什麼菜,什麼時候要,你做好送來

.

在前面第一個module試試引入Spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="userDaomSql" class="dao.UserDaoMysqlImpl"></bean>

    <bean id="userServiceImpl" class="service.UserServiceImp">
        <!--ref引用spring中已經建立很好的物件-->
        <!--value是一個具體的值,基本資料型別-->
        <property name="userDao" ref="userDaomSql"/>
    </bean>

</beans>

第一個module改良後測試

package holle0;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserServiceImpl;

public class MyTest0 {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
		userServiceImpl.getUser();
	}
}

總結:

所有的類都要裝配的beans.xml 裡面;

所有的bean 都要通過容器去取;

容器裡面取得的bean,拿出來就是一個物件,用物件呼叫方法即可;

4、IOC建立物件的方式

  1. 使用無參構造建立物件,預設。
  2. 使用有參構造(如下)

下標賦值

index指的是有參構造中引數的下標,下標從0開始;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="pojo.User">
        <constructor-arg index="0" value="chen"/>
    </bean>
</beans>

型別賦值(不建議使用)

<bean id="user" class="pojo.User">
    <constructor-arg type="java.lang.String" value="kuang"/>
</bean>

直接通過引數名(掌握)

<bean id="user" class="pojo.User">
    <constructor-arg name="name" value="kuang"></constructor-arg>
</bean>
<!-- 比如引數名是name,則有name="具體值" -->

註冊bean之後就物件的初始化了(類似 new 類名()

彈幕評論:

name方式還需要無參構造和set方法,index和type只需要有參構造

就算是new 兩個物件,也是隻有一個例項(單例模式:全域性唯一

User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
system.out.println(user == user2)//結果為true

總結:在配置檔案載入的時候,容器(< bean>)中管理的物件就已經初始化了

5、Spring配置

5.1、別名

<bean id="user" class="pojo.User">
    <constructor-arg name="name" value="chen"></constructor-arg>
</bean>

<alias name="user" alias="userLove"/>
<!-- 使用時
	User user2 = (User) context.getBean("userLove");	
-->

5.2、Bean的配置

<!--id:bean的唯一識別符號,也就是相當於我們學的物件名
class:bean物件所對應的會限定名:包名+型別
name:也是別名,而且name可以同時取多個別名 -->
<bean id="user" class="pojo.User" name="u1 u2,u3;u4">
    <property name="name" value="chen"/>
</bean>
<!-- 使用時
	User user2 = (User) context.getBean("u1");	
-->

5.3、import

import一般用於團隊開發使用,它可以將多個配置檔案,匯入合併為一個

假設,現在專案中有多個人開發,這三個人複製不同的類開發,不同的類需要註冊在不同的bean中,我們可以利
用import將所有人的beans.xml合併為一個總的!

  • 張三(beans.xm1)

  • 李四(beans2.xm1)

  • 王五(beans3.xm1)

  • applicationContext.xml

    <import resource="beans.xm1"/>
    <import resource="beans2.xml"/>
    <import resource="beans3.xm1"/>
    

使用的時候,直接使用總的配置就可以了

彈幕評論:

按照在總的xml中的匯入順序來進行建立,後匯入的會重寫先匯入的,最終例項化的物件會是後匯入xml中的那個

6、依賴注入(DI)

6.1、構造器注入

第4點有提到

6.2、set方式注入【重點】

依賴注入:set注入!

  • 依賴:bean物件的建立依賴於容器
  • 注入:bean物件中的所有屬性,由容器來注入

【環境搭建】

  1. 複雜型別

    Address類

  2. 真實測試物件

    Student類

  3. beans.xml

  4. 測試

    MyTest3

Student類

package pojo;

import java.util.*;
@Get
@Set
public class Student {
//別忘了寫get和set方法(用lombok註解也行)
    private String name;
    private Address address;

    private String[] books;
    private List<String> hobbies;

    private Map<String, String> card;
    private Set<String> game;

    private Properties infor;
    private String wife;

    @Override
    public String toString() {
        return "Student{" +"\n"+
                "name='" + name + '\'' +"\n"+
                ", address=" + address.toString() +"\n"+
                ", books=" + Arrays.toString(books) +"\n"+
                ", hobbies=" + hobbies +"\n"+
                ", card=" + card +"\n"+
                ", game=" + game +"\n"+
                ", infor=" + infor +"\n"+
                ", wife='" + wife + '\'' +"\n"+
                '}';
    }
}

Address類

package pojo;

public class Address {

    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="address" class="pojo.Address">
		<property name="address" value="address你好" />
	</bean>

	<bean id="student" class="pojo.Student">
		<!--第一種,普通值注入 -->
		<property name="name" value="name你好" />
		<!--第二種,ref注入 -->
		<property name="address" ref="address" />

		<!--陣列注入 -->
		<property name="books">
			<array>
				<value>三國</value>
				<value>西遊</value>
				<value>水滸</value>
			</array>
		</property>

		<!--list列表注入 -->
		<property name="hobbies">
			<list>
				<value>唱</value>
				<value>跳</value>
				<value>rap</value>
				<value>籃球</value>
			</list>
		</property>

		<!--map鍵值對注入 -->
		<property name="card">
			<map>
				<entry key="username" value="root" />
				<entry key="password" value="root" />
			</map>
		</property>

		<!--set(可去重)注入 -->
		<property name="game">
			<set>
				<value>wangzhe</value>
				<value>lol</value>
				<value>galname</value>
			</set>
		</property>

		<!--空指標null注入 -->
		<property name="wife">
			<null></null>
		</property>

		<!--properties常量注入 -->
		<property name="infor">
			<props>
				<prop key="id">20200802</prop>
				<prop key="name">cbh</prop>
			</props>
		</property>
	</bean>
</beans>

MyTest3

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Student;

public class MyTest3 {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		Student stu = (Student) context.getBean("student");
		System.out.println(stu.toString());
	}	
}

6.3、拓展注入

官方文件位置

pojo增加User類

package pojo;

public class User {
    private String name;
    private int id;
	public User() {
        
	}
	public User(String name, int id) {
		super();
		this.name = name;
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", id=" + id + "]";
	}
}

注意: beans 裡面加上這下面兩行

使用p和c名稱空間需要匯入xml約束

xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”

?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p名稱空間注入/set注入,可以直接注入屬性的值-》property-->
    <bean id="user" class="pojo.User" p:name="cxk" p:id="20" >
    </bean>

    <!--c名稱空間,通過構造器注入,需要寫入有參和無參構造方法-》construct-args-->
    <bean id="user2" class="pojo.User" c:name="cbh" c:id="22"></bean>
</beans>

測試

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user",User.class);//確定class物件,就不用再強轉了
System.out.println(user.toString());

6.4、Bean作用域

  1. 單例模式(預設)

    <bean id="user2" class="pojo.User" c:name="cxk" c:age="19" scope="singleton"></bean>
    


彈幕評論:單例模式是把物件放在pool中,需要再取出來,使用的都是同一個物件例項

  1. 原型模式: 每次從容器中get的時候,都產生一個新物件!

    <bean id="user2" class="pojo.User" c:name="cxk" c:age="19" scope="prototype"></bean>
    

  1. 其餘的request、session、application這些只能在web開放中使用!

7、Bean的自動裝配

  • 自動裝配是Spring滿足bean依賴的一種方式
  • Spring會在上下文自動尋找,並自動給bean裝配屬性

在Spring中有三種裝配的方式

  1. 在xml中顯示配置

  2. 在java中顯示配置

  3. 隱式的自動裝配bean 【重要】

  4. 環境搭建:一個人有兩個寵物

  5. byType自動裝配:byType會自動查詢,和自己物件set方法引數的型別相同的bean

    保證所有的class唯一(類為全域性唯一)

  6. byName自動裝配:byName會自動查詢,和自己物件set對應的值對應的id

    保證所有id唯一,並且和set注入的值一致

    <!-- 找不到id和多個相同class -->
    <bean id="cat1" class="pojo.Cat"/>
    <bean id="cat2" class="pojo.Cat"/>
    找不到 id=cat,且有兩個Cat
    

7.1測試:自動裝配

pojo的Cat類

public class Cat {
    public void shut(){
        System.out.println("miao");
    }
}

pojo的Dog類

public class Dog {

    public void shut(){
        System.out.println("wow");
    }

}

pojo的People類

package pojo;
public class People {
    
    private Cat cat;
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

xml配置 -> byType 自動裝配

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="pojo.Cat"/>
    <bean id="dog" class="pojo.Dog"/>
    
    <!--byType會在容器自動查詢,和自己物件屬性相同的bean
		例如,Dog dog; 那麼就會查詢pojo的Dog類,再進行自動裝配
	-->
    <bean id="people" class="pojo.People" autowire="byType">
        <property name="name" value="cbh"></property>
    </bean>

</beans>

xml配置 -> byName 自動裝配

<bean id="cat" class="pojo.Cat"/>
<bean id="dog" class="pojo.Dog"/>
<!--byname會在容器自動查詢,和自己物件set方法的set後面的值對應的id
  例如:setDog(),取set後面的字元作為id,則要id = dog 才可以進行自動裝配
  
 -->
<bean id="people" class="pojo.People" autowire="byName">
	<property name="name" value="cbh"></property>
</bean>

彈幕評論:byName只能取到小寫,大寫取不到

7.2、使用註解實現自動裝配

jdk1.5支援的註解,spring2.5支援的註解

The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.(翻譯:基於註釋的配置的引入提出了一個問題,即這種方法是否比XML“更好”)

  1. 匯入context約束

xmlns:context="http://www.springframework.org/schema/context"

  1. 配置註解的支援:< context:annotation-config/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
</beans>

7.2.1、@Autowired

預設是byType方式,如果匹配不上,就會byName

在屬性上個使用,也可以在set上使用

我們可以不用編寫set方法了,前提是自動裝配的屬性在Spring容器裡,且要符合ByName 自動裝配

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
}

@Nullable 欄位標記了這個註解,說明該欄位可以為空

public name(@Nullable String name){

}

//原始碼
public @interface Autowired { 
	boolean required() default true; 
}

如果定義了Autowire的require屬性為false,說明這個物件可以為null,否則不允許為空(false表示找不到裝配,不丟擲異常)

7.2.2、@Autowired+@Qualifier

@Autowired不能唯一裝配時,需要@Autowired+@Qualifier

如果@Autowired自動裝配環境比較複雜。自動裝配無法通過一個註解完成的時候,可以使用@Qualifier(value = “dog”)去配合使用,指定一個唯一的id物件

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog")
    private Dog dog;
    private String name;
}

彈幕評論:

如果xml檔案中同一個物件被多個bean使用,Autowired無法按型別找到,可以用@Qualifier指定id查詢

7.2.3、@Resource

預設是byName方式,如果匹配不上,就會byType

public class People {
    Resource(name="cat")
    private Cat cat;
    Resource(name="dog")
    private Dog dog;
    private String name;
}

彈幕評論:

Autowired是byType,@Autowired+@Qualifier = byType || byName

Autowired是先byteType,如果唯一則注入,否則byName查詢。resource是先byname,不符合再繼續byType

區別:

@Resource和@Autowired的區別:

  • 都是用來自動裝配的,都可以放在屬性欄位上
  • @Autowired通過byType的方式實現,而且必須要求這個物件存在!【常用】
  • @Resource預設通過byname的方式實現,如果找不到名字,則通過byType實現!如果兩個都找不到的情況下,就報錯!【常用】
  • 執行順序不同:@Autowired通過byType的方式實現。@Resource預設通過byname的方式實現

8、使用註解開發

在spring4之後,使用註解開發,必須要保證aop包的匯入

使用註解需要匯入contex的約束

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

8.1、bean

彈幕評論:
有了< context:component-scan>,另一個< context:annotation-config/>標籤可以移除掉,因為已經被包含進去了。

<!--指定要掃描的包,這個包下面的註解才會生效
	別隻掃一個com.kuang.pojo包--> 
<context:component-scan base-package="com.kuang"/> 
<context:annotation-config/>
1234
//@Component 元件
//等價於<bean id="user" classs"pojo.User"/> 
@Component
public class User {  
     public String name ="秦疆";
}

8.2、屬性如何注入@value

@Component
public class User { 
    //相當於<property name="name" value="kuangshen"/> 
    @value("kuangshen") 
    public String name; 
    
    //也可以放在set方法上面
    //@value("kuangshen")
    public void setName(String name) { 
        this.name = name; 
    }
}

8.3、衍生的註解

@Component有幾個衍生註解,會按照web開發中,mvc架構中分層。

  • dao (@Repository)
  • service(@Service)
  • controller(@Controller)

這四個註解的功能是一樣的,都是代表將某個類註冊到容器中

8.4、自動裝配置

@Autowired:預設是byType方式,如果匹配不上,就會byName

@Nullable:欄位標記了這個註解,說明該欄位可以為空

@Resource:預設是byName方式,如果匹配不上,就會byType

8.5、作用域@scope

//原型模式prototype,單例模式singleton
//scope("prototype")相當於<bean scope="prototype"></bean>
@Component 
@scope("prototype")
public class User { 
    
    //相當於<property name="name" value="kuangshen"/> 
    @value("kuangshen") 
    public String name; 
    
    //也可以放在set方法上面
    @value("kuangshen")
    public void setName(String name) { 
        this.name = name; 
    }
}

8.6、小結

xml與註解:

  • xml更加萬能,維護簡單,適用於任何場合
  • 註解,不是自己的類使用不了,維護複雜

最佳實踐:

  • xml用來管理bean
  • 註解只用來完成屬性的注入
  • 要開啟註解支援

9、使用Java的方式配置Spring

不使用Spring的xml配置,完全交給java來做!

Spring的一個子專案,在spring4之後,,,它成為了核心功能


實體類:pojo的User.java

//這裡這個註解的意思,就是說明這個類被Spring接管了,註冊到了容器中 
@component 
public class User { 
    private String name;
    
    public String getName() { 
    	return name; 
    } 
    //屬性注入值
    @value("QINJIANG')  
    public void setName(String name) { 
    	this.name = name; 
    } 
    @Override 
    public String toString() { 
        return "user{" + 
        "name='" + name + '\''+ 
        '}'; 
    } 
}

彈幕評論:要麼使用@Bean,要麼使用@Component和ComponentScan,兩種效果一樣

配置檔案:config中的kuang.java

@Import(KuangConfig2.class),用@import來包含KuangConfig2.java

//這個也會Spring容器託管,註冊到容器中,因為他本米就是一個@Component 
// @Configuration表這是一個配置類,就像我們之前看的beans.xml,類似於<beans>標籤
@Configuration 
@componentScan("com.Kuang.pojo") //開啟掃描
//@Import(KuangConfig2.class)
public class KuangConfig { 
    //註冊一個bean , 就相當於我們之前寫的一個bean 標籤 
    //這個方法的名字,就相當於bean 標籤中的 id 屬性 ->getUser
    //這個方法的返同值,就相當於bean 標籤中的class 屬性 ->User
    
    //@Bean 
    public User getUser(){ 
    	return new User(); //就是返回要注入到bean的物件! 
    } 
}

彈幕評論:ComponentScan、@Component("pojo”) 這兩個註解配合使用

測試類

public class MyTest { 
    public static void main(String[ ] args) { 
    //如果完全使用了配置類方式去做,我們就只能通過 Annotationconfig 上下文來獲取容器,通過配置類的class物件載入! 
    ApplicationContext context = new AnnotationConfigApplicationContext(KuangConfig.Class); //class物件
    User getUser =(User)context.getBean( "getUser"); //方法名getUser
    System.out.Println(getUser.getName()); 
    } 
}

會建立兩個相同物件問題的說明:

彈幕總結 - -> @Bean是相當於< bean>標籤建立的物件,而我們之前學的@Component是通過spring自動建立的這個被註解宣告的物件,所以這裡相當於有兩個User物件被建立了。一個是bean標籤建立的(@Bean),一個是通過掃描然後使用@Component,spring自動建立的User物件,所以這裡去掉@Bean這些東西,然後開啟掃描。之後在User頭上用@Component即可達到spring自動建立User物件了

//這個也會Spring容器託管,註冊到容器中,因為他本米就是一個@Component 
// @Configuration表這是一個配置類,就像我們之前看的beans.xml,類似於<beans>標籤
@Configuration 
@componentScan("com.Kuang.pojo") //開啟掃描
//@Import(KuangConfig2.class)
public class KuangConfig { 
    //註冊一個bean , 就相當於我們之前寫的一個bean 標籤 
    //這個方法的名字,就相當於bean 標籤中的 id 屬性 ->getUser
    //這個方法的返同值,就相當於bean 標籤中的class 屬性 ->User
    
    //@Bean 
    public User getUser(){ 
    	return new User(); //就是返回要注入到bean的物件! 
    } 
}

彈幕評論:ComponentScan、@Component("pojo”) 這兩個註解配合使用

測試類

public class MyTest { 
    public static void main(String[ ] args) { 
    //如果完全使用了配置類方式去做,我們就只能通過 Annotationconfig 上下文來獲取容器,通過配置類的class物件載入! 
    ApplicationContext context = new AnnotationConfigApplicationContext(KuangConfig.Class); //class物件
    User getUser =(User)context.getBean( "getUser"); //方法名getUser
    System.out.Println(getUser.getName()); 
    } 
}

會建立兩個相同物件問題的說明:

彈幕總結 - -> @Bean是相當於< bean>標籤建立的物件,而我們之前學的@Component是通過spring自動建立的這個被註解宣告的物件,所以這裡相當於有兩個User物件被建立了。一個是bean標籤建立的(@Bean),一個是通過掃描然後使用@Component,spring自動建立的User物件,所以這裡去掉@Bean這些東西,然後開啟掃描。之後在User頭上用@Component即可達到spring自動建立User物件了

10、動態代理

代理模式是SpringAOP的底層

分類:動態代理和靜態代理

10.1、靜態代理


程式碼步驟:

1、介面

package pojo;
public interface Host {
	public void rent();
}

2、真實角色

package pojo;
public class HostMaster implements Host{	
    
	public void rent() {
		System.out.println("房東要出租房子");
	}
}

3、代理角色

package pojo;
public class Proxy {

	public Host host;
	
	public Proxy() {
		
	}
	
	public Proxy(Host host) {
		super();
		this.host = host;
	}
	
	public void rent() {
		seeHouse();
		host.rent();
		fee();
		sign();
	}
	//看房
	public void seeHouse() {
		System.out.println("看房子");
	}
	//收費
	public void fee() {
		System.out.println("收中介費");
	}
	//合同
	public void sign() {
		System.out.println("籤合同");
	}		
}

4、客戶端訪問代理角色

package holle4_proxy;

import pojo.Host;
import pojo.HostMaster;
import pojo.Proxy;

public class My {

	public static void main(String[] args) {
		//房東要出租房子
		Host host = new HostMaster();
		//中介幫房東出租房子,但也收取一定費用(增加一些房東不做的操作)
		Proxy proxy = new Proxy(host);
		//看不到房東,但通過代理,還是租到了房子
		proxy.rent();
		
	}
}


程式碼翻倍:幾十個真實角色就得寫幾十個代理

AOP橫向開發

10.2、動態代理

動態代理和靜態角色一樣,動態代理底層是反射機制

動態代理類是動態生成的,不是我們直接寫好的!

動態代理(兩大類):基於介面,基於類

  • 基於介面:JDK的動態代理【使用ing】
  • 基於類:cglib
  • java位元組碼實現:javasisit

瞭解兩個類
1、Proxy:代理
2、InvocationHandler:呼叫處理程式

例項:

介面 Host.java

//介面
package pojo2;
public interface Host {
	public void rent();
	
}

介面Host實現類 HostMaster.java

//介面實現類
package pojo2;
public class HostMaster implements Host{	
	public void rent() {
		System.out.println("房東要租房子");
	}
}

代理角色的處理程式類 ProxyInvocationHandler.java

package pojo2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

///用這個類,自動生成代理
public class ProxyInvocationHandler implements InvocationHandler {

	// Foo f =(Foo) Proxy.NewProxyInstance(Foo. Class.GetClassLoader(),
	// new Class<?>[] { Foo.Class },
	// handler);

	// 被代理的介面
	public HostMaster hostMaster ;
	
	public void setHostMaster(HostMaster hostMaster) {
		this.hostMaster = hostMaster;
	}

	// 得到生成的代理類 
	public Object getProxy() {
		// newProxyInstance() -> 生成代理物件,就不用再寫具體的代理類了
		// this.getClass().getClassLoader() -> 找到載入類的位置
		// hostMaster.getClass().getInterfaces() -> 代理的具體介面
		// this -> 代表了介面InvocationHandler的實現類ProxyInvocationHandler
		return Proxy.newProxyInstance(this.getClass().getClassLoader(), hostMaster.getClass().getInterfaces(), this);


	// 處理代理例項並返回結果
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		seeHouse();
		// 動態代理的本質,就是使用反射機制實現的
        // invoke()執行它真正要執行的方法
		Object result = method.invoke(hostMaster, args);
		fee();
		return result;
	}

	public void seeHouse() {
		System.out.println("看房子");
	}

	public void fee() {
		System.out.println("收中介費");
	}

}

使用者類 My2.java

package holle4_proxy;

import pojo2.Host;
import pojo2.Host2;
import pojo2.HostMaster;
import pojo2.ProxyInvocationHandler;

public class My2 {

	public static void main(String[] args) {
        
		//真實角色
		HostMaster hostMaster = new HostMaster();
        
		//代理角色,現在沒有;用代理角色的處理程式來實現Host介面的呼叫
		ProxyInvocationHandler pih = new ProxyInvocationHandler();
        
        //pih -> HostMaster介面類 -> Host介面
		pih.setHostMaster(hostMaster);
        
		//獲取newProxyInstance動態生成代理類
		Host proxy = (Host) pih.getProxy();
		
		proxy.rent();

	}
}

彈幕評論:
什麼時候呼叫invoke方法的?
代理例項呼叫方法時invoke方法就會被呼叫,可以debug試試

改為萬能代理類

///用這個類,自動生代理
public class ProxyInvocationHandler implements InvocationHandler {

	// 被代理的介面
	public Object target;

	public void setTarget(Object target) {
		this.target = target;
	}

	// 得到生成的代理類 -> 固定的程式碼
	public Object getProxy() {
		return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
	}

	// 處理代理例項並返回結果
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// 動態代理的本質,就是使用反射機制實現的
		// invoke()執行它真正要執行的方法
		Object result = method.invoke(target, args);
		return result;
	}

}

11、AOP

11.1、什麼是AOP

11.2、AOP在Spring中的使用

提供宣告式事務,允許使用者自定義切面

  • 橫切關注點:跨越應用程式多個模組的方法或功能。即是,與我們業務邏輯無關的,但是我們需要關注的部分,就是橫切關注點。如日誌,安全,快取,事務等等…
  • 切面(Aspect):橫切關注點 被模組化的特殊物件。即,它是一個類。(Log類)
  • 通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。(Log類中的方法)
  • 目標(Target):被通知物件。(生成的代理類)
  • 代理(Proxy):向目標物件應用通知之後建立的物件。(生成的代理類)
  • 切入點(PointCut):切面通知執行的”地點”的定義。(最後兩點:在哪個地方執行,比如:method.invoke())
  • 連線點(JointPoint):與切入點匹配的執行點。


SpringAOP中,通過Advice定義橫切邏輯,Spring中支援5種類型的Advice:


即AOP在不改變原有程式碼的情況下,去增加新的功能。(代理)

11.3、使用Spring實現AOP

匯入jar包

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

11.3.1、方法一:使用原生spring介面

springAPI介面實現

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--註冊bean-->
    <bean id="userservice" class="service.UserServiceImpl"/>
    <bean id="log" class="log.Log"/>
    <bean id="afterLog" class="log.AfterLog"/>
	<!--方式一,使用原生Spring API介面-->
    <!--配置aop,還需要匯入aop約束-->
    <aop:config>
        <!--切入點:expression:表示式,execution(要執行的位置)-->
        <aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"/>
        <!--UserServiceImpl.*(..) -》 UserServiceImpl類下的所以方法(引數)-->
        <!--執行環繞增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
        <!-- 環繞,在id="pointcut"的前後切入 -->
    </aop:config>

</beans>

execution(返回型別,類名,方法名(引數)) -> execution(* com.service.,(…))

UserService.java

package service;
public interface UserService {   
	    public void add() ;
	    public void delete() ;
	    public void query() ;
	    public void update();
}

UserService 的實現類 UserServiceImp.java

package service;

public class UserServiceImpl implements UserService {

    public void add() {
        System.out.println("add增");
    }
    public void delete() {
        System.out.println("delete刪");
    }
    public void update() {
        System.out.println("update改");
    }
    public void query() {
        System.out.println("query查");
    }
}

前置Log.java

package log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    //method:要執行的目標物件的方法
    //args:引數
    //target:目標物件
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被執行了");
    }
}

後置AfterLog.java

package log;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

public class AfterLog implements AfterReturningAdvice {
    //returnVaule: 返回值
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
    	System.out.println("執行了"+method.getName()+"方法,返回值是"+returnValue);
    }
}

測試類MyTest5

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;

public class MyTest5 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //注意:動態代理代理的是介面
        UserService userService = (UserService) context.getBean("userservice");
        userService.add();
    }
}

11.3.2、方法二:自定義類實現AOP

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
   	https://www.springframework.org/schema/beans/spring-beans.xsd
   	http://www.springframework.org/schema/aop
   	https://www.springframework.org/schema/aop/spring-aop.xsd">

   <!--註冊bean-->
   <bean id="userservice" class="service.UserServiceImpl"/>
   <bean id="log" class="log.Log"/>
   <bean id="afterLog" class="log.AfterLog"/>
   <!-- 方式二,自定義 -->
   <bean id="diy" class="diy.DiyPointcut"/>
   <aop:config>
       <!--自定義切面-->
       <aop:aspect ref="diy">
           <!--切入點-->
           <aop:pointcut id="point" expression="execution(* service.UserServiceImpl.*(..))"/>
           <aop:before method="before" pointcut-ref="point"/>
           <aop:after method="after" pointcut-ref="point"/>
       </aop:aspect>
   </aop:config>

</beans>
package diy;
public class DiyPointcut {

    public void before(){
        System.out.println("插入到前面");
    }

    public void after(){
        System.out.println("插入到後面");
    }
}
//測試
public class MyTest5 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //注意:動態代理代理的是介面
        UserService userService = (UserService) context.getBean("userservice");
        userService.add();
    }
}

11.3.3、方法三:使用註解實現

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">
	
    <!-- 註冊 -->
    <bean id="userservice" class="service.UserServiceImpl"/>
    <!--方式三,使用註解實現-->
    <bean id="diyAnnotation" class="diy.DiyAnnotation"></bean>
    
    <!-- 開啟自動代理 
		實現方式:預設JDK (proxy-targer-class="fasle")
    			 cgbin (proxy-targer-class="true")-->
	<aop:aspectj-autoproxy/>
    
</beans>

DiyAnnotation.java

package diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect  //標註這個類是一個切面
public class DiyAnnotation {
	
    @Before("execution(* service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("=====方法執行前=====");
    }

    @After("execution(* service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("=====方法執行後=====");
    }

    //在環繞增強中,我們可以給地暖管一個引數,代表我們要獲取切入的點
    @Around("execution(* service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("環繞前");

        Object proceed = joinPoint.proceed();

        System.out.println("環繞後");
    }
}

測試

public class MyTest5 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //注意:動態代理代理的是介面
        UserService userService = (UserService) context.getBean("userservice");
        userService.add();
    }
}

輸出結果:

12、整合mybatis

mybatis-spring官網:https://mybatis.org/spring/zh/

mybatis的配置流程:

  1. 編寫實體類
  2. 編寫核心配置檔案
  3. 編寫介面
  4. 編寫Mapper.xmi
  5. 測試

12.1、mybatis-spring-方式一

  1. 編寫資料來源配置
  2. sqISessionFactory
  3. sqISessionTemplate(相當於sqISession)
  4. 需要給介面加實現類【new】
  5. 將自己寫的實現類,注入到Spring中
  6. 測試!

先匯入jar包

<dependencies>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.4</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.12</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </dependency>

</dependencies>
	
<!--在build中配置resources,來防止資源匯出失敗的問題-->
<!-- Maven解決靜態資源過濾問題 -->
<build>
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
</build>


編寫順序:
User -> UserMapper -> UserMapper.xml -> spring-dao.xml -> UserServiceImpl -> applicationContext.xml -> MyTest6

程式碼步驟:

pojo實體類 User

package pojo;
import lombok.Data;
@Data
public class User {
	private int id;
	private String name;
	private String pwd;
}

mapper目錄下的 UserMapper、UserMapperImpl、UserMapper.xml

介面UserMapper

package mapper;
import java.util.List;
import pojo.User;
public interface UserMapper {
	public List<User> getUser();
}

UserMapperImpl

package mapper;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import pojo.User;

public class UserMapperImpl implements UserMapper{
	
	//我們的所有操作,在原來都使用sqlSession來執行,現在都使用SqlSessionTemplate;
	private SqlSessionTemplate sqlSessionTemplate;

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}

	public List<User> getUser() {
		UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
		return mapper.getUser();
	}
}

UserMapper.xml (狂神給面子才留下來的)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        
<!-- 繫結介面 -->
<mapper namespace="mapper.UserMapper">
	<select id="getUser" resultType="pojo.User">
		select * from mybatis.mybatis
	</select>
</mapper>

resource目錄下的 mybatis-config.xml、spring-dao.xml、applicationContext.xml

mybatis-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>
	<!--開啟日誌-->
	<settings>
		<setting name="logImpl" value="STDOUT_LOGGING" />
	</settings>
	
	<!--可以給實體類起別名 -->
	<typeAliases> 
		<package name="pojo" />
	</typeAliases>

</configuration>

spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">
		
	<!--DataSource:使用Spring的數幫源替換Mybatis的配置 其他資料來源:c3p0、dbcp、druid 
		這使用Spring提供的JDBC: org.springframework.jdbc.datasource -->
	<!--data source -->
	<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.cj.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai"/>
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	
	<!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <!--繫結 mybatis 配置檔案-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

	<!-- sqlSessionTemplate 就是之前使用的:sqlsession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    	<!-- 只能使用構造器注入sqlSessionFactory 原因:它沒有set方法-->	
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
		
</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">
    
	<!-- 匯入spring-dao.xml -->
	<import resource="spring-dao.xml"/>
	
    <bean id="userMapper" class="mapper.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"></property>
    </bean>

</beans>

測試類

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import mapper.UserMapper;
import pojo.User;
public class MyTest6 {
	public static void main(String[] args) {
        
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		UserMapper userMapper = (UserMapper) context.getBean("userMapper");
		
		for (User user : userMapper.getUser()) {
			System.out.println(user);
		}
	}
}

12.2、mybatis-spring-方式二

UserServiceImpl2

package mapper;
import pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
//繼承SqlSessionDaoSupport 類
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
    public List<User> getUser() {
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.getUser();
        //或者一句話:return getSqlSession().getMapper(UserMapper.class).getUser();
    }
}

spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">
		
	<!--DataSource:使用Spring的數幫源替換Mybatis的配置 c3p0 dbcp druid 
		這使用Spring提供的JDBC: org.springframework.jdbc.datasource -->
	<!--data source -->
	<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.cj.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai"/>
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	
	<!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <!--繫結 mybatis 配置檔案-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    
	<!-- 方法二:SqlSessionTemplate 可以不寫了-->
    
</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">

	<import resource="spring-dao.xml" />

	<!-- 方法二 -->
	<bean id="userMapper2" class="mapper.UserMapperImpl2">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
</beans>

測試

public class MyTest6 {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserMapper userMapper = (UserMapper) context.getBean("userMapper2");
		for (User user : userMapper.getUser()) {
			System.out.println(user);
		}
	}
}

13. 宣告式事務

  • 把一組業務當成一個業務來做;要麼都成功,要麼都失敗!
  • 事務在專案開發中,十分的重要,涉及到資料的一致性問題
  • 確保完整性和一致性

事務的ACID原則:
1、原子性
2、隔離性
3、一致性
4、永續性

ACID參考文章:https://www.cnblogs.com/malaikuangren/archive/2012/04/06/2434760.html

Spring中的事務管理

  • 宣告式事務:AOP
  • 程式設計式事務:需要再程式碼中,進行事務管理

宣告式事務

先匯入jar包

<dependencies>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.4</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.12</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </dependency>

</dependencies>
	
<!--在build中配置resources,來防止資源匯出失敗的問題-->
<!-- Maven解決靜態資源過濾問題 -->
<build>
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
</build>

程式碼步驟:

pojo實體類 User

package pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
	private int id;
	private String name;
	private String pwd;
}

mapper目錄下的 UserMapper、UserMapperImpl、UserMapper.xml

介面UserMapper

package mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import pojo.User;

public interface UserMapper {
	public List<User> getUser();
	
	public int insertUser(User user); 
	
	public int delUser(@Param("id") int id); 
}

UserMapperImpl

package mapper;

import pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;

public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
    public List<User> getUser() {
    	User user = new User(5,"你好","ok");
    	insertUser(user);
    	delUser(5);
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.getUser();
        //或者return  getSqlSession().getMapper(UserMapper.class).getUser();
    }
    //插入
	public int insertUser(User user) {
		return getSqlSession().getMapper(UserMapper.class).insertUser(user);
	}
	//刪除
	public int delUser(int id) {
		return getSqlSession().getMapper(UserMapper.class).delUser(id);
	}
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        
<!-- 繫結介面 -->
<mapper namespace="mapper.UserMapper">
	<select id="getUser" resultType="pojo.User">
		select * from mybatis.mybatis
	</select>
	
	<insert id="insertUser"  parameterType="pojo.User" >
		insert into  mybatis.mybatis (id,name,pwd) values (#{id},#{name},#{pwd})
	</insert>
	
	<delete id="delUser" parameterType="_int">
		deleteAAAAA from mybatis.mybatis where id = #{id}
		<!-- deleteAAAAA是故意寫錯的 -->
	</delete>

</mapper>

resource目錄下的 mybatis-config.xml、spring-dao.xml、applicationContext.xml

mybatis-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 -->
<configuration>
	
	<!--開啟日誌-->
	<settings>
		<setting name="logImpl" value="STDOUT_LOGGING" />
	</settings>
	
	<!--可以給實體類起別名-->
	<typeAliases> 
		<package name="pojo" />
	</typeAliases>

</configuration>

spring-dao.xml(已匯入約束)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
		
	<!--data source -->
	<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.cj.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai"/>
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	
	<!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <!--繫結 mybatis 配置檔案-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
	
	<!--宣告式事務-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="datasource" />
    </bean>

    <!--結合aop實現事務織入-->
    <!--配置事務的通知類-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--給哪些方法配置事務-->
        <!--新東西:配置事務的傳播特性 propagation-->
        <tx:attributes>
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="query" read-only="true"/>
            <!-- *號包含上面4個方法:
            <tx:method name="*" propagation="REQUIRED"/> -->
        </tx:attributes>
    </tx:advice>

    <!--配置事務切入-->
    <aop:config>
        <aop:pointcut id="txpointcut" expression="execution(* mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txpointcut"/>
    </aop:config>

</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">

	<import resource="spring-dao.xml" />

	<bean id="userMapper" class="mapper.UserMapperImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
</beans>

測試類

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import mapper.UserMapper;import pojo.User;
public class MyTest7 {
	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		UserMapper userMapper = (UserMapper) context.getBean("userMapper");
		
		for (User user : userMapper.getUser()) {
			System.out.println(user);
		}
	}
}

思考:
為什麼需要事務?

  • 如果不配置事務,可能存在資料提交不一致的情況下;
  • 如果不在spring中去配置宣告式事務,我們就需要在程式碼中手動配置事務!
  • 事務在專案的開發中非常重要,涉及到資料的一致性和完整性問題!