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 ...
随机推荐
- Codeforces 982 树边两端点计数偶数连通块 鲨鱼活动最小K最大location 扩展欧几里得方块内光线反射
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- SQL代码
SELECT SCHEMA_NAME(SCHEMA_ID)AS ID,name as Table_name FROM sys.tables;--查询表视图 查询表视图
- canvas实现圆角图片 (处理原图是长方形或正方形)
/** * 生成图片的缩略图 * @param {[type]} img 图片(img)对象或地址 * @param {[type]} width 缩略图宽 * @param {[type]} hei ...
- Python---webserver
一. # HTTP项目实战 - 深入理解HTTP协议 - 模拟后台服务程序基本流程和大致框架 - 每一个步骤一个文件夹 - 图解http协议,图解tcp/ip协议 # v01-验证技术 - 验证soc ...
- 如何导出不带.svn的文件夹
在工作环境中,有的时候需要将本地SVN服务器中的文件导出来,提交到另一个SVN服务器中去(比如做现场开发时,由于外网速度慢,项目组内部往往使用一个SVN服务器,但又同时又需要公司统一管理,定期提交到公 ...
- CSS布局之flexbox
参考链接: https://www.cnblogs.com/qingchunshiguang/p/8011103.html 练习代码 <!DOCTYPE html> <html la ...
- ideal 工具jdk环境配置
1.File >> Other Settings >> Default Project Structure ... 2.Project >> jdk_vie ...
- 创建ThreadFactory实例的多种方式
spring的CustomizableThreadFactory guava的MoreExecutors.platformThreadFactory()静态方法 guava的ThreadFactory ...
- 箱排序(Bin Sort)
1.基本思想 排序过程无须比较关键字,而是通过"分配"和"收集"过程来实现排序.它们的时间复杂度可达到线性阶:O(n). 箱排序也称桶排序(Bucket Sor ...
- 【Dart学习】-- Dart之消息循环机制[翻译]
概述 异步任务在Dart中随处可见,例如许多库的方法调用都会返回Future对象来实现异步处理,我们也可以注册Handler来响应一些事件,如:鼠标点击事件,I/O流结束和定时器到期. 这篇文章主要介 ...