Spring、整合Spring+JDBC
首先引入Spring包和JDBC所使用到的包:
配置beans.xml步骤:
1.配置dataSource的属性
2.将DataSource交给DataSourceTransactionManager管理
3.开启事务开关
4.配置JdbcTemplate工具类
5.将jdbcTemplate注入到PersonServiceImpl中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 如果使用.properties 属性文件的方式加载配置项的话,使用context:property-placeholder加载 -->
<!-- <context:property-placeholder location="classpath:jdbc.properties"/> --> <!-- 1.配置dataSource的属性 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/springjdbc?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialSize" value="1" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxActive" value="500" />
<!--最大空闲值,当经过一段峰值以后,连接会被释放掉一部分,直到释放到maxidle -->
<property name="maxIdle" value="2" />
<!--最小空闲值 当空闲的连接数小于阀值时,连接池会主去的去申请一些连接 -->
<property name="minIdle" value="1" />
</bean>
<!-- 2.将DataSource交给DataSourceTransactionManager管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 3.标明采用注解的方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 4.配置JdbcTemplate工具类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 5.将jdbcTemplate注入到PersonServiceImpl中 -->
<bean id="personIService" class="cn.service.impl.PersonServiceImpl">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
</beans>
配置PersonServiceImpl:
public interface PersonIService {
void save(Person person);
void update(Person person);
Person getById(int id);
List<Person> getPersons();
void delete(int id);
}
@Transactional
public class PersonServiceImpl implements PersonIService { private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} @Override
public void save(Person person) {
jdbcTemplate.update("insert into person(name) values(?)"
,new Object[]{person.getName()}
, new int[]{java.sql.Types.VARCHAR});
} @Override
public void update(Person person) {
jdbcTemplate.update("update person set name=? where id=?"
,new Object[]{person.getName(),person.getId()}
, new int[]{ java.sql.Types.VARCHAR,java.sql.Types.INTEGER});
}
@Override
public void delete(int id ) {
jdbcTemplate.update("delete from person where id=?"
,new Object[]{id}
, new int[]{java.sql.Types.INTEGER});
} @Override
public Person getById(int id) {
return (Person)jdbcTemplate.queryForObject("select * from person where id=?"
, new Object[]{id}, new PersonRowMapper());
} @Override
public List<Person> getPersons() {
return (List<Person>)jdbcTemplate.query("select * from person"
, new Object[]{}, new PersonRowMapper());
} }
public class PersonRowMapper implements RowMapper {
@Override
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
Person person=new Person(rs.getString("name"));
person.setId(rs.getInt("id"));
return person;
}
}
测试代码:
public class test {
private PersonIService personIService;
@org.junit.Before
public void init(){
ApplicationContext cxt=new ClassPathXmlApplicationContext("beans.xml");
personIService=(PersonIService)cxt.getBean("personIService");
}
@Test
public void test() {
personIService.save(new Person("但丁"));
}
@Test
public void test2() {
Person person=personIService.getById(1);
System.out.println(person.getName());
}
@Test
public void test3() {
Person person=personIService.getById(1);
System.out.println(person.getName());
person.setName("克路丝");
personIService.update(person);
}
@Test
public void test4() {
personIService.delete(1);
}
@Test
public void test5() {
List<Person> list=personIService.getPersons();
for(Person per : list){
System.out.println(per.getName());
}
}
}
Spring、整合Spring+JDBC的更多相关文章
- spring 整合 spring mvc
需要进行 Spring 整合 SpringMVC 吗 ? 还是否需要再加入 Spring 的 IOC 容器 ? 是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 Context ...
- Spring整合jdbc
首先web.xml文件跟往常一样,加载spring容器和加载org.springframework.web.context.ContextLoaderListener读取applicationCont ...
- JAVAEE——spring03:spring整合JDBC和aop事务
一.spring整合JDBC 1.spring提供了很多模板整合Dao技术 2.spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术. JDBCTemplate => JDBC模 ...
- Spring整合JDBC及事务处理
1.Spring整合JDBC DAO是数据访问对象(data access object)的简写.接口是实现松耦合的关键,Spring也鼓励使用接口,但不是强制的. 捕获异常时希望能尝试从异常状态中恢 ...
- Spring整合JDBC以及AOP管理事务
本节内容: Spring整合JDBC Spring中的AOP管理事务 一.Spring整合JDBC Spring框架永远是一个容器,Spring整合JDBC其实就是Spring提供了一个对象,这个对象 ...
- Spring整合JDBC实现简单的增删改
Spring整合JDBC实现简单的增删改: 1.导入Spring的包和数据库的驱动包: 2.选择一个数据源(dbcp和C3P0) 3.导入数据源的包(这里我们使用dbcp) <span styl ...
- Spring知识点总结(五)Spring整合JDBC
1. 回顾JDBC a. java操作关系型数据的API.导入相关数据库的驱动包后可以通过JDBC提供的接口来操作数据库. b. 实现JDBC的六个步骤 ...
- Spring整合JDBC和Druid连接池
我的博客名为黑客之谜,喜欢我的,或者喜欢未来的大神,点一波关注吧!顺便说一下,双十二快到了,祝大家双十二快乐,尽情的买买买~ 如果转载我的文章请标明出处和著名,谢谢配合. 我的博客地址为: https ...
- Spring整合JDBC temple
一.Spring对Jdbc的支持 Spring为了提供对Jdbc的支持,在Jdbc API的基础上封装了一套实现,以此建立一个 JDBC 存取框架. 作为 Spring JDBC 框架的核心, JDB ...
- Spring整合JDBC(连接池、JDBC模板、Dao配置到Spring容器、配置文件的优化)
1.Spring整合JDBC (1)导包(共12个): c3p0连接池.JDBC驱动(4个) Spring-jdbc.Spring-tx事务(2个) (2)JDBC模板对象(JDBCTemplate) ...
随机推荐
- 微软云基础架构Hyper-scale Datacenter
每天醒来,可能很多人的习惯都是打开手机,看看微信,刷刷朋友圈,或者看看新闻,去咖啡店,打开电脑搜索一些关键字,观看视频,电视剧--可是你有没有想过你每一次键盘的敲击,每一次微信的语音的发送,数据会流向 ...
- Windows下Node.js开发环境搭建
1.http://nodejs.org/下载node.js运行环境安装 2.打开DOS命令行 .安装express框架 1 >npm install express 末尾显示如下为安装成功 .安 ...
- Excel导入到DataTable
1.前台代码 <asp:FileUpload ID="fupFiles" runat="server" /> <asp:Button ID=& ...
- MEMS市场介绍
惠普第一.德州仪器第二 市场观察发展报告说,MEMS市场在2007年增长百分之九,达到70亿美元,其中前30名制造商的收入总和有56亿美元,平均增长7个百分点. 惠普(HP)打印机使用MEMS喷墨头, ...
- 解决未能启动服务“VMware Authorization Service”
计算机-管理-服务--服务列表找到VMware Authorization Service 并双击 打开服务.
- 国内外DNS服务器地址列表
DNS(Domain Name System)是域名解析服务器的意思,它在互联网的作用是把域名转换成为网络可以识别的IP地址.目前国内电信运营商通过使用DNS劫持的方法,干扰用户正常上网,使得用户无法 ...
- 第九十六题(编写strcpy 函数)
96.08 年中兴校园招聘笔试题 1.编写strcpy 函数 已知strcpy 函数的原型是 char *strcpy(char *strDest, const char *strSrc); 当中st ...
- Ubuntu12.04下jamvm1.5.4+classpath-0.98成功执行 helloworld.class
经过两天的努力,总于在ubuntu以下编译好classpath-0.98与jamvm1.5.4,并能成功的运行类文件:jamvm hellowold,当屏幕上打印出"hello world! ...
- 调用Response.Redirect 捕获异常 解决办法(摘抄)
如果使用 Response.End.Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常.您可以使用 try-catch ...
- js方法在对象中的状态
在C#中,只有对象的字段存储在堆中,而方法则存储在一个方法表中.当实例化多个对象时,为字段分配了内存空间,而方法都指向一个方法表中的同一个方法. 如 而在JS中,字段和方法都属于值类型,都存储在堆中. ...