spring整合mybatis-XML形式
2019-08-18 本文已影响0人
艾特小师叔
Spring-MyBatis
Spring整合MyBatis
导入包
spring相关包
aspectjweaver.jar
spring-aop-4.3.24.RELEASE.jar
spring-aspects-4.3.24.RELEASE.jar
spring-beans-4.3.24.RELEASE.jar
spring-context-4.3.24.RELEASE.jar
spring-context-support-4.3.24.RELEASE.jar
spring-core-4.3.24.RELEASE.jar
spring-expression-4.3.24.RELEASE.jar
spring-jdbc-4.3.24.RELEASE.jar
spring-orm-4.3.24.RELEASE.jar
spring-oxm-4.3.24.RELEASE.jar
spring-tx-4.3.24.RELEASE.jar
mybatis相关包
mybatis-3.5.1.jar
mysql
mysql-connector-java-5.1.20-bin.jar
日志包
commons-logging-1.2.jar
log4j-1.2.17.jar
log4j-api-2.11.2.jar
log4j-core-2.11.2.jar
spring与mybatis整合包
mybatis-spring-1.3.2.jar
分页插件包
pagehelper-5.1.10
基本配置文件
日志:log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
数据库配置:jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=true
user=root
password=root
XML配置文件
spring
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 引入dao 配置文件 -->
<import resource="application-dao.xml"/>
</beans>
application-dao.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 引入properties配置文件 JDBC 配置 -->
<context:property-placeholder location="jdbc.properties" system-properties-mode="FALLBACK"/>
<!-- 配置dataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--
创建SqlSession
1.创建SqlSessionFactory
2.创建SqlSession
-->
<!--1.创建SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- mybatis核心配置文件 -->
<property name="configLocation" value="mybatis.cfg.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 扫描mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置扫描的mapper接口 spring 生成代理对象-->
<property name="basePackage" value="com.mrt.mapper" />
<!-- 多个mapper包时可以使用以下配置 -->
<!--<property name="basePackage" value="com.mrt.mapper1,com.mrt.mapper2" /> -->
<!-- <property name="basePackage" >
<value>
com.mrt.mapper1
com.mrt.mapper2
</value>
</property> -->
<!-- 配置 sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
mybatis
mybatis.cfg.xml
<?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="logImpl" value="LOG4J"/>
</settings>
<!-- 配置别名 -->
<typeAliases>
<package name="com.mrt.pojo"/>
</typeAliases>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<!-- 加载mapper文件 -->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
UserMapper.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="com.mrt.mapper.IUserMapper">
<select id="selectUser" resultType="com.mrt.pojo.User" >
select id,name,pwd from user
</select>
</mapper>
java包类:
com.mrt.mapper.IUserMapper
package com.mrt.mapper;
import java.util.List;
import com.mrt.pojo.User;
public interface IUserMapper {
public List<User> selectUser();
}
com.mrt.pojo.User
package com.mrt.pojo;
public class User {
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
}
}
com.mrt.test.Test
package com.mrt.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mrt.mapper.IUserMapper;
import com.mrt.pojo.User;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
IUserMapper userMapper = context.getBean(IUserMapper.class);
List<User> users = userMapper.selectUser();
System.out.println(users);
}
}
Spring事务配置
注解形式
application-tx.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 创建事务管理器对象 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven/>
</beans>
package com.mrt.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.mrt.mapper.IUserMapper;
import com.mrt.pojo.User;
import com.mrt.service.IUserService;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private IUserMapper userMapper;
/**
* 事务传播行为
* propagation :
* Propagation.REQUIRED(默认) : 若当前存在事务 则在当前事务中执行 多个操作共用同一个事务
* Propagation.SUPPORTS : 若当前有事务则在事务中执行,若没有事务则在非事务的情况下执行
* Propagation.MANDATORY : 必须在事务中执行 若当前没有事务则会发生异常
* REQUIRES_NEW : 必须新建一个事务,若当前有事务则当前事务挂起,执行自己事务 必须 使用 JtaTransactionManager事务管理器
* PROPAGATION_NEVER : 不适用任何事务,若存在事务则异常
* Propagation.NESTED : 若存在活动的事务,则运行在一个嵌套的事务中。若没有活动事务,则新建一个。仅仅支持 DataSourceTransactionManager 事务管理器
* 外部事务可以回滚内部事务
* Propagation.NOT_SUPPORTED:总是非事务地执行,并挂起任何存在的事务,需要使用:JtaTransactionManager 事务管理器
*/
@Override
@Transactional
public User addUser(User u) {
u = new User(0, "addUser", "1111");
userMapper.insertUser(u);
return u;
}
@Override
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public User addUser1(User u) {
u = new User(0, "addUser1", "22222");
userMapper.insertUser(u);
int m = 0;
System.out.println(10/m);
return u;
}
@Override
public List<User> queryUser() {
return userMapper.selectUser();
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 创建事务管理器对象 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<!-- <tx:annotation-driven/> -->
<!-- xml的形式声明事务 -->
<tx:advice id="txAdvise" transaction-manager="transactionManager">
<tx:attributes>
<!-- add开头方法配置事务 -->
<tx:method name="add*" />
<!-- insert开头方法配置事务 -->
<tx:method name="insert*" />
<!-- save开头方法配置事务 -->
<tx:method name="save*" />
<!-- query开头方法配置事务 -->
<tx:method name="query*" read-only="true"/>
<!-- select开头方法配置事务 -->
<tx:method name="select*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--进行AOP事务织入 -->
<aop:config>
<aop:pointcut expression="execution(* com.mrt.service.impl.*.*(..))" id="pc"/>
<!-- 织入 -->
<aop:advisor advice-ref="txAdvise" pointcut-ref="pc"/>
</aop:config>
</beans>
使用XML形式配置事务,则方法必须遵循配置的规范
事务的传播行为:
/**
* 事务传播行为
* propagation :
* Propagation.REQUIRED(默认) : 若当前存在事务 则在当前事务中执行 多个操作共用同一个事务
* Propagation.SUPPORTS : 若当前有事务则在事务中执行,若没有事务则在非事务的情况下执行
* Propagation.MANDATORY : 必须在事务中执行 若当前没有事务则会发生异常
* REQUIRES_NEW : 必须新建一个事务,若当前有事务则当前事务挂起,执行自己事务 必须 使用 JtaTransactionManager事务管理器
* PROPAGATION_NEVER : 不适用任何事务,若存在事务则异常
* Propagation.NESTED : 若存在活动的事务,则运行在一个嵌套的事务中。若没有活动事务,则新建一个。仅仅支持 DataSourceTransactionManager 事务管理器
* 外部事务可以回滚内部事务
* Propagation.NOT_SUPPORTED:总是非事务地执行,并挂起任何存在的事务,需要使用:JtaTransactionManager 事务管理器
*/