1、Spring整合MyBatis
1.1、添加依赖以及过滤信息
<?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.westos</ groupId>
< artifactId> spring-mybatis-csdn</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< dependencies>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.2</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.47</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.1.10.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.1.10.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 2.0.2</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.12</ version>
</ dependency>
</ dependencies>
< build>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> true</ filtering>
</ resource>
</ resources>
</ build>
</ project>
1.2、创建实体类
package org. westos. pojo;
import lombok. AllArgsConstructor;
import lombok. Data;
import lombok. NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
1.3、创建接口
package org. westos. mapper;
import org. westos. pojo. User;
import java. util. List;
public interface UserMapper {
public List< User> getUserList ( ) ;
}
1.4、创建接口的实现类
package org. westos. mapper. impl;
import org. mybatis. spring. SqlSessionTemplate;
import org. westos. mapper. UserMapper;
import org. westos. pojo. User;
import java. util. List;
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate ( SqlSessionTemplate sqlSessionTemplate) {
this . sqlSessionTemplate = sqlSessionTemplate;
}
public List< User> getUserList ( ) {
UserMapper mapper = sqlSessionTemplate. getMapper ( UserMapper. class ) ;
return mapper. getUserList ( ) ;
}
}
1.5、创建接口对应的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 = " org.westos.mapper.UserMapper" >
< select id = " getUserList" resultType = " User" >
select * from user
</ select>
</ mapper>
1.6、创建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: 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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd" >
< 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=true& useUnicode=true& characterEncoding=utf8" />
< property name = " username" value = " root" />
< property name = " password" value = " 123456" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " typeAliasesPackage" value = " org.westos.pojo" />
< property name = " mapperLocations" value = " classpath:org/westos/mapper/xml/UserMapper.xml" />
</ bean>
< bean id = " sqlSession" class = " org.mybatis.spring.SqlSessionTemplate" >
< constructor-arg index = " 0" ref = " sqlSessionFactory" />
</ bean>
< bean id = " userMapperImpl" class = " org.westos.mapper.impl.UserMapperImpl" >
< property name = " sqlSessionTemplate" ref = " sqlSession" />
</ bean>
</ beans>
1.7、测试
import org. junit. Test;
import org. springframework. context. support. ClassPathXmlApplicationContext;
import org. westos. mapper. UserMapper;
import org. westos. mapper. impl. UserMapperImpl;
import org. westos. pojo. User;
import java. util. List;
public class MyTest1 {
@Test
public void test1 ( ) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
UserMapper userMapperImpl = ( UserMapper) context. getBean ( "userMapperImpl" ) ;
List< User> userList = userMapperImpl. getUserList ( ) ;
for ( User user : userList) {
System. out. println ( user) ;
}
}
}
2、Spring整合事务
2.1、新增AOP的依赖
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.9.4</ version>
</ dependency>
2.2、在spring整合mybatis的xml配置文件中添加信息
< 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" >
< aop: config>
< aop: pointcut id = " txPointcut" expression = " execution(* org.westos.mapper.*.*(..))" />
< aop: advisor advice-ref = " txAdvice" pointcut-ref = " txPointcut" />
</ aop: config>
< tx: advice id = " txAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " *" propagation = " REQUIRED" />
</ tx: attributes>
</ tx: advice>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>