spring(二) JDBC
一、配置 bean.xml , 链接数据库。
c3p0数据库连接池
<?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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--注册类 -->
<bean id="userDao" class="com.spring_jdbc.dao.UserDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean> <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> </beans>
UserDAO.java
package com.spring_jdbc.dao;
import org.springframework.jdbc.core.JdbcTemplate; public class UserDAO {
private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} public void update(){
//第四步:通过模板进行操作
String str = "update t_user set money=money-1000 where username=?;";
jdbcTemplate.update(str,"rose");
System.out.println("操作成功。。。");
}
}
/*
//使用编码方式实现c3p0数据库连接池
private ComboPooledDataSource dataSource;
private JdbcTemplate jdbcTemplate;
//第一步:创建连接池核心工具类
ComboPooledDataSource dataSource = new ComboPooledDataSource(); //第二步:连接池,url,驱动,账号,密码,初始连接数,最大连接数
dataSource.setJdbcUrl("jdbc:mysql:///spring");//设置url
dataSource.setDriverClass("com.mysql.jdbc.Driver");//设置驱动
dataSource.setUser("root");//mysql的账号
dataSource.setPassword("root");//mysql的密码 //第三步:创建模板
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource); //第四步:通过模板进行操作
String str = "update t_user set money=money-1000 where username=?;";
jdbcTemplate.update(str,"rose");
System.out.println("操作成功。。。");
*/
service.java
package com.spring_jdbc.dao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Service { public static void main(String[] args) throws Exception {
ApplicationContext con = new ClassPathXmlApplicationContext("com/spring_jdbc/dao/UserDAO.xml");
UserDAO userDao = (UserDAO)con.getBean("userDao");
userDao.update();
}
}
事务管理
一、配置 bean.xml , 链接数据库,创建一个事务管理器。
c3p0数据库连接池
<?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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 初始化模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean> <!-- 创建数据源(连接池) -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> <bean id="service" class="com.spring_jdbc.transaction.Service">
<property name="userDao" ref="userDao"></property>
</bean> <!--注册类 -->
<bean id="userDao" class="com.spring_jdbc.transaction.UserDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!-- 创建一个事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="change*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.spring_jdbc.transaction.*.*(..))" />
</aop:config> </beans>
UserDAO.java
package com.spring_jdbc.transaction;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserDAO {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void update() throws Exception{
//第四步:通过模板进行操作
String str = "update t_user set money=money-1000 where username=?;";
jdbcTemplate.update(str,"rose");
System.out.println("操作成功。。。");
}
/**
* 转账方
* */
public void money_less(){
String str = "update t_user set money=money-? where username=?;";
jdbcTemplate.update(str,1000,"rose");
System.out.println("转出成功。。。");
}
/**
* 接收方
* */
public void money_more(){
String str = "update t_user set money=money+? where username=?;";
jdbcTemplate.update(str,1000,"jack");
System.out.println("接收成功。。。");
}
}
Service.java
package com.spring_jdbc.transaction;
public class Service {
private UserDAO userDao;
public UserDAO getUserDao() {
return userDao;
}
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
// 转账的实际操作
public void changeAccount(){
userDao.money_less();
// String s1 = null; //模拟异常。 事务回滚,数据不改变
// s1.charAt(0);
userDao.money_more();
}
}
test.java
package com.spring_jdbc.transaction; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Action {
public static void main(String[] args) { ApplicationContext con = new ClassPathXmlApplicationContext("com/spring_jdbc/transaction/UserDao.xml");
Service service = (Service)con.getBean("service");
service.changeAccount(); }
}
spring(二) JDBC的更多相关文章
- JAVAEE——spring03:spring整合JDBC和aop事务
一.spring整合JDBC 1.spring提供了很多模板整合Dao技术 2.spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术. JDBCTemplate => JDBC模 ...
- Spring的jdbc模板1
Spring是EE开发的一站式框架,有EE开发的每一层解决方案.Spring对持久层也提供了解决方案:ORM模块和jdbc模块,ORM模块在整合其他框架的时候使用 Spring提供了很多的模板用于简化 ...
- 跟着刚哥学习Spring框架--JDBC(六)
Spring的JDBC框架 Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发. Spring主要提供JDBC模板方式.关系数据库对象化方式.SimpleJdbc方式.事务管理来简 ...
- SSM 实训笔记 -11- 使用 Spring MVC + JDBC Template 实现筛选、检索功能(maven)
SSM 实训笔记 -11- 使用 Spring MVC + JDBC Template 实现筛选.检索功能(maven) 本篇是新建的一个数据库,新建的一个完整项目. 本篇内容: (1)使用 Spri ...
- Spring 开发第一步(三)Spring与JDBC
<spring in action 3rd>中的前面4章讲解的是Spring的核心,也就是DI/IOC和AOP .从第5章开始是Spring在企业开发中的各个方面的应用.其实作为笔者从事的 ...
- Spring整合JDBC以及AOP管理事务
本节内容: Spring整合JDBC Spring中的AOP管理事务 一.Spring整合JDBC Spring框架永远是一个容器,Spring整合JDBC其实就是Spring提供了一个对象,这个对象 ...
- Spring整合jdbc编程
一.Spring对Jdbc的支持 Spring为了提供对Jdbc的支持,在Jdbc API的基础上封装了一套实现,以此建立一个 JDBC 存取框架. 作为 Spring JDBC 框架的核心, ...
- Spring学习笔记(五)—— Spring整合JDBC
一.Spring对JDBC的支持 Spring提供了很多模板整合Dao技术 与JDBC的整合中,Spring中提供了一个可以操作数据库的对象——JdbcTemplate,该对象封装了JDBC技术,与D ...
- spring----AOP注解以及spring的JDBC和事务
技术分析之:Spring框架的AOP技术(注解方式) 1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开 ...
- 四、spring的JDBC模板和事务管理
Spring的JDBC模板 Spring是JavaEE开发的一站式框架,对各种持久化技术都提供了简单的模板 ORM持久化技术 模板类 JDBC org.springframework.jdbc.cor ...
随机推荐
- IBM产品系列和AIX系统版本
AIX系统版本 AIX 7.2 No supported AIX levels. AIX 7.1 Technology Level Base Level Recommended L ...
- 总结linux内核的一些参数优化
sysctl命令被用于在动态地修改内核的运行参数,可用的内核参数在目录/proc/sys中. 它包含一些TCP/IP堆栈和虚拟内存系统的高级选项, 用sysctl可以读取设置超过五百个系统变量. sy ...
- Error: unable to perform an operation on node 'rabbit@DESKTOP-6JT7D2H'. Please see diagnostics information and suggestions below.
https://blog.csdn.net/qq_32814555/article/details/79494533
- yum安装nginx服务
配置yum源 官网更新源地址:nginx 添加到 yum.repos.d 中 vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx sta ...
- 【NOIP2016提高A组五校联考4】label
题目 题目 20%算法 设\(f_{i,j}\)表示第i个节点选了j这个权值的方案数. 显然转移方程为,\[f_{i,j}=\Pi_{v=son(i)}(\sum_{k=1}^{j-k}f_{v,k} ...
- 【转】博弈论——acm
转自http://blog.csdn.net/lgdblue/article/details/15809893 序:博弈是信息学和数学试题中常会出现的一种类型,算法灵活多变是其最大特点,而其中有一类试 ...
- 原生实现ajax解析--XMLHttpRequest
ajax基础: Asynchronous JavaScript and XML,意思就是用JavaScript执行异步网络请求. 如果仔细观察一个Form的提交,你就会发现,一旦用户点击“Submit ...
- JDK中String类的源码分析(二)
1.startsWith(String prefix, int toffset)方法 包括startsWith(*),endsWith(*)方法,都是调用上述一个方法 public boolean s ...
- centos7 安装 eclipse
1.到eclipse官网下载 https://www.eclipse.org/downloads/packages/ spring 官网 https://spring.io/tools3/eclips ...
- 2018-2019-2 20165235《网络对抗技术》Exp7 网络欺诈防范
2018-2019-2 20165235<网络对抗技术>Exp7 网络欺诈防范 实验目的 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法 实验内容 (1)简单应 ...