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 ...
随机推荐
- Nginx Location规则
Nginx由内核和模块组成,其中内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端的请求映射到一个location block,而location是Nginx配置中的一个指令 ...
- Java学习01-使用maven插件tomcat搭建web maven项目
我使用社区版的IDEA,社区版的没有tomcat插件,只能使用maven插件进行tomcat的使用了,下面进入正题 一.pom.xml配置tomcat插件 <build> <fina ...
- 配置文件加载位置与多profile文件
一. 我们在编写配置文件时,文件名可以是: application-{profile}.properties 例如:我们有几个配置文件对应的是项目不同时期的配置文件 1.application-sit ...
- easygui _1
GUI---图形用户界面 什么是GUI? GUI是Graphical User Interface(图形用户界面)的缩写.在GUI中,并不是键入文本和返回值,用户可以看到文本框,窗口,按钮等图形 ...
- 【NOIP2016提高组复赛day2】天天爱跑步
题目 小 C 同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏. <天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一棵 ...
- springboot使用外部application.properties配置文件
一.背景介绍 springboot默认的application.properties文件只能在项目内部,如果打成docker镜像后配置文件也打进去了,这样每次需要改动配置(比如数据库的连接信息)就需要 ...
- Linux入门培训教程 常见linux命令释义
快到中午吃饭了,然后忽然想起来samba里面没有添加用户.于是乎,就玩弄起了samba. Samba三下五除二就安装好了,想想window里面不断的点击下一步,还要小心提防各种隐藏再角落里的绑定软件. ...
- #419 Div2 Problem B Karen and Coffee (统计区间重叠部分 && 前缀和)
题目链接 :http://codeforces.com/contest/816/problem/B 题意 :给出 n 表示区间个数,限定值 k 以及问询次数 q,当一个数被大于或等于 k 个区间重复覆 ...
- 题解 CF1190B 【Tokitsukaze, CSL and Stone Game】
思路: 首先题目告诉我们,一次只能删去一个石子.当然有翻译时会注意,但是看英文题时总是容易忽略.. 先排序. 然后,你会发现,有些情况是一开始就输的,具体情况如下: 有两个 两个相等非零数.(a[x] ...
- HDU 2923 Relocation(状压dp+01背包)
题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...