首页 > 其他分享 >11-SSM_Spring_整合JDBC

11-SSM_Spring_整合JDBC

时间:2022-11-03 22:34:59浏览次数:48  
标签:11 事务 JDBC www springframework SSM team org schema


目录

​​四,Spring整合JDBC​​

​​1,使用spring-jdbc操作数据库​​

​​1.1 简单测试JdbcTemplate​​

​​1.2  通过spring配置的方式实现数据源配置​​

​​1.3 查询​​

​​2,Spring的事务管理​​

​​2.1 事务管理器接口PlatformTransactionManager​​

​​2.2 事务定义接口TransactionDefinition​​

​​2.3 声明式事务控制​​

​​2.4 使用案例@Transactional​​

​​2.5 注解方式实现事务​​


 

四,Spring整合JDBC

1,使用spring-jdbc操作数据库

学习使用JdbcTemplate API和 如何使用Spring管理 JdbcTemplate。

这里使用的MySQL数据库版本为5.7.31

1.1 简单测试JdbcTemplate

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>com.xxy</groupId>
<artifactId>Spring04-sql</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!--spring 核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</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.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

2,测试能否正常连接数据库

public class Test01 {
@Test
public void test01 () throws PropertyVetoException {
// 创建数据源
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");// !!!这里要根据不同的数据库版本进行调整
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test01?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("2017217905");
// 使用JDBCTemplate
JdbcTemplate template = new JdbcTemplate(comboPooledDataSource);
String SQL = "insert into info(name,age) value(?,?)";
int update = template.update(SQL, "甘雨", 18);
System.out.println("数据插入结果:" + update);
}
}

11-SSM_Spring_整合JDBC_后端

11-SSM_Spring_整合JDBC_java_02

1.2  通过spring配置的方式实现数据源配置

3,创建dao

// 这里要继承父类JdbcDaoSupport(org.springframework.jdbc.core.support.JdbcDaoSupport)
public class InfoDao extends JdbcDaoSupport {
public void insert() {
String SQL = "insert into info(name,age) value(?,?)";
// 这可以直接调用父类中的getJdbcTemplate方法
int update = this.getJdbcTemplate().update(SQL, "甘雨", 18);
}
}

4,添加applications.xml配置,通过spring依赖注入实现infoDao、jdbcTemplate、dataSource

通过ref属性环环相扣:infoDao-》jdbcTemplate-》dataSource

<?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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">


<!--创建jdbcTemplate数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test01?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false"></property>
<property name="user" value="root"></property>
<property name="password" value="2017217905"></property>
</bean>
<!--创建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="infoDao" class="com.xxy.dao.InfoDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

</beans>

5,测试

@Test
public void test02 () {
ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
InfoDao infoDao = (InfoDao) ac.getBean("infoDao");
infoDao.insert();
}

 

11-SSM_Spring_整合JDBC_spring_03

1.3 查询

增删改的流程和上面的案例类似。

1,查询单个结果queryForObject

public Team findById(int id){
String sql="select * from team where tid=?";
return this.getJdbcTemplate().queryForObject(sql, new Object[]{id}, new RowMapper<Team>() {
@Override
public Team mapRow(ResultSet resultSet, int i) throws SQLException {
Team team=new Team();
team.setId(resultSet.getInt("tid"));
team.setName(resultSet.getString("tname"));
team.setLocation(resultSet.getString("location"));
return team;
}
});
}

测试 

@Test
public void testFindById(){
ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
TeamDao dao= (TeamDao) ac.getBean("teamDao");
Team t = dao.findById(2);
System.out.println(t);
}

2,查询多个结果query

public List<Team> findAll(){
String sql="select * from team";
return this.getJdbcTemplate().query(sql, new RowMapper<Team>() {
@Override
public Team mapRow(ResultSet resultSet, int i) throws SQLException {
Team team=new Team();
team.setId(resultSet.getInt("tid"));
team.setName(resultSet.getString("tname"));
team.setLocation(resultSet.getString("location"));
return team;
}
});
}
@Test
public void testFindAll(){
TeamDao dao= (TeamDao) ac.getBean("teamDao");
List<Team> all = dao.findAll();
for (Team team : all) {
System.out.println(team);
}
}

可以看出,查询单个和查询多个时重写的mapRow方法内容是一致的,因此可以将这些重复的部分提取出来,单独作为一个方法。

/**
* 自己封装一个处理结果的方法
* @param resultSet
* @return
* @throws SQLException
*/
public Team handlResult(ResultSet resultSet) throws SQLException {
Team team=new Team();
team.setId(resultSet.getInt("tid"));
team.setName(resultSet.getString("tname"));
team.setLocation(resultSet.getString("location"));
return team;
}

public List<Team> findAll(){
String sql="select * from team";
return this.getJdbcTemplate().query(sql, new RowMapper<Team>() {
@Override
public Team mapRow(ResultSet resultSet, int i) throws SQLException {
return handlResult(resultSet);
}
});
}

3,查询聚集函数

public int getCount(){
String sql="select count(tid) from team";
return this.getJdbcTemplate().queryForObject(sql,Integer.class);
}

public Map<String, Object> getMany(){
String sql="select max(tid),min(tid) from team";
return this.getJdbcTemplate().queryForMap(sql);
}
@Test
public void testGet(){
TeamDao dao= (TeamDao) ac.getBean("teamDao");
int count = dao.getCount();
System.out.println("查询的总行数:"+count);

Map<String, Object> many = dao.getMany();
Set<Map.Entry<String, Object>> entries = many.entrySet();
for (Map.Entry<String, Object> entry : entries) {
System.out.println(entry.getKey()+"----"+entry.getValue());
}
}

2,Spring的事务管理

事务原本是数据库中的概念,在 Dao 层。但在实际开发中,一般将事务提升到业务层,即 Service 层。

这样做是为了能够使用事务的特性来管理具体的业务。

2.1 事务管理器接口PlatformTransactionManager

事务管理器是 PlatformTransactionManager 接口对象。其主要用于完成事务的提交、回滚,及获取事务的状态信息。

11-SSM_Spring_整合JDBC_后端_04

PlatformTransactionManager 接口常用的实现类:

选中 PlatformTransactionManager快捷键【Ctrl+H】,可以查看实现该接口的方法

11-SSM_Spring_整合JDBC_spring_05

DataSourceTransactionManager:使用 JDBC 或 MyBatis 进行数据库操作时使用。

进入DataSourceTransactionManager中,快捷键【Alt+7】查看类中所有方法

11-SSM_Spring_整合JDBC_sql_06

Spring 事务的默认回滚方式是:发生运行时异常和 error 时回滚,发生受查(编译)异常时提交。不过,对于受查异常,程序员也可以手工设置其回滚方式。 

2.2 事务定义接口TransactionDefinition

事务定义接口 TransactionDefinition 中定义了事务描述相关的三类常量:事务隔离级别、事务传播行为、事务默认超时时限,及对它们的操作。


1,事务隔离级别常量

这些常量均是以 ISOLATION_开头。即形如 ISOLATION_XXX。 

  • DEFAULT:采用 DB 默认的事务隔离级别。MySql 的默认为 REPEATABLE_READ; Oracle默认为READ_COMMITTED。 
  • READ_UNCOMMITTED:读未提交。未解决任何并发问题。
  • READ_COMMITTED:读已提交。解决脏读,存在不可重复读与幻读。
  • REPEATABLE_READ:可重复读。解决脏读、不可重复读,存在幻读
  • SERIALIZABLE:串行化。不存在并发问题。

2,事务传播行为常量

所谓事务传播行为是指,处于不同事务中的方法在相互调用时,执行期间事务的维护情况。如,A 事务中的方法 doSome()调用 B 事务中的方法 doOther(),在调用执行期间事务的维护情况,就称为事务传播行为。事务传播行为是加在方法上的。

事务传播行为常量都是以 PROPAGATION_ 开头,形如 PROPAGATION_XXX。

Propagation.REQUIRED(增、删、改常用)

当前没有事务的时候,就会创建一个新的事务;如果当前有事务,就直接加入该事务,比较常用的设置

Propagation.SUPPORTS(查询常用)

支持当前事务,如果当前有事务,就直接加入该事务;当前没有事务的时候,就以非事务方式执行

Propagation.MANDATORY

支持当前事务,如果当前有事务,就直接加入该事务;当前没有事务的时候,就抛出异常

Propagation.REQUIRES_NEW

创建新事务,无论当前是否有事务都会创建新的

PROPAGATION_NESTED
PROPAGATION_NEVER
PROPAGATION_NOT_SUPPORTED

......


3,默认事务超时时限

常量 TIMEOUT_DEFAULT 定义了事务底层默认的超时时限,sql 语句的执行时长。

注意,事务的超时时限起作用的条件比较多,且超时的时间计算点较复杂。所以,该值一般就使用默认值即可。

2.3 声明式事务控制

Spring提供的对事务的管理,就叫做声明式事务管理。

如果用户需要使用spring的声明式事务管理,在配置文件中配置即可:不想使用的时候直接移除配置。

这种方式实现了对事务控制的最大程度的解耦。

声明式事务管理,核心实现就是基于AOP

Spring中提供了对事务的管理。开发者只需要按照spring的方式去做就行。

事务必须在service层统一控制。

事务的粗细粒度:

  • 细粒度:对方法中的某几行的代码进行开启提交回滚;
  • 粗粒度:对整个方法进行开启提交回滚;

Spring中的aop只能对方法进行拦截,所有我们也就针对方法进行事务的控制。

如果只有单条的查询语句,可以省略事务;如果一次执行的是多条查询语句,例如统计结果、报表查询,必须开启事务。

2.4 使用案例@Transactional

1,编写service方法,添加@Transactional注解

@Transactional 属性 说明: 

readOnly:是否只读 

rollbackFor={Exception.class}: 遇到什么异常会回滚 

propagation事务的传播: 

  • Propagation.REQUIRED:当前没有事务的时候,就会创建一个新的事务;如果当前有事务,就直 接加入该事务,比较常用的设置 
  • Propagation.SUPPORTS:支持当前事务,如果当前有事务,就直接加入该事务;当前没有事务的 时候,就以非事务方式执行 
  • Propagation.MANDATORY:支持当前事务,如果当前有事务,就直接加入该事务;当前没有事务的 时候,就抛出异常 
  • Propagation.REQUIRES_NEW:创建新事务,无论当前是否有事务都会创建新的 

isolation=Isolation.DEFAULT:事务的隔离级别:默认是数据库的隔离级别 

@Service
public class TeamService {

@Autowired
private TeamDao teamDao;

@Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
public int insert(Team team){
int num1=teamDao.insert(team);
System.out.println("第一条执行结果:num1="+num1); // 这一条可以执行成功
System.out.println(10/0); // 出现异常,回滚
int num2=teamDao.insert(team);
System.out.println("第二条执行结果:num2="+num2);
return num2+num1;
}
}

2,在application.xml中添加配置

在前面配置的基础上添加xmlns:tx、xsi:schemaLocation、transactionManager对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">

<context:component-scan base-package="com.kkb"/>
<!--创建 JdbcTemplate 的数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://192.168.58.128:3306/springJDBC?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--创建JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="teamDao" class="com.kkb.dao.TeamDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

</beans>

测试

@Test
public void test01(){
ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
TeamService teamService = (TeamService) ac.getBean("teamService");
int num=teamService.insert(new Team("凯尔特人","波士顿"));
System.out.println(num);
}

2.5 注解方式实现事务

在配置文件中对方法进行拦截,这样就无需同类型的方法添加同样的注解

1,修改配置(先将TeamService中的事务注解删除),添加以下配置

<tx:annotation-driven transaction-manager="transactionManager" />

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="pt" expression="execution(* com.kkb.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
</aop:config>

2,如果报出下面的错误,表示缺少一个依赖的jar包

11-SSM_Spring_整合JDBC_sql_07

在pom.xml中添加依赖

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>

标签:11,事务,JDBC,www,springframework,SSM,team,org,schema
From: https://blog.51cto.com/u_15849465/5821267

相关文章

  • 2022.11.3 闲话
    想不到博主更新了?[Warning]流水账警告。今天复健了某军事博弈软件(还是不要明说为好),终于直到之前学长们为什么可以研究这么深刻了/hanx不过没有研究太久,只有半个小时左......
  • c++11 unique_ptr
    unique_ptr使用详解      ......
  • 【2022.11.3】luffy项目前期部署(1)
    内容概要1.企业项目类型2.企业项目开发流程3.路飞项目需求4.pip换源5.虚拟环境搭建5.1使用pytharm创建虚拟环境5.2通用方案创建虚拟环境6.luffy后台创建目录......
  • 闲话 22.11.3
    闲话好今日得到最让人感到奇妙的一件事是松鼠加入ps_plive力虽然但是……比较地奇妙……不知道该评价什么(连步玎都不是ps_p的(所以给了一天自由练习的时间就是刷......
  • 2022-11-3学习内容
    1.在存储卡上读写图片文件1.1activity_image_write.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/an......
  • 11.3 论文学习笔记
      这周看了一篇论文,该篇论文发自2022的coling,论文的题目为:ArgLegalSumm:ImprovingAbstractiveSummarizationofLegal DocumentswithArgumentMining,即利用论点......
  • 【计算机考研】从N非本科到考研上岸一线211的心路历程
    大家好,我是亓官劼(qíguānjié)。在【亓官劼】公众号xGitHub、B站、华为开发者论坛等平台分享一些技术博文,时光荏苒,未来可期,一起加油~更多精彩文章可以关注微信公众号【亓......
  • 11 Ceph 集群测试
    目录mon高可用测试模拟mon节点宕掉一个mon节点宕机后,测试集群业务情况宕掉集群中2个mon节点测试业务是否正常集群mon服务恢复mds主从切换查看集群状态信息手动模......
  • 11.03
    今日内容1.动静态方法2.面向对象之继承的概念3.继承的本质4.名字的查找顺序5.经典类与新式类6.派生方法1.动静态方法在类中定义的函数有多种特性classStudent:......
  • 11月3日内容总结——对象之动静态方法、继承及相关知识点、类中名称查找顺序及经典类
    目录一、动静态方法动态方法静态方法二、面向对象之继承的概念面向对象三大特性1.继承的含义2.继承的目的3.继承解决了什么问题4.多继承的优缺点5.继承的实操三、继承的本......