Spring的简单使用(3)
- 2022 年 8 月 9 日
- 筆記
- Spring的简单使用
一:SM框架的整合:
所需要的依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
mybatis核心配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"//mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
spring—mapper.核心配置文件(接管了mybatis的核心配置文件):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="//www.springframework.org/schema/beans"
xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
xmlns:context="//www.springframework.org/schema/context"
xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd //www.springframework.org/schema/context //www.springframework.org/schema/context/spring-context.xsd">
<!--读取jdbc配置文件-->
<context:property-placeholder location="db.properties" system-properties-mode="FALLBACK"></context:property-placeholder>
<!--创建数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置sqlsessionFactoryBean类-->
<bean id="sqlSessionFactoryBeanName" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--配置mybatis的核心配置文件-->
<property name="configLocation" value="mybatis-config.xml"></property>
<!-- 注册实体类别名-->
<property name="typeAliasesPackage" value="com.ztb.pojo"></property>
</bean>
<!--注册mapper.xml文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ztb.dao"></property>
</bean>
</beans>
spring-service核心配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="//www.springframework.org/schema/beans"
xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:context="//mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd //mybatis.org/schema/mybatis-spring //mybatis.org/schema/mybatis-spring.xsd">
<import resource="spring-mapper.xml"></import>
<context:scan base-package="com.ztb.spring"></context:scan>
</beans>
database.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=ztb
dao层:
dao层接口:
public interface UserMapper {
int insert(User user);
}
dao层配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"//mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ztb.dao.UserMapper">
<insert id="insert" parameterType="com.ztb.pojo.User">
insert into user values (#{id},#{name},#{pwd})
</insert>
</mapper>
实体类:
package com.ztb.pojo;
public class User {
private Integer id;
private String name;
private String pwd;
public User() {
}
public User(Integer id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
service层:
service接口:
public interface UserService {
int insert(User user);
}
service实现类:
@Service("user")
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
@Override
public int insert(User user) {
return userMapper.insert(user);
}
}
测试:
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-service.xml");
UserService userServiceImpl = (UserService) context.getBean("user");
int i = userServiceImpl.insert(new User(13, "张三", "2222"));
System.out.println(i);
二:事务:
分为注解式事务和声明式事务:
1:注解式事务:使用@Transactional注解完成事务控制,此注解可以添加到类上,,则对类中的所有方法都执行事务的处理。也可以添加到方法上,添加到方法上只是对该方法进行事务处理。
由于事务都是在业务逻辑层,所以所有的事务都必须配在service层:
首先需要在spring-service配置文件中添加支持注解式事务:
<!-- 1添加事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 因为事务必须关联数据库操作,所以要配置数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2添加事务的注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
注解式事务必须要在实现类上面添加注解:
2:声明式事务(重要):在配置文件中添加一次,整个项目都遵循事物的设定。
声明式事务配置如下:
<!-- 1添加事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 因为事务必须关联数据库操作,所以要配置数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2添加事务的声明驱动-->
<tx:advice id="myadvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- *指的是为所有方法添加事务 REQUIRED指的是增删改,-->
<tx:method name="*" propagation="NOT_SUPPORTED"/>
</tx:attributes>
</tx:advice>
<!-- 绑定切面和切入点-->
<aop:config>
<aop:pointcut id="mycut" expression="execution(* com.ztb.spring.*.*(..))"/>
<!-- 把myadvice的事务绑定到mycut的service包(包名写错了sping)下的任意方法中-->
<aop:advisor advice-ref="myadvice" pointcut-ref="mycut"></aop:advisor>
</aop:config>
也可以如下写:
事务优先级的处理:
如果有一些方法和声明式事务不匹配,则可以设置优先级:
在配置文件中添加注解式事务,设置优先级比声明式要高,这样在方法上就会选择注解式事务来操作了
<tx:annotation-driven order="100"></tx:annotation-driven>注解式设置为100
<aop:advisor order="1" advice-ref="myadvice" pointcut-ref="mycut"></aop:advisor>声明式设置为1