文章目录
- 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><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><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("中米手机");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:component-scan base-package="com.aistart"/><context:annotation-config/><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"><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><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="mapperLocations" value="classpath*:com/aistart/mapper/*Mapper.xml"></property><property name="configLocation" value="mybatis-config.xml"></property></bean><bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property name="dataSource" ref="dataSource"/></bean><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 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();}
}