目录
spring-dao.xml 【建议在里面写set 和 typeAliases 】
mybatis-config.xml 【工具类】 【以加事务】
pom.xml
<dependencies>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.12</version>
</dependency>
<!--aspectJ AOP 织入器-->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<!--mybatis-spring整合包 【重点】-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
<!--配置Maven静态资源过滤问题!-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.png</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
文件目录
UserMapper
public interface UserMapper {
List<User1> getUserList();
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gh.Mapper.UserMapper">
<select id="getUserList" resultType="user">
select * from user;
</select>
</mapper>
UserMapperImpl 【工具类】
public class UserMapperImpl implements UserMapper {
//我们的所有操作,都使用sqLSession来执行,在原来,现在都使用sqLSessionTemplate;
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<User1> getUserList() {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
List<User1> userList = mapper.getUserList();
return userList;
}
}
User
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User1 {
private int id;
private String name;
private String pwd;
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.gh.Mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
</beans>
db.properties 【工具类】
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&serverTimezone=UTC&characterEncoding=utf8
username=root
password=123456
spring-dao.xml 【建议在里面写set 和 typeAliases 】
<?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>
<!--一般在mybatis中留下设置、和别名 【看个人习惯】-->
<settings>
<!--输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--开启驼峰命名转换-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--显示开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--引入别名typeAliases-->
<typeAliases>
<!--方式一-->
<typeAlias type="com.gh.pojo.User1" alias="user"/> <!--建议-->
</typeAliases>
</configuration>
mybatis-config.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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--Datasource:使用Spring的数据源替换Mybatis 的配置c3pe dbcp druid
我们这里使用Spring提供的JDBC : org.springframework.jdbc.datasource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&
useUnicode=true&serverTimezone=UTC&characterEncoding=utf8"/>
<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"/>
<!--关联Mybatis-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/huang/dao/*.xml"/>
</bean>
<!-- 配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.gh.dao"/>
</bean>
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--结合AOP实现事务的植入-->
<!--配置事务的类;-->
<tx:advice id="tx" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--配置事务的传播特性: new,propagation:传播,REQUIRED(默认)-->
<tx:attributes>
<tx:method name="select*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<!--所有方法--><!--true为只读-->
<!-- <tx:method name="*" read-only="true"/>-->
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.gh.mapper.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
测试1
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User1 user1 : userMapper.getUserList()) {
System.out.println(user1);
}
}
方式一
UserMapperImpl1
public class UserMapperImpl implements UserMapper {
//我们的所有操作,都使用sqLSession来执行,在原来,现在都使用sqLSessionTemplate;
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<User1> getUserList() {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
List<User1> userList = mapper.getUserList();
return userList;
}
}
/* 注册
<bean id="userMapper" class="com.gh.Mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
*/
方式二
UserMapperImpl2
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
@Override
public List<User> select() {
//SqlSession sqlSession = getSqlSession();
//UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
//最精简版
return getSqlSession().getMapper(UserMapper.class).getUserList();
//getUserList()自己的 dao 方法
}
}
/* 注册
<bean id="userMapper2" class="com.gh.Mapper.UserMapperImpl2">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
*/
测试
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User1 user1 : userMapper.getUserList()) {
System.out.println(user1);
}
}
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper2", UserMapper.class);
for (User1 user1 : userMapper.getUserList()) {
System.out.println(user1);
}
}
方式三【常用】
自动扫描 【工具类】
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.gh.dao"/>
</bean>
此时 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:context="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--Datasource:使用Spring的数据源替换Mybatis 的配置c3pe dbcp druid
我们这里使用Spring提供的JDBC : org.springframework.jdbc.datasource-->
<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&useUnicode=true&characterEncoding=UTF-8&serverTimezone=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"/>
<!--关联Mybatis-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/gh/Mapper/*.xml"/>
</bean>
<!--UserMapperImpl2不需要下面的配置*************************************************-->
<!--注册sqlSessionTemplate , 关联sqlSessionFactory-->
<!--SqlSessionTemplate就是我们使用的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能利用构造器注入-->
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.gh.Mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
<bean id="userMapper2" class="com.gh.Mapper.UserMapperImpl2">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
</beans>
测试
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper2", UserMapper.class);
for (User1 user1 : userMapper.getUserList()) {
System.out.println(user1);
}
}
Spring中的事务管理
要么都成功,要么都失败
十分重要,涉及到数据一致性
确保完整性和一致性
事务的ACID原则:
原子性
一致性
隔离性
多个业务可能操作一个资源,防止数据损坏
持久性
事务一旦提交,无论系统发生什么问题,结果都不会被影响。
Spring中的事务管理
编程式事务管理 需要在代码中,进行事务的管理
将事务管理代码嵌到业务方法中来控制事务的提交和回滚
缺点:必须在每个事务操作业务逻辑中包含额外的事务管理代码
声明式事务管理 AOP 横切 不影响原来的代码
一般情况下比编程式事务好用。
将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。
将事务管理作为横切关注点,通过aop方法模块化。Spring中通过Spring AOP框架支持声明式事务管理。
使用Spring管理事务,注意头文件的约束导入 : tx
xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
配置好事务管理器后我们需要去配置事务的通知 和 配置AOP
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--结合AOP实现事务的植入-->
<!--配置事务的类;-->
<tx:advice id="tx" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--配置事务的传播特性: new,propagation:传播,REQUIRED(默认)-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
<!--所有方法--><!--true为只读-->
<!-- <tx:method name="*" read-only="true"/>-->
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.gh.mapper.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="txPointCut"/>
</aop:config>