本文主要是介绍010 spring整合mybatis(事务)(xml),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- pom.xml
- ProductsMapper.java
- ProductsServiceImpl.java
- ProductsService.java
- ProductsMapper.xml
- ApplicationContext.xml
- db.properties
- mybatis-config.xml
- ProductsServiceImplTest.java
pom.xml
<?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>com.aistart</groupId><artifactId>spring_mybatis</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!--mybatis的环境--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.15</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><!--spring的环境--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.30</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.30</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.6</version></dependency><!--测试环境--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!--整合环境--><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>**/*.properties</include><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>true</filtering></resource></resources></build></project>
ProductsMapper.java
package com.aistart.mapper;import org.springframework.stereotype.Repository;public interface ProductsMapper {public int insertOne(String name);public int deleteOne(String name);
}
ProductsServiceImpl.java
package com.aistart.service.impl;import com.aistart.mapper.ProductsMapper;
import com.aistart.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class ProductsServiceImpl implements ProductsService {@Autowiredprivate ProductsMapper productsMapper;public void setProductsMapper(ProductsMapper productsMapper) {this.productsMapper = productsMapper;}@Overridepublic int insertOne(String name) {int rows = productsMapper.insertOne(name);return rows;}@Overridepublic int deleteOne(String name) {int rows = productsMapper.deleteOne(name);return rows;}@Overridepublic void consume() {/*事务开启*/productsMapper.insertOne("中米手机");// System.out.println(1/0);/*事务中,如果发生异常,回滚*/productsMapper.deleteOne("中米手机");/*事务关闭*/}@Overridepublic void print(String msg) {System.out.println(msg);}}
ProductsService.java
package com.aistart.service;public interface ProductsService {public int insertOne(String name);public int deleteOne(String name);public void consume();public void print(String msg);
}
ProductsMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aistart.mapper.ProductsMapper"><insert id="insertOne">insert into products set product_name = #{name}</insert><delete id="deleteOne">deletefrom productswhere product_name = #{name};</delete></mapper>
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"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!--context注解--><context:component-scan base-package="com.aistart"/><context:annotation-config/><!--将mapper代理对象托管到Bean容器中--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.aistart.mapper"/></bean><!--推荐,从外部获取其中的细节配置--><context:property-placeholder location="classpath:db.properties"/><!--设置数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><!--四大件 获取方式1.手动写2.通过配置文件设置,上面这个
<context:property-placeholder location="classpath:db.properties"/>--><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!--整合,将factory交管给spring--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--数据源的获取--><property name="dataSource" ref="dataSource"></property><!--mapper的定位,注意格式--><property name="mapperLocations" value="classpath*:com/aistart/mapper/*Mapper.xml"></property><!--mapper文件定位--><property name="configLocation" value="mybatis-config.xml"></property></bean><!--搞事务了--><!--增强--><bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property name="dataSource" ref="dataSource"/></bean><!--通知--><!--id时通知命名,transaction-manager管理的引入--><tx:advice id="txAdvice" transaction-manager="manager"><!--对应一个函数业务一个--><tx:attributes><tx:method name="consume"/></tx:attributes></tx:advice><!--本质要进行织入--><aop:config><!--一种规则--><aop:pointcut id="tx" expression="execution(* com.aistart.service.impl.ProductsServiceImpl.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="tx" /></aop:config></beans>
db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatisdb?serverTimezone=Asia/Shanghai&usessl=false
jdbc.username=root
jdbc.password=root
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings></configuration>
ProductsServiceImplTest.java
package com.aistart.service.impl;import com.aistart.config.SpringConfig;
import com.aistart.service.ProductsService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import static org.junit.Assert.*;public class ProductsServiceImplTest {@Testpublic void insertOne() {ApplicationContext context;// context = new AnnotationConfigApplicationContext(SpringConfig.class);context = new ClassPathXmlApplicationContext("ApplicationContext.xml");ProductsService productsService = context.getBean( ProductsService.class);productsService.insertOne("大米");}@Testpublic void consume() {ApplicationContext context;context = new ClassPathXmlApplicationContext("ApplicationContext.xml");ProductsService productsService = context.getBean( ProductsService.class);productsService.consume();}
}
这篇关于010 spring整合mybatis(事务)(xml)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!