首先引入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的更多相关文章

  1. spring 整合 spring mvc

    需要进行 Spring 整合 SpringMVC 吗 ? 还是否需要再加入 Spring 的 IOC 容器 ? 是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 Context ...

  2. Spring整合jdbc

    首先web.xml文件跟往常一样,加载spring容器和加载org.springframework.web.context.ContextLoaderListener读取applicationCont ...

  3. JAVAEE——spring03:spring整合JDBC和aop事务

    一.spring整合JDBC 1.spring提供了很多模板整合Dao技术 2.spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术. JDBCTemplate => JDBC模 ...

  4. Spring整合JDBC及事务处理

    1.Spring整合JDBC DAO是数据访问对象(data access object)的简写.接口是实现松耦合的关键,Spring也鼓励使用接口,但不是强制的. 捕获异常时希望能尝试从异常状态中恢 ...

  5. Spring整合JDBC以及AOP管理事务

    本节内容: Spring整合JDBC Spring中的AOP管理事务 一.Spring整合JDBC Spring框架永远是一个容器,Spring整合JDBC其实就是Spring提供了一个对象,这个对象 ...

  6. Spring整合JDBC实现简单的增删改

    Spring整合JDBC实现简单的增删改: 1.导入Spring的包和数据库的驱动包: 2.选择一个数据源(dbcp和C3P0) 3.导入数据源的包(这里我们使用dbcp) <span styl ...

  7. Spring知识点总结(五)Spring整合JDBC

     1. 回顾JDBC        a. java操作关系型数据的API.导入相关数据库的驱动包后可以通过JDBC提供的接口来操作数据库.        b. 实现JDBC的六个步骤          ...

  8. Spring整合JDBC和Druid连接池

    我的博客名为黑客之谜,喜欢我的,或者喜欢未来的大神,点一波关注吧!顺便说一下,双十二快到了,祝大家双十二快乐,尽情的买买买~ 如果转载我的文章请标明出处和著名,谢谢配合. 我的博客地址为: https ...

  9. Spring整合JDBC temple

    一.Spring对Jdbc的支持 Spring为了提供对Jdbc的支持,在Jdbc API的基础上封装了一套实现,以此建立一个 JDBC 存取框架. 作为 Spring JDBC 框架的核心, JDB ...

  10. Spring整合JDBC(连接池、JDBC模板、Dao配置到Spring容器、配置文件的优化)

    1.Spring整合JDBC (1)导包(共12个): c3p0连接池.JDBC驱动(4个) Spring-jdbc.Spring-tx事务(2个) (2)JDBC模板对象(JDBCTemplate) ...

随机推荐

  1. 使用Python管理Azure(1):基础配置

    Azure提供了丰富的Python SDK来对Azure进行开发管理,包括使用Azure的开源框架在Azure上创建web应用程序,对Azure的虚拟机,存储等进行管理,本系类会简单介绍如何在ASM和 ...

  2. Oracle ORA-01555(快照过旧)

    一.引言: [oracle@yft yft]$ oerr ora 01555 01555, 00000, "snapshot too old: rollback segment number ...

  3. gem update --system 302 错误 解决方案(转)

    具体过程如下: 1.InstantRails-2.0安装后,在配置环境变量path中配置ruby/bin目录(如果系统中有多个RUBY,执行命令行的时候系统认的就是path中的) 2.进入DOS命令行 ...

  4. python爬爬爬之单网页html页面爬取

    python爬爬爬之单网页html页面爬取 作者:vpoet mail:vpoet_sir@163.com 注:随意copy 不用告诉我 #coding:utf-8 import urllib2 Re ...

  5. Centos下编译Linux内核

    Linux内核编译是一件简单却费事的事.但是独立的编译linux内核会帮助你很好的理解Linux内核的工作机理. 首先编译linux内核我们需要在当前linux操作系统下安装gcc编译器,因为我是Ce ...

  6. Error: Linux下 mysql.sock文件丢失被删除解决方法

    在默认情况下,Mysql安装以后会在/tmp目录下生成一个mysql.sock文件,如该文件丢失则Mysql将不能够正常启动,解决方法:使用mysqld_safe 启动即可解决: #basedir:m ...

  7. 浅析WebGIS

    浅析WebGIS 摘要:随着网络的发展,利用Web公布信息越来越普及化.而地理信息系统(GIS)与网络的结合就产生了万维网地理信息系统(WebGIS),它引起了地理信息公布的新的变革,对实现GIS信息 ...

  8. HDU2054_A == B ?【模拟题】【大数】【水的问题】

    A == B ? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. 关于jQuery的ajax的源码的dataType解读

    $.ajax其实底层还是用的XMLHttpRequest,对于加载数据的格式datatype有:xml.text.html.json.jsonp.script. 其中xml.text不需要处理,直接使 ...

  10. Android Service组件(1)

    android service 和其他服务一样,并没有实际运行的界面,它运行在android 后台.一般通过service为应用程序提供服务(比如,从Internet下载文件,控制音乐播放器等).Se ...