首页 > 其他分享 >Spring的学习

Spring的学习

时间:2023-02-17 22:03:55浏览次数:46  
标签:http Spring void springframework 学习 org import public

1.Spring

简介:

Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。

优点:

组成:

1.1 spring官方中文文档

​https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/data-access.html​

2.IOC理论推导

userDao --> userDaoImpl

userService-->userServiceImpl

我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改原来的代码!如果程序的代码量十分的大,修改一次的成本代价十分昂贵

我们使用一个set接口实现

private userDao userDao ;
//利用set进行动态实现值的注入
public void setUserDao(userDao userDao) {
this.userDao = userDao;
}

3.IOC的本质

控制反转是一种通过描述(xml或注解)并通过第三方去生产或者获取特定对象的方式。在spring中实现控制反转的是ioc容器,其中实现方法是依赖注入。

4.Hello---Spring

编写一个类

package com.pojo;

public class Hello {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void show(){
System.out.println("Hello"+name);
}

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

配置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
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 使用spring 创建-->
<bean id="hello" class="com.pojo.Hello">
<property name="name" value="gukeyang"/>
</bean>

</beans>

测试的进行

import com.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class beanstest {
public static void main(String[] args) {
//获取spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我们的对象都在spring中管理,我们要使用,直接从外面取出来就可以了
Hello hello = (Hello) context.getBean("hello");

System.out.println(hello.toString());
}
}

5.IOC创建对象方式

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了。

6.spring配置说明

别名

<alias name="user" alias="sadas"/>

bean的配置

<!--
id:bean的唯一标识符,也就是相当于我们学的类型名
class:bean 对象所对应的全限定名:包名+类型
name也是别名,而且可以取多个别名
-->
<bean id="userT" class="com.kuang.pojo.UserT" name="user2">
<property name ="name" value ="西部开源"/>
</bean>

import的说明

7.DI依赖注入环境

package com.pojo;

import java.util.*;

public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobby;

private Map<String,String> card;
private Set<String> games;
private Properties info;
private String wife;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Address getAddress() {
return address;
}

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

public String[] getBooks() {
return books;
}

public void setBooks(String[] books) {
this.books = books;
}

public List<String> getHobby() {
return hobby;
}

public void setHobby(List<String> hobby) {
this.hobby = hobby;
}

public Map<String, String> getCard() {
return card;
}

public void setCard(Map<String, String> card) {
this.card = card;
}

public Set<String> getGames() {
return games;
}

public void setGames(Set<String> games) {
this.games = games;
}

public Properties getInfo() {
return info;
}

public void setInfo(Properties info) {
this.info = info;
}

public String getWife() {
return wife;
}

public void setWife(String wife) {
this.wife = wife;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobby=" + hobby +
", card=" + card +
", games=" + games +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
}
package com.pojo;

public class Address {
private String address;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}
<bean id="address" class="com.pojo.Address"/>
<bean id="student" class="com.pojo.Student">
<!-- 普通注入-->
<property name="name" value="谷科阳"/>
<!-- bean注入-->
<property name="address" ref="address"/>
<!-- 数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!--list-->
<property name="hobby">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
</list>
</property>
<!--map-->
<property name="card">
<map>
<entry key="1" value="a"/>
<entry key="2" value="b"/>
<entry key="3" value="c"/>
<entry key="4" value="d"/>
</map>
</property>

<property name="games">
<set>
<value>lol</value>
<value>coc</value>
</set>
</property>

<property name="wife">
<null/>
</property>

<property name="info">
<props>
<prop key="学号">20211544113</prop>
<prop key="性别">男</prop>
<prop key="姓名">谷科阳</prop>
</props>
</property>

</bean>

测试test

一:得到name
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Student student = (Student) context.getBean("student");

System.out.println(student.getName());
二:得到student对象的值
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Student student = (Student) context.getBean("student");

System.out.println(student.toString());

8.c命名和p命名注入

xmlns:p="http://www.springframework.org/schema/p"
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="[emailprotected]"/>
</bean>

<bean name="p-namespace" class="com.example.ExampleBean"
p:email="[emailprotected]"/>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="thingOne" class="x.y.ThingTwo"/>
<bean id="thingTwo" class="x.y.ThingThree"/>

<!-- traditional declaration -->
<bean id="thingOne" class="x.y.ThingOne">
<constructor-arg ref="thingTwo"/>
<constructor-arg ref="thingThree"/>
<constructor-arg value="[emailprotected]"/>
</bean>

<!-- c-namespace declaration -->
<bean id="thingOne" class="x.y.ThingOne" c:thingTwo-ref="thingTwo" c:thingThree-ref="thingThree" c:email="[emailprotected]"/>
</beans>

9.bean的作用域

单例

原型

10.自动装配bean

  • 自动配置是spring满足bean依赖的一种方式
  • Spring会在上下文中自动寻找,并自动会给bean装配属性

在spring的三种装配方式

1.在xml中显示的配置

2.在java中显示的配置

3.隐式的装配bean

小结:

byname的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致;

bytype的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致;

11.注解实现装配

1.导入约束

2.配置注解的支持

<?xml versinotallow="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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config/>

</beans>
package com.pojo;

public class dog {
public void shout() {
System.out.println("汪汪汪!");

}
}

package com.pojo;

public class cat {
public void shout(){
System.out.println("喵喵喵!");
}
}
package com.pojo;

import org.springframework.beans.factory.annotation.Autowired;

public class people {
@Autowired
private cat cat;
@Autowired
private dog dog;
private String name;

public com.pojo.cat getCat() {
return cat;
}

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

public com.pojo.dog getDog() {
return dog;
}

public void setDog(com.pojo.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 versinotallow="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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config/>

<bean id="cat" class="com.pojo.cat"/>
<bean id="dog" class="com.pojo.dog"/>


<bean id="people" class="com.pojo.people"/>
</beans>
import com.pojo.people;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
@Test
public void test01(){
ApplicationContext Context = new ClassPathXmlApplicationContext("beans.xml");
people people = Context.getBean("people", people.class);

people.getCat().shout();
people.getDog().shout();
}
}

@Resource

@Resource
private cat cat;
@Resource
private dog dog;
private String name;

12.Spring注解开发

在spring4之后,要使用注解开发,必须保证aop的包导入了

1.bean

2.属性如何注入

@Component
public class User {
@Value("gukeyang")
public String name ;


@Value("value-set")
public void setName(String name) {
this.name = name;
}
}

3.衍生的注解

4.自动装配置

5.作用域

6.小结

<context:component-scan base-package="com.gukeyang.pojo"/>
<context:annotation-config/>

13.使用JavaConfig实现配置

实体类

@Component

public class User{

配置文件

测试类

14.代理模式

为什么要学习代理模式?因为这是SpringAop的底层

代理模式的分类:静态代理和动态代理

静态代理

动态代理

Proxy代理,InvocationHandler

动态代理的好处:

可以使真实角色的操作更加纯粹!不用去关注一些公共的业务。

公共也就交给代理角色,实现业务的分工!

公共业务发生扩展的时候,方便集中管理

一个动态代理类代理的是一个接口,一般就是对应的一类业务

一个动态代理类可以代理多个类,只要实现同一个接口即可

15.AOP的实现方式

使用Spring实现AOP

<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<!-- 方式一:使用原生Spring API接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>

<!--执行环绕增加-->
<aop:advisor advice-ref="log " pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
public interface UserService {
public void add();
public void delete();
public void update();
public void select();

}
public class UserServiceImpl implements UserService{

@Override
public void add() {
System.out.println("增加了一个用户");
}

@Override
public void delete() {
System.out.println("删除了一个用户");
}

@Override
public void update() {
System.out.println("修改了一个用户");
}

@Override
public void select() {
System.out.println("查询了一个用户");
}
}
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class log implements MethodBeforeAdvice {

//method:要执行的目标对象的方法
//args:参数
//target:目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {

System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了" );
}
}
package com.kuang.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {

//returnValue返回值
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"返回结果为:"+returnValue);
}
}
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理是接口
UserService UserService = (UserService) context.getBean("UserService");

UserService.select();
}
}

自定义来实现AOP(主要是切面定义)

package com.kuang.diy;

public class DiyPointCut {
public void before(){
System.out.println("=======方法执行前========" );
}

public void after(){
System.out.println("=======方法执行后========" );
}
}
<!--方式二:自定义类-->
<bean id="diy" class="com.kuang.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理是接口
UserService UserService = (UserService) context.getBean("UserService");

UserService.select();
}
}

使用注解实现AOP

16.整合Mybatis

步骤:

1.导入相关的jar包

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>

2.编写配置文件

3.测试

16.1整合mybatis方式一

什么是mybatis-spring

​http://mybatis.org/spring/zh/sqlsession.html​

SqlSessionTemplate

<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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">



<!--DataSource 使用spring的数据源替换mybatis的配置 c3p0 dbcp druid-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimeznotallow=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory-->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

<!--只能使用构造器注入,没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>

</bean>

<bean id="userMapper" class="com.gu.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>


</beans>
package com.gu.mapper;

import com.gu.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{

private SqlSessionTemplate sqlSession;

public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}

@Override
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
import com.gu.mapper.UserMapper;
import com.gu.pojo.User;
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.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class MyTest {
@Test
public void test() throws IOException {
/* String resources = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resources);

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.selectUser();
for (User user : userList) {

System.out.println(user);

}*/
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);

}
}
}

16.2整合mybatis方式二

SqlSessionDaoSupport

<bean id="userMapper2" class="com.gu.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
package com.gu.mapper;

import com.gu.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {

@Override
public List<User> selectUser() {
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}

17.回顾mybatis

编写实体类

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

编写核心配置文件

<?xml versinotallow="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.gu.pojo"/>
</typeAliases>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimeznotallow=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>

<mappers>
<mapper class="com.gu.mapper.UserMapper"/>
</mappers>

</configuration>
<?xml versinotallow="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">
<parent>
<artifactId>spring-study</artifactId>
<groupId>com.gukeyang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-04-mybatis</artifactId>

<properties>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

<!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

</project>

编写接口

package com.gu.mapper;

import com.gu.pojo.User;

import java.util.List;

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

编写mapper.xml

<?xml versinotallow="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="com.gu.mapper.UserMapper">
<select id="selectUser" resultType="User">
select * from mybatis.user;
</select>

</mapper>

测试

import com.gu.mapper.UserMapper;
import com.gu.pojo.User;
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.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class MyTest {
@Test
public void test() throws IOException {
String resources = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resources);

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.selectUser();
for (User user : userList) {

System.out.println(user);

}
}
}

18.声明式事务

回顾事务

要么都成功,要么都失败

事务在项目开发中,涉及到数据的一致性问题,不能马虎

确保完整性和一致性

事务的ACID原则:

原子性

一致性

隔离性 多个业务可能同时操作同一个资源,防止数据损坏

持久性 事务一旦提交,无论系统发生什么问题,结果都不会再被影响,被持久化的写到存储器中!

spring的事务管理

声明式 AOP

编程式 需要在代码中进行事务的管理

<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--结合AOP实现事务的织入-->
<!--配置事务的类-->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<!--给哪些方法配置事务-->
<!--配置事务传播特性:new 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"/>
<tx:method name="* " propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

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

##

标签:http,Spring,void,springframework,学习,org,import,public
From: https://blog.51cto.com/u_15971093/6064526

相关文章

  • SAP BW/4HANA学习笔记1
    1.MasterDataBW/4HANABW/4HANA简介BW/4HANA的前身是SAPBW(BusinessWarehouse),本质是一种数据仓库解决方案。数据仓库解决方案:1.云端仓库;2.SAPHANAforSQL,偏个......
  • SAP BW/4HANA学习笔记2
    2.DataModelingBW/4HANADataModeling简介DataQuality:数据质量问题;silos(桶仓):大量重复冗余的主数据,独立计算统计;数据silos缺点:交流误解,聚合加总错误,不同角度数据差异......
  • WDA学习(29):WDA & HTML
    1.22HTMLContainer本实例测试HTML在WDA中结合使用。創建WDAComponent:Z_TEST_WDA99UIElementVIEW:MAIN 創建UIElement:Splitter,SP1設置height:600px;設置s......
  • 学习打卡day07-面向对象
    1,基本知识面向过程编程:开发一个一个的方法,有数据要处理了,我们就调方法来处理。强调的是做事情的每一个步骤,逐个步骤完成功能。侧重于步骤面向对象编程:开发一个一个......
  • SpringBoot 多环境切换
    日常开发中一般都会有三个不同的环境,分别是开发环境(dev),测试环境(test)和生产环境(prod),不同的环境各种配置都不相同,比如数据库配置,服务器端口等等。SpringBoot多环境配置Sp......
  • SpringBoot集成Logback遇到的问题
     SpringBoot集成Logback遇到的问题https://zhuanlan.zhihu.com/p/540737614 解决log4j和self4j日志报错CouldNOTfindresource[logback.groovy]及CouldNOTfindr......
  • LaTex第一天-基础学习
    在线LaTeX编辑器:https://www.overleaf.comTeXLive下载:https://www.tug.org/texlive/acquire-iso.htmlMikTeX下载:https://miktex.org/downloadLaTeX公式编辑器:https://......
  • docker学习记录
    系统环境NAME="Ubuntu"VERSION="18.04.2LTS(BionicBeaver)"感受惊为天人,这玩意真的好用在我目前来看,docker就是一个轻量化的虚拟机,用多了vmware,用这样迅捷的虚拟......
  • go学习
    go环境变量$GOPATH在1.11之前它是我们书写项目的工作目录在1.11之后它内部存储的是我们公共go.mod下载的依赖包$GOROOT安装目录$GOARCH目标机器的处理器架构,它......
  • 230217 关于英语学习的目标
    最近几天,你在听larry的英语课程.通过larry的介绍,你有了一些新的感触与理解.尤其是你关于你的英语的学习目标,你值得去深入思考,同时,不断校正与优化你的目标.之前,你......