【Spring实战】—— 15 Spring JDBC模板使用

  前一篇经过对传统的JDBC的使用操做,能够体会到使用的繁琐与复杂,套句话说,是用了20%做了真正的工做,80%做了重复的工做。mysql

那么经过本篇,能够了解以下的内容:web

1 如何配置数据源spring

2 如何在spring中使用模板sql

3 如何创建数据源的统一的基类数据库

  首先看一下如何配置数据源

  咱们可使用3种方式配置数据源:设计模式

  1 JNDI配置数据源app

  这种作法我是没用过,感受每次都要去修改配置Tomcat之类的web容器,非常麻烦。测试

 

  2 使用DBCP数据源链接池this

  通常状况下都是采用这种方式,对于链接池的实现,也有不少种,好比DBCP,c3p0等等。url

  用户能够针对链接池进行本身的配置,有助于数据库端的调优。

  若是想对数据源链接池多谢了解,能够猛戳该连接

  相对来讲,最常使用的就是dbcp和c3p0了。

 

  3 基于JDBC的驱动的数据源

  这种是最基本的经过驱动程序管理数据源,可是没有链接池的概念。

  有两种实现方式:

  DriverManagerDataSource:通常都是使用这种,这种方式每次请求都会返回一个新的链接。

  SingleConnectionDataSource:这种每次都是使用的一个链接。

  

  本篇为了简单方便,就直接使用的第三种。

  

  Spring中的模板以及提供的基类

  在Spring中为咱们提供了三种模板:

  1 JdbcTemplate

  提供最简单的数据访问等功能。

 

  2 NamedParameterJdbcTemplate

  经过该模板,能够把参数做为查询的条件传入方法中。

 

  3 SimpleJdbcTemplate(通常都是使用这种)

  结合了一些自动装箱等功能,3.0之后,整合了NamedParameterJdbcTemplate。

  

  为了不每次都要把jdbctemplate的bean注入到咱们的DAO里面,Spring为咱们实现了三种对应的基类,咱们的DAO实现类须要继承这些基类,就能够直接使用模板了。

  对应的分别是:JdbcDapSupport、SimpleJdbcDaoSupport、NamedParameterJdbcDaoSupport

  

  最后就是程序的使用介绍了

  首先看一下配置数据源的bean.xml

<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
                         
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value="123qwe"/>
    </bean>
    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <constructor-arg ref="dataSource"/>
    </bean>
    
    <bean id="newjdbcdao" class="com.spring.chap5.dao.NewJdbcImpl" >
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>
</beans>

  这里,咱们配置了dataSource,以及jdbcTemplate,最后把jdbcTemplate注入到dao的实现类里面。

  接下来的DAO的接口

public interface NewJdbc { public void insertPerson(String id,String name,int age); public void findPersonById(String id); }

  DAO的实现类

public class NewJdbcImpl implements NewJdbc{ private SimpleJdbcTemplate jdbcTemplate; public void setJdbcTemplate(SimpleJdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void insertPerson(String id,String name,int age){ jdbcTemplate.update("insert into persons (id,name,age) values (?,?,?)", id,name,age); } public void findPersonById(String id){ Person person = jdbcTemplate.queryForObject("select * from persons where id = ?", new ParameterizedSingleColumnRowMapper<Person>(){ public Person mapRow(ResultSet rs,int rowNum) throws SQLException{ Person p = new Person(); p.setId(rs.getString(1)); p.setName(rs.getString(2)); p.setAge(rs.getInt(3)); return p; } } , id); System.out.println("id:"+person.getId()+" name:"+person.getName()+" age:"+person.getAge()); } }

  最后是测试使用的类

public class test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); NewJdbc newjdbc = (NewJdbc)ctx.getBean("newjdbcdao"); newjdbc.insertPerson("003", "xingoo3", 25); newjdbc.findPersonById("003"); } }

  以上即是Spring基于JDBC的模板使用了。

 

  能够看到,相对于前面的传统的JDBC操做数据库来讲,省略了建立链接以及释放的过程。

  仅仅是把操做的真正的实现部分交给开发人员,这就是模板的设计模式的应用——分离模板与开发人员的实现。

相关文章
相关标签/搜索