赞
踩
<?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>
package com.aistart.mapper;
import org.springframework.stereotype.Repository;
public interface ProductsMapper {
public int insertOne(String name);
public int deleteOne(String name);
}
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 { @Autowired private ProductsMapper productsMapper; public void setProductsMapper(ProductsMapper productsMapper) { this.productsMapper = productsMapper; } @Override public int insertOne(String name) { int rows = productsMapper.insertOne(name); return rows; } @Override public int deleteOne(String name) { int rows = productsMapper.deleteOne(name); return rows; } @Override public void consume() { /*事务开启*/ productsMapper.insertOne("中米手机"); // System.out.println(1/0); /*事务中,如果发生异常,回滚*/ productsMapper.deleteOne("中米手机"); /*事务关闭*/ } @Override public void print(String msg) { System.out.println(msg); } }
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); }
<?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.aistart.mapper.ProductsMapper"> <insert id="insertOne"> insert into products set product_name = #{name} </insert> <delete id="deleteOne"> delete from products where product_name = #{name}; </delete> </mapper>
<?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/beans http://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/aop https://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>
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
<?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>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
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 { @Test public void insertOne() { ApplicationContext context; // context = new AnnotationConfigApplicationContext(SpringConfig.class); context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); ProductsService productsService = context.getBean( ProductsService.class); productsService.insertOne("大米"); } @Test public void consume() { ApplicationContext context; context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); ProductsService productsService = context.getBean( ProductsService.class); productsService.consume(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。