赞
踩
<?xml version="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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring-Study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>IOC-01</module>
<module>HelloSpring</module>
<module>IOC-02</module>
<module>DI-01</module>
<module>Autowired-01</module>
<module>anno-01</module>
<module>JavaConfig</module>
<module>proxy_AOP</module>
<module>AOP_01</module>
<module>Spring-Mybatis</module>
</modules>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.spring-jdbc/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
<scope>runtime</scope>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!--mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
</project>
首先创建一个Mapper接口创建方法:
package Mapper;
import pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> select();
}
在UserMapper.xml中写sql语句:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定一个对应的Mapper接口-->
<mapper namespace="Mapper.UserMapper">
<select id="select" resultType="pojo.User">
select * from mybatis.user;
</select>
</mapper>
mybatis-spring可以在spring的spring-mybatis.xml配置文件中整合mybatis配置(获取SqlSessionFactory对象、SqlSession对象、DataSource):
<?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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--注解支持-->
<context:annotation-config/>
<context:component-scan base-package="java"/>
<!--数据源DataSource:使用spring的数据源替换mybatis的配置 c3p0 dbcp druid
我们这里使用spring提供的jdbc
-->
<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=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="fzl-2000528"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis配置文件mybatis-config.xml和Mapper.xml-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:UserMapper.xml"/>
</bean>
<!--获取sqlSession对象-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能用构造器注入sqlSessionFactory 因为SqlSessionTemplate类没有set方法-->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!--创建UserMapperImpl类对象,通过这个UserMapper接口的实现类对象来调用方法-->
<bean id="userMapper" class="Mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
</beans>
然后将spring-mabytis.xml整合到整个spring的配置文件中:<import resource="spring-mybatis.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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--注解支持-->
<context:annotation-config/>
<context:component-scan base-package="java"/>
<import resource="spring-mybatis.xml"/>
</beans>
上方代码就配置完了mybatis,下面就是实现增删改查操作,因为是用spring整合了mybatis,所以要用Mapper接口的实现类来实现接口中的方法,然后再创建Mapper接口实现类的对象,从而调用方法的sql语句:
下方是UserMapper的实现类UserMapperImpl:
package Mapper;
import org.mybatis.spring.SqlSessionTemplate;
import pojo.User;
import java.util.List;
/**
* 原来我们的所有操作都使用sqlSession来执行,现在都使用sqlSessionTemplate
*/
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<User> select() {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
return mapper.select();
}
}
然后就可以在测试类中测试一下能否成功执行sql:
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user:
userMapper.select() ) {
System.out.println(user);
}
}
事务:
事务的ACID原则:
spring中的事务管理:
我们只关注声明式事务管理:
在spring-mabytis.xml中添加声明式事务配置:
首先要导入tx配置
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
然后添加配置:
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
</bean>
<!--结合AOP实现事务的织入-->
<!--配置事务通知-->
<!--导入tx配置-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--配置事务的传播特性 propagation-->
<tx:attributes>
<tx:method name="add" />
</tx:attributes>
</tx:advice>
将Mapper包下所有类都切入事务:
<!--配置事务切入点-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* Mapper.*.*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="txPointCut"/>
</aop:config>
这样结合AOP横切事务,可以在不改变代码的情况下添加事务。
这样配置好事务的spring-mybatis.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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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/context
https://www.springframework.org/schema/context/spring-context.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">
<!--注解支持-->
<context:annotation-config/>
<context:component-scan base-package="java"/>
<!--数据源DataSource:使用spring的数据源替换mybatis的配置 c3p0 dbcp druid
我们这里使用spring提供的jdbc
-->
<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=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="fzl-2000528"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis配置文件mybatis-config.xml和Mapper.xml-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:UserMapper.xml"/>
</bean>
<!--获取sqlSession对象-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能用构造器注入sqlSessionFactory 因为SqlSessionTemplate类没有set方法-->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!--创建UserMapperImpl类对象,通过这个UserMapper接口的实现类对象来调用方法-->
<bean id="userMapper" class="Mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
</bean>
<!--结合AOP实现事务的织入-->
<!--配置事务通知-->
<!--导入tx配置-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--配置事务的传播特性 propagation-->
<tx:attributes>
<tx:method name="addUser" />
<tx:method name="select" />
</tx:attributes>
</tx:advice>
<!--配置事务切入点-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* Mapper.*.*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
首先获取声明式事务transactionManager,然后创建事务并规定将哪些方法加入事务,然后结合AOP确定事务切入点,在要执行事务的地方全部加入事务。
在UserMapper接口中添加add方法:
package Mapper;
import pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> select();
public int addUser(User user);
}
在UserMapperImpl中实现方法:
package Mapper;
import org.mybatis.spring.SqlSessionTemplate;
import pojo.User;
import java.util.List;
/**
* 原来我们的所有操作都使用sqlSession来执行,现在都使用sqlSessionTemplate
*/
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<User> select() {
User user = new User(9,"刘六","666666");
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
addUser(user);
return mapper.select();
}
@Override
public int addUser(User user) {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
return mapper.addUser(user);
}
}
测试:
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user:
userMapper.select() ) {
System.out.println(user);
}
}
结果:
User(id=1, name=张三, pwd=123456)
User(id=2, name=李四, pwd=654321)
User(id=3, name=王五, pwd=222222)
User(id=4, name=王六, pwd=123123)
User(id=9, name=刘六, pwd=666666)
可以看出事务添加成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。