主頁 > 後端開發 > Spring5.0學習知識總結

Spring5.0學習知識總結

2022-08-29 07:02:38 後端開發

## spring總結
### 1 、spring
#### 1.1 spring 簡介
簡化了專案的開發、但配置依然很繁瑣!!!
擴展:

SSH:Struct2 + Spring + Hibernate

SSM: SpringMvc +Spring + Mybatis

spring官網:https://spring.io/

spring下載: http://repo.spring.io/libs-release-local/org/springframework/spring/

maven配置:https://mvnrepository.com/
![在這里插入圖片描述](https://img-blog.csdnimg.cn/c3e91a155ab74f84907acde844fb6a81.png#pic_center)
```java
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>

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

#### 1.2 優點

一句話:Spring就是一個輕量級的控制反轉(IOC)和面向切面編程(AOP)的框架

#### 1.3 spring的組成(重要)

![img](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vaGFveXVubHdoL1R5cG9yYWltYWdlL3Jhdy9tYXN0ZXIvaW1nLzIwMjAwODIxMDc1NDM4LnBuZw?x-oss-process=image/format,png)

#### 1.4 擴展

spring->springboot->springcloud->spring網格

框架的一般原則:約定>配置>編碼

學習springcloud要先學springboot、spring、java基礎依次類推~~

### 2 、IOC(重要)

原始:程式主動創建物件、控制權在程式員上

spring:使用set注入(lombok簡化開發springboot)程式不在具有主動性、而是變成了被動接受的物件!

#### 2.1 IOC的本質

IOC是spring的核心內容!可以使用組態檔(xml)和注解的方式實作IOC、其實作方式是依賴注入(DI)

```java
public class hello(){
private String str;
getter/setter(略)
toString(略)
}
```

```xml
xml:官網復制
<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">

<!--使用spring來創建物件、在spring這里都稱為bean
型別 變數名 = new 型別
Hello hello = new Hello();
id =變數名
class=new 的物件
property 相當于給物件中的屬性設定一個值,
bean = 物件 new hello();
name:
value:具體的值、基本資料型別
ref:應用spring容器中創建好的物件
-->
<bean id="hello" calss="com.pojo.hello">
<property name="str" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/Spring"></property>
</bean>

</beans>

```

```
public class testMain(){
public static void main(String[] args){
//獲取spring的背景關系物件 固定寫法
ApplicationContext context = new ClassPathApplicationContext(beans.xml)
//物件都在spring中管理了、我們要使用直接從物件里面取出來 就可以了,
Hello hello = (hello) context.getBean("hello")
System.out.println(hello)
}
}
```

```java
public class testlh(){
public static void main(String[] args){
//獲取ApplictionContext:拿到spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//容器在手、天下我有、需要什么get什么
UserServiceImpl userserviceimpl = context.getBean("UserServiceImpl");
userServiceImpl.getUser();
}
}
```

IOC:物件由spring來創建、管理、裝配

### 3、spring的創建方式

```java
public class User(){
private String name;
getter/setter
toString
}
```

```java
public class MyTest(){
public static void main(String[] args){
Application context = new ClassPathXmlApplicationContext("beans.xml")
User user = (User) context.getBean("user");
user.show();
}
}
```

```xml
<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">

<bean id="user" >
<property name="name" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/lihui">

</property>
</bean>

</bean>
```

IOC創建物件的方式:(重要)

1、使用無參構造創建物件、默認

2、使用有參構造創建物件:三種方式

```xml
<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">
<!--
第一種方式:下標賦值
-->
<bean id="user" >
<constrctor-arg index="0" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/lihui"></constrctor-arg>
</bean>
<!--
第二種方式:不建議使用、通過型別創建的
-->
<bean id="user" >
<constructor-arg type="java.lang.String" value=https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/“lihui>
</bean>
<!--
第三種方式:直接通過引數名稱設定(常用)
-->
<bean id="user" >
<constructor-arg name="name" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/lihui"> </constructor-arg>
</bean>
</bean>
```

總結:在組態檔加載的時候、容器中管理的物件就已經開始初始化了!!!

### 4、Spring配置

#### 4.1 別名、配置、import

```xml
<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">

<!--別名:名字和資料庫中的名字一樣,如果添加了別名、我們也可以通過別名來獲取這物件-->
<alias name="user" alias="userNew"></alias>

<!--
配置:
id:bean的唯一識別符號,也就是相當于我們學的物件名
class:bean 物件所對應的全限定名:包名+型別
name:也是別名,而且name更高級、可以同時取多個別名、
可以通過空格、逗號、分號進行分割!!!
-->
<bean id="userId" name="userId2"></bean>

<import resource="applicationContext.xml"></import>
<import resource="beans.xml"></import>
<import resource="beans1.xml"></import>
<import resource="beans2.xml"></import>
</beans>
```

```java
public class MyTest(){
public static void main(String[] args){
Application context = new ClassPathXmlApplicationContext("beans.xml")
User user = (User) context.getBean("user,u2,u3,u4");
user.show();
}
}

```

### 5、依賴注入(DI)

#### 5.1 構造器注入

#### 5.2 Set注入(重要)

依賴注入:set注入

? 依賴:bean物件的創建依賴于容器

? 注入:bean物件中的所有屬性、由容器來注入

【環境搭建】

```java
public class Address(){
private String address;
...
}

```

```java
public class Student(){
//各種不同屬性的注入方式
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,Sring> card;
private Set<String> games;
private String wife;
private Properties info;
}

```

```xml
<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">
<!--第一種:普通值注入:,value-->
<bean id="student" >
<property name="name" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/lihui"></property>
</bean>
<!--第二種:bean注入 ref-->
<bean id="address" ></bean>
<!--第三種:陣列注入 ref-->
<property name="books">
<arrar>
<value>hahh</value>
<value>youyuio</value>
<value>kafghj</value>
<value>oiuh</value>
</arrar>
</property>
<!--list注入-->
<property name="list">
<list>lkgjha</list>
<list>ioregh</list>
<list>oaigh</list>
</property>
<!--map注入-->
<property name="map">
<map>
<entry key="id" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/h1234"></entry>
<entry key="akfg" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/dlakgh"></entry>
</map>
</property>
<!--set注入-->
<property name="set">
<set>
<value>algh</value>
<value>cos</value>
<value>32oriu</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null></null>
</property>
<!--properties注入-->
<property name="info">
<props>
<prop key="ihi">2345</prop>
<prop key="afskl">jalfkj</prop>
<prop key="weil"></prop>
<prop key="aklegja"></prop>
</props>
</property>
<beans>
```

```java
public class testStudent(){
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlContext("bean.xml");
Student student = (Student)context.getBean("student")
System.out.println(student.getName());
}
}
```

```xml
<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">
<!--擴展方式注入:
注意點:p命名和c命名不能直接使用,需要匯入xml約束!!!
-->
<!--C命名和P命名(官網添加)-->
<!--p命名空間注入,可以直接注入屬性的值:property-->
<bean id="user" p:name="lihui" p:age="18">
</bean>
<!--c命名空間注入,可以通過構造器注入:construct-args-->
<bean id="user" c:name="lihui" c:age="18">
</bean>

</beans>
```

```
p命名空間和c命名空間的使用:
......

```

#### 5.3 bean的作用域

共六種:

1、單列默認(默認)

scope="singleton"

2、原型模式:每此從容器中get的時候、都會產生一個新物件~

scope="prototype"

3、其余的request、session、application、這些都只在web開發中使用!!!

### 6、Bean的自動裝配

自動裝配是spring滿足bean依賴的一種方式,

spring會在背景關系中自動尋找、并自動給bean裝配屬性

 

在spring中有三種裝配的方式

1、在xml中顯示的配置

2、在java中顯示裝配

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

#### 6.1、測驗

```java
public class Cat(){
public void shout(){
System.out.println("miao~~~");
}
}
```

```java
public class Dog(){
public void shout(){
System.out.println("wang~~~~");
}
}
```

```java
public class People(){
private Cat cat;
private Dog dog;
private String name;
.......
}
```

```xml
bean.xml:
<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">

<bean id="cat" ></bean>
<bean id="dog" ></bean>
<bean id="people" >
<property name="name" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/lihui"></property>
<property name="dog" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/dog"></property>
<property name="cat" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/cat"></property>
</bean>

</beans>
```

```java
public class test(){
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
People people = context.getBean("people",People.class);
people.getDog().shout();
people.getCat().shout();
}
}
```

#### 6.2、自動裝配:autowire(byName、byType)

```xml
bean.xml:
<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">

<bean id="cat" ></bean>
<bean id="dog" ></bean>
<!--
byName:會自動在容器背景關系中查找,和自己物件set方法后面對應的值的bean-id
byType:會自動在容器背景關系中查找,和自己屬性型別相同的bean
都有弊端:byName:要保證所有bean的id唯一、并且這個bean需要和自動注入的屬性的set方法的值一致,
byType:需要保證所有的bean的class唯一、并且這個bean需要自動注入的屬性的型別一致
-->
<bean id="people" autowire="byName" autowire="byType">
<property name="name" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/lihui"></property>
</bean>

</beans>
```

#### 6.3、使用注解實作自動裝配(重要)

jdk1.5以上支持的注解、spring2.5以上支持注解

要使用注解:

1、匯入約束:xml中配置context約束

2、配置注解的支持:context:annotation-config/

```xml
<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">
<!----!!>學會自己配置xml組態檔</!----!!>
<!--配置注解的支持-->
<context:annotation-config></context:annotation-config>

</beans>

```

###### @Autowired

直接在屬性上使用即可!也可以在set方式上使用,

使用Autowired可以不用撰寫set方法、前提是自動裝配的屬性在IOC(Spring)容器中存在,且符合名字byName!

```
@Nullable:欄位標記了這個屬性、
@Autowired(required=false):required的值為false,說明物件可以為空、否則物件不能為空!!!
@Qualifier(value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/dog"):如果自動裝配的環境畢節復雜,一個注解無法完成時可以用@Qualifier指定裝配多個物件
-------------------
@Resource:結合了名字(name)和型別(type)
```

總結:@Resource和@Autowired的區別?

- 都是用來自動裝配的,都可以實作放在屬性欄位上
- @Autowired通過byType的方式實作、而且必須要求這個物件存在![常用]
- @Resource默認通過byName的方式實作、如果找不到名字、則通過byType實作、如果兩個都找不到就報錯!!!
- 執行順序不同:
- @Autowired 先通過byTpe的方式實作,再通過byName的方式實作
- 如果Autowired不能唯一自動裝配實作,則需要通過@Qulifier(value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/xxx")來實作
- @Resource 先通過使用byName再通過byType的方式實作!!!

### 7、使用注解開發

在spring4之后、要使用注解開發、必須保證aop的包匯入了!!!

需要匯入注解的支持~

```xml
<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 component-scan="com.lh.pojo"/>
<context:annotation-config></context:annotation-config>

</beans>
```

```java
@Component:組件
@Component
public class User(){
@Value("lihui")
private String name;

@Value("lihui")
private void setName(String name){
this.name=name;
}
}
```

#### 1、bean

#### 2、屬性如何注入

#### 3、衍生的注解

@Component有幾個衍生的注解,我們在web開發中會按照mvc三層架構分層!!

dao:【@Repository】

service:【@Service】

controller:【@Controller】

這四個注解功能一樣:都是代表將某個類注入到spring容器中~

#### 4、自動裝配

~~~
上面已陳述
~~~

#### 5、作用域

```
@Scope("singleton","property")
```

#### 6、小節

- xml與注解

- xml更加萬能,適用于任何場合~,維護相對復雜
- 注解不是自己類使用不了,維護相對復雜

- xml與注解的最佳實踐

- xml用來管理bean

- 注解只負責完成屬性的注入,

- 在使用的程序中只需要主要一個問題:要讓注解生效,必須開啟注解的支持和掃描對應的包

- ```
<!--指定要掃描的包,這個包下的注解就會生效-->
<context component-scan="com.lh.pojo"/>
<context:annotation-config></context:annotation-config>
```

### 8、使用java的方式配置spring

不使用spring的xml配置,完全使用java來注解來做~~

javaconfig

```java
//這個注解的意思說明這個類被spring接管了,注冊到了容器中,
@Component
public class User(){
@Value("lihui")//屬性注入值
private String name;
......
}
```

```java
@Configuration//這個也會被spring容器托管,注冊到spring容器中,因為他也是一個組件,代表這是一個配置類,和beans.xml一樣的
@ComponentScan("com.lh.pojo")
@Import("lihui.class")//新特性~~~~
public class lihuiConfig(){
//注冊一個bean就相當于,我們之前寫的一個bean標簽
//這個方法的名字,就相當于bean標簽中的id屬性
//這個方法的回傳值,就相當于bean標簽中的class屬性
@Bean
public User user(){
return new User(); //就是回傳要注入到bean的物件~
}
}
```

```java
public class test(){
public static void main(String[] args){
//如果完全使用了配置類方式去做,我們就要通過AnnotationConfig背景關系來獲取容器,通過配置類的class物件加載~
Application context = new AnnotationConfigApplicationContext(lihui.class);
User getUser =(User) context.getBean("getUser")
System.out.println(getUser.getName());
}

}
```

這個純java的配置方式,在springboot中隨處可見~~~

### 9、AOP(重點)

#### 9.1 代理模式

為啥要學習代理模式?

因為這就是springAop的底層【springAOP和springMVC重要】

代理模式的分類:

~~~~
我---->中介----->買房

~~~~

#### 9.2、靜態代理:

角色分析:

- 抽象角色:一般會使用介面或者抽象類來解決

- 真實角色:被代理的角色

- 代理角色:代理真實角色,代理真實角色后,我們一般會做附屬操作

- 客戶:訪問代理物件的人

```java
//租房介面
public interface Rent(){
public void rent();
}

```

```java
//房東
public class Host implements Rent(){
public void rent(){
System.out.println("房東要出租房子");
}
}

```

```java
public class Client{
public static void main(String[] args){
Host host = new Host();
//代理,代理一般會有一些附屬操作~~~
Proxy proxy = new Proxy(host);
proxy.rent();
}
}

```

```java
public class Proxy{
private Host host;
public Proxy(){}
public proxy(Host host){
this.host=host;
}
public void rent(){
host.rent();
}
//看房
public void seeHose(){
System.out.println("中介帶你看房");
}
//收中介費
public void fare(){
Syetem.out.println("收中介費");
}

}

```

代理模式的好處:

- 可以是真實角色的操作根據純粹,不要去關注一些公共的業務

- 公共業務就交給代理角色,實作了業務的分工

- 公共業務發生擴展的時候,方便集中管理

缺點:

代碼量翻倍,開發效率較低~~~

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-zJL1CTEm-1661669913089)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1661652710109.png)]

#### 9.3、動態代理:

- 動態代理和靜態代理一樣,
- 動態代理的代理類是動態生成的,不是我們直接寫好的
- 動態代理分為兩大類:基于介面的動態代理,基于類的動態代理
- 基于介面----JDK動態代理【建議使用】
- 基于類:cglib
- java位元組碼實作:javasist

需要了解兩個類:Proxy:代理、InvocationHandler:呼叫處理程式

**InvocationHandler**

```java
//用這個類,自動生成代理類
public class ProxyInvocationHandler implements InvocationHandle{
//被代理的介面
private Rent rent;
public void setRent(Rent rent){
this.rent=rent;
}
//生成得到代理類
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(),this);
}
//處理代理實體,并回傳結果
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{
//動態代理的本質,就是使用反射機制實作!!!
Object result = method.invoke(rent,args);
return result;
}
//添加
public void seeHose(){
System.out.println("hafgjkllj");
}
}

```

```java
//真實角色
public class true(){
Host host = new Host();
//代理角色
ProxyInvocationHandler pih = new ProxyInvocationHandler();
pih.setRent(host);
Rent proxy = (Rent) pih.getProxy();
proxy.rent();
}

```

動態代理的好處:

- 具有靜態代理的所有好處
- 一個動態代理類代理的是一個介面,一般就是對應的一個業務,
- 一個動態代理類可以代理多個類,只有實作了介面,

#### 9.3、AOP(面向多面編程)

#### AOP的實作方式一

```xml
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>

```

```java
public interface UserService{
public void add();
public void delete();
public void update();
public void select();
}

```

```java
public class UserServiceImpl implements UserService{
public void add(){
sout("...")
}
public void delete(){
sout("...")
}
public void update(){
sout("...")
}
public void select(){
sout("...")
}
}

```

方式一:使用spring的介面【主要是springAPI介面實作】

```java
public class log implements MethOdBeforeAdvice{
//method:要執行的目標物件的方法
//args:引數
//target:目標物件
public void before(Method method,Object[] args,Object target) throws Throwable{
sout(target.getClass().getName()+method.getMethod());
}
}

```

```xml
//注冊bean
<bean id="userService" >
</bean>
//配置aop:需要匯入aop的約束
<aop:config>
//切入點 expression運算式,execution(要執行的位置****)
<aop:pointcut id="pointcut" expression="execution(要執行的位置!*****)"></aop:>
//執行環繞
<aop:advisor advice-ref="afterLog"></aop:>
</aop:config>

//自定義類
<bean id="diy" calss="com.lh.diy.DiyPointCut"></bean>
<aop:config>
//自定義切面 ref要參考的類
<aop:aspect ref="diy">
//切入點
<aop:pointcut id="point" expression="execution(* com.lh.service.UserServiceImpl.*(..))">
//通知
<aop:before method="before" pointcut-ref="point"></aop:>
</aop:>
</aop:>
</aop:config>

//注解實作
<aop:aspectj-autoproxy proxy-target-></aop:aspectj-autoproxy>

```

**注:動態代理代理的是介面,不能代理實作類!!!**

方式二:使用自定義類實作【主要是實作切面】

```java
public class DiyPointCut{
public void before(){
sout("...")
}
public void after(){
sout("...")
}
}

```

方式三:注解實作

```java
@Aspect//標注這是一個切面
public class AnnotationPointCut{
@Before("execution(* com.lh.UserserviceImpl*(..))")
public void before(){
sout("....")
}
}

```

### 10、整合Mybatis

步驟:

1、匯入相關jar包

- junit
- mybatis
- mysql
- spring相關
- aop織入
- mybatis-spring【new package】

```xml
<dependencies>
...<略>
</dependencies>

```

2、撰寫組態檔

3、測驗

#### 10.1、回顧mybatis

步驟:

- 撰寫物體類

```java
@Data
public class User(){
private String name;
private Integer age;
}

```

- 撰寫核心組態檔

```xml
<typeAliases>
<package name="com.lh.pojo"></package>
</typeAliases>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC"> </transactionManager>
<dataSource type="POOLED">
<property name="driver" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/xxx"></property>
<property name="username" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/xxx"></property>
<property name="password" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/xxx"></property>
</dataSource>
</environment>
</environments>

<mappers>
<mapper > </mapper>
</mappers>

```

- 撰寫介面

```java
public interface UserMapper(){
public List<User> selectUser();
}

```

- 撰寫mapper.xml

```xml
<mapper namespace="com.lh.mapper.UserMapper">
<select id="selectUser" resultType="user">
select *from mybatis.user;
</select>
</mapper>

```

- 測驗

```java
public class mytest(){
@Test
public void test(){
String resources ="mybatis-config.xml";
Resources input = Resources.getResourcesAsStream(resources);
......
}
}

```

#### 10.2 mybatis-spring

整合mybatis的方式一:

```java
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>

```

```xml
//DataSource:使用spring的資料源替換mybatis的配置 c3p0 dbcp druid
//使用spring直接提供的JDBC
<bean id="datasource" >
<property name="driverClassName" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/xxx"></property>
<property name="url" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/xxx"></property>
<property name="username" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/xxx"></property>
<property name="password" value="https://www.cnblogs.com/lihui-qianying/archive/2022/08/28/xxx"></property>
</bean>

<bean id="sqlSessionFactory" >
<property name="" value=""></property>
<property name="" value=""></property>
</bean>

```

 

整合mybatis的方式二:(略)

### 11、宣告式事務

#### 1、事務?

- 把一組業務當做一個業務來做,要么成功,要么失敗!
- 事務在專案開發中,十分重要,設計到資料的一致性問題,不可馬虎~~
- 確保完整性和一致性

事務的ACID原則:

- 原子性
- 一致性
- 隔離性
- 多個事務可能操作同一個資源,防止資料損壞
- 持久性
- 事務一旦提交,無論系統發生什么問題,結果都不在被影響,

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/502976.html

標籤:其他

上一篇:行程、執行緒補充與協程相關介紹

下一篇:Pytest框架 — 16、Pytest的測驗報告(pytest-html插件和Allure框架)

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more