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 ...
随机推荐
- 第一节,搭建openwrt开发环境
一,安装VMware虚拟机或者VirtualBox虚拟机 安装过程就不在此赘述了.附上百度搜索来的链接,供大家参考. https://baijiahao.baidu.com/s?id=16233731 ...
- (转) Java中的负数及基本类型的转型详解
(转) https://my.oschina.net/joymufeng/blog/139952 面这行代码的输出是什么? 下面两行代码的输出相同吗? 请尝试在Eclipse中运行上面的两个代码片段, ...
- [POJ1664]放苹果(动态规划)
[POJ1664]放苹果 Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第 ...
- ZROI 19.08.01 生成函数方法
写在前面:由于我数学基础不好,加上缺乏生成函数知识,所以这一下午我都处在掉线和非掉线的叠加态.而且我写\(\LaTeX\)很慢,所以笔记相当混乱而且不全面.说白了就是我太菜了听不懂. 1.一般生成函数 ...
- python绘制国际象棋棋盘核心代码
import turtle step = 40 for i in range(8): for j in range(8): turtle.penup() turtle.goto(i*step, j*s ...
- thinkphp读取器和修改器
读取器 如果在模型中,自定义了方法,那么读取器会读取模型中自定义的方法,否则会调用默认的方法. 写入器
- pyqt-swf
# pyqt5界面打开flash.swf文件 from PyQt5 import QtCore, QtGui, QAxContainer, QtWidgets class Ui_Flash(QAxCo ...
- C# 之 数组倒叙排列
//倒叙排列 string temp=""; ; i < strlist.Length / ; i++) { temp = strlist[i]; strlist[i] = ...
- Card Game Cheater
Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- springMVC的常用注解有哪些?
1.@Controller @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否 ...