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


      前一篇通过对传统的JDBC的使用操作,可以体会到使用的繁琐与复杂,套句话说,是用了20%作了真正的工作,80%作了重复的工作。

    那么通过本篇,可以了解如下的内容:

    1 如何配置数据源

    2 如何在spring中使用模板

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

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

      我们可以使用3种方式配置数据源:

      1 JNDI配置数据源

      这种做法我是没用过,感觉每次都要去修改配置Tomcat之类的web容器,很是麻烦。

      2 使用DBCP数据源连接池

      一般情况下都是采用这种方式,对于连接池的实现,也有很多种,比如DBCP,c3p0等等。

      用户可以针对连接池进行自己的配置,有助于数据库端的调优。

      如果想对数据源连接池多谢了解,可以猛戳该链接

      相对来说,最常使用的就是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操作数据库来说,省略了创建连接以及释放的过程。

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

  • 相关阅读:
    (转)ubuntu 对拍和基本操作
    一个在线翻译LateX的网站
    51nod 1376: 最长递增子序列的数量(二维偏序+cdq分治)
    BZOJ1087: [SCOI2005]互不侵犯King(状态压缩动态规划)
    ZOJ Problem Set
    bzoj2301:[HAOI2011]Problem b(容斥+莫比乌斯反演+分块)
    BZOJ 4318 OSU!期望DP
    CodeForces 235B Let's Play Osu!(概率)
    博客界面美化
    A+B Problem
  • 原文地址:https://www.cnblogs.com/xing901022/p/4269815.html
Copyright © 2020-2023  润新知