一、用Java配置的方式

1、实体类:

Role

public class Role {
private int id;
private String roleName;
private String note; @Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", note='" + note + '\'' +
'}';
} public Role() {
} public Role(int id, String roleName, String note) {
this.id = id;
this.roleName = roleName;
this.note = note;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getRoleName() {
return roleName;
} public void setRoleName(String roleName) {
this.roleName = roleName;
} public String getNote() {
return note;
} public void setNote(String note) {
this.note = note;
}
}

2、接口

RoleService

3、实现类

RoleServiceImpl
package com.wbg.springtransaction.service;

import com.wbg.springtransaction.config.JavaConfig;
import com.wbg.springtransaction.entity.Role;
import org.apache.ibatis.session.SqlSessionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition; import javax.sql.DataSource;
import javax.transaction.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; @Service
public class RoleServiceImpl implements RoleService {
@Autowired
DataSource dataSource;
@Autowired
private PlatformTransactionManager transactionManager = null;
@Override
public List<Role> listAll() {
List<Role> list = new ArrayList<Role>();
Connection connection = null;
try {
connection = dataSource.getConnection(); } catch (SQLException e) {
e.printStackTrace();
}
String sql = "select * from role";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
Role role = null;
while (resultSet.next()) {
role = new Role(
resultSet.getInt(1),
resultSet.getString(2),
resultSet.getString(3)
);
list.add(role);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
} public int insert(Role role) {
Connection connection = null;
DefaultTransactionDefinition dtd = new DefaultTransactionDefinition();
dtd.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus ts = transactionManager.getTransaction(dtd);
String sql = "insert into role(role_name,note) values(?,?)";
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, role.getRoleName());
preparedStatement.setString(2, role.getNote());
preparedStatement.executeUpdate();
transactionManager.commit(ts);
} catch (SQLException e) {
transactionManager.rollback(ts);
System.out.println("原因:" + e.getMessage());
}
return 0;
}
}

4、配置:

JavaConfig

@Configuration
@ComponentScan("com.wbg.springtransaction.*")
@EnableTransactionManagement
public class JavaConfig implements TransactionManagementConfigurer { @Bean(name = "dataSource")
public DataSource getDataSource() {
ComboPooledDataSource dataSource=new ComboPooledDataSource();
try {
dataSource.setDriverClass("org.mariadb.jdbc.Driver");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
dataSource.setJdbcUrl("jdbc:mariadb://localhost:3306/wbg_logistics");
dataSource.setUser("root");
dataSource.setPassword("123456");
dataSource.setMaxPoolSize(30);
return dataSource;
} @Override
@Bean("transactionManager")
public PlatformTransactionManager annotationDrivenTransactionManager() {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(getDataSource());
return transactionManager;
}
}

5、测试:

 public static void main(String[] args) throws SQLException {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfig.class);
RoleService roleService = applicationContext.getBean(RoleService.class);
roleService.insert(new Role(1,"asd","ss"));
for (Role role : roleService.listAll()) {
System.out.println(role);
}
}

二、用xml进行配置:

1、创建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/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="org.mariadb.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mariadb://localhost:3306/wbg_logistics" />
<property name="user" value="root" />
<property name="password" value="123456" /> <property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<property name="autoCommitOnClose" value="false" />
<property name="checkoutTimeout" value="10000" />
<property name="acquireRetryAttempts" value="2" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>

2、配置:

@Configuration
@ComponentScan("com.wbg.springtransaction.*")
@EnableTransactionManagement
@ImportResource({"classpath:spring-cfg.xml"})
public class JavaConfig implements TransactionManagementConfigurer {
}

3、实现类RoleServiceImpl不变

4、测试:

 public static void main(String[] args) throws SQLException {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfig.class);
RoleService roleService = applicationContext.getBean(RoleService.class);
roleService.insert(new Role(1,"asd","ss"));
for (Role role : roleService.listAll()) {
System.out.println(role);
}
}

最新文章

  1. XML编程知识点总结
  2. 网络流-最大流问题 ISAP 算法解释(转自Renfei Song&#39;s Blog)
  3. vim使用01
  4. IIS上发布WCF发布服务,访问不到
  5. Android添加标题进度条
  6. hdu 1811 Rank of Tetris
  7. phpmyadmin上传较大sql文件
  8. JSP进阶 之 SimpleTagSupport 开发自定义标签
  9. stun/turn/ice学习笔记
  10. 神州数码HSRP(热备份路由协议)配置
  11. 解决.NET Web API生成的Help无Controller说明&amp;服务端接收不到请求
  12. ue4 笔记
  13. e.getMessage 为空NULL
  14. scala变量类型和性质
  15. iOS开发手记-iOS8中使用定位服务解决方案
  16. 安装MySQL-python时报错
  17. Python学习-5.Python的变量与数据类型及字符串的分割与连接
  18. 服务端接收不到ajax post请求的参数
  19. Java容器-引用数据类型排序+TreeSet、TreeMap底层实现
  20. 关于iframe里的子页面如何调取父级页面里的事件(子调父)

热门文章

  1. redis(4)事务
  2. C Primer Plus note9
  3. php从身份证获取性别和出生年月
  4. laravel5.7 表单验证
  5. Hibernate 一对多,多对多,多对一检索策略
  6. csharp:Convert Image to Base64 String and Base64 String to Image
  7. git自动更新网站代码
  8. cf1043C. Smallest Word(贪心)
  9. vue的事件
  10. roadflow asp.net core版工作流引擎更新发布