Spring + JDBC 组合开发集成步骤
1:配置数据源,如:
<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-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="1"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean> </beans>
这一步完成之后我们可以进行数据的增删改查操作啦。
注意使用到的Jar包

写实体bean, 以及servive 层, 然后再xml中配置bean 以及为bean配置数据源;
<bean class="cn.gbx.service.impl.PersonServiceImpl" id="personServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
package cn.gbx.service.impl; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import cn.gbx.bean.Person;
import cn.gbx.service.IPersonService;
@Transactional
public class PersonServiceImpl implements IPersonService { private JdbcTemplate jdbcTemplate; //通过DI将数据源注入给jdbcTemplate
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
} @Override
public void save(Person person) {
jdbcTemplate.update("insert into t_person(name) values(?)", new Object[] {person.getName()}, new int[] {java.sql.Types.VARCHAR}); } @Override
public void delte(Person person) {
jdbcTemplate.update("delete from t_person where id = ?", new Object[] {person.getId()}, new int[] {java.sql.Types.INTEGER});
} @Override
public void update(Person person) { jdbcTemplate.update("update t_person set name = ? where id = ?", new Object[] {person.getName(), person.getId()},
new int[]{java.sql.Types.VARCHAR, java.sql.Types.VARCHAR}
); } @Override
public Person getPerson(Integer personid) {
Person person = (Person)jdbcTemplate.queryForObject("select * from t_person where id = ?", new Object[] {personid},
new int[] {java.sql.Types.INTEGER} , new PersonHandler());
return person;
} @Override
public List<Person> getPersons() {
List<Person> persons = null;
persons = jdbcTemplate.query("select * from t_person", new PersonHandler());
return persons;
} }
2:配置事务。配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式。
在spring配置文件中引入用于声明事务的tx命名空间
采用注解方式配置事务:
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!– 采用@Transactional注解方式使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
@Service @Transactional
public class PersonServiceBean implements PersonService {
}
采用基于XML方式配置事务
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* cn.itcast.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
Spring + JDBC 组合开发集成步骤的更多相关文章
- (转)Spring+JDBC组合开发
http://blog.csdn.net/yerenyuan_pku/article/details/52882435 搭建和配置Spring与JDBC整合的环境 使用Spring+JDBC集成步骤如 ...
- Spring笔记——Spring+JDBC组合开发
使用Spring+JDBC集成步骤如下: 1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...
- SpringMVC笔记——Spring+MyBatis组合开发简单实例
简介 SSH框架很强大,适合大型项目开发.但学无止境,多学会一门框架组合开发会让自己增值许多. SSM框架小巧精致,适合中小型项目快速开发,对于新手来说也是简单上手的.在SSM框架搭建之前,我们先学习 ...
- Spring JDBC数据库开发
针对数据库操作,Spring框架提供了JdbcTemplate类. 1.Spring JDBC的配置 创建配置文件applicationContext.xml,添加如下代码: <!--配置数据源 ...
- 开涛spring3(7.5) - 对JDBC的支持 之 7.5 集成Spring JDBC及最佳实践
7.5 集成Spring JDBC及最佳实践 大多数情况下Spring JDBC都是与IOC容器一起使用.通过配置方式使用Spring JDBC. 而且大部分时间都是使用JdbcTemplate类(或 ...
- 完整java开发中JDBC连接数据库代码和步骤[申明:来源于网络]
完整java开发中JDBC连接数据库代码和步骤[申明:来源于网络] 地址:http://blog.csdn.net/qq_35101189/article/details/53729720?ref=m ...
- 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析
Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...
- (转)Spring4.2.5+Hibernate4.3.11组合开发
http://blog.csdn.net/yerenyuan_pku/article/details/52887573 搭建和配置Spring与Hibernate整合的环境 今天我们来学习Spring ...
- Spring(五)Spring与Web环境集成
MVC 是 Model.View 和 Controller 的缩写,分别代表 Web 应用程序中的 3 种职责. 模型:用于存储数据以及处理用户请求的业务逻辑. 视图:向控制器提交数据,显示模型中的数 ...
随机推荐
- history and its relevant variables in Linux/GNU and Mac OS history命令以及相关环境变量
对于Terminalor们,history命令并不陌生,什么!n, !!更是很常用的,而且您在命令行敲的cmds是默认保存在/home/$USER/.bash_history(linux) /User ...
- 华硕本本重装系统后出现can not open file c:\RECOVERY.DAT
华硕本本重装系统后出现can not open file c:\RECOVERY.DAT很多网友会觉得困惑,不知道为什么会这样,下面我就为大家来解决这个问题,方法一: 这个问题就出在华硕自带系统都是装 ...
- cut DEMO
分割后得到列: cat /etc/passwd|head -n 5 | cut -d : -f 1,6
- preparedStatement和Statement 有什么不一样
1. PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象. 2.作 ...
- HDU 4707:Pet
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 20150916_001 vba 基础
一.什么是“宏”.“宏”有什么用 关于“宏”的详细定义,可以参考百度百科的解释(点击查看).我给它一个简单的或许不太严谨的定义: 宏的通俗定义:宏是被某些软件所能识别.理解并执行的特定代码/脚本. 宏 ...
- Wormholes 分类: POJ 2015-07-14 20:21 21人阅读 评论(0) 收藏
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35235 Accepted: 12861 Descr ...
- C语言typeof详解 offsetof
http://blog.chinaunix.net/uid-28458801-id-4200573.html 前言: typeof关键字是C语言中的一个新扩展,这个特性在linux内核中应用非常 ...
- 2016年11月14日 星期一 --出埃及记 Exodus 20:5
2016年11月14日 星期一 --出埃及记 Exodus 20:5 You shall not bow down to them or worship them; for I, the LORD y ...
- windows下的mysql客户端mysqlworkbench链接虚拟机上CentOS的mysql服务器
本人在虚拟机上CentOS的Linux环境下安装了mysql服务器,在本地Windows下安装了mysql的客户端mysqlworkbench ,所以就想让windows下的mysql客户端mysql ...