Spring第七篇【Spring的JDBC模块】

前言

上一篇Spring博文主要讲解了如何使用Spring来实现AOP编程,本博文主要讲解Spring的对JDBC的支持javascript

对于JDBC而言,咱们确定不会陌生,咱们在初学的时候确定写过很是很是多的JDBC模板代码java

回顾对模版代码优化过程

咱们来回忆一下咱们怎么对模板代码进行优化的!mysql

  • 首先来看一下咱们原生的JDBC:须要手动去数据库的驱动从而拿到对应的链接..
try {
            String sql = "insert into t_dept(deptName) values('test');";
            Connection con = null;
            Statement stmt = null;
            Class.forName("com.mysql.jdbc.Driver");
            // 链接对象
            con = DriverManager.getConnection("jdbc:mysql:///hib_demo", "root", "root");
            // 执行命令对象
            stmt =  con.createStatement();
            // 执行
            stmt.execute(sql);

            // 关闭
            stmt.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
  • 由于JDBC是面向接口编程的,所以数据库的驱动都是由数据库的厂商给作到好了,咱们只要加载对应的数据库驱动,即可以获取对应的数据库链接….所以,咱们写了一个工具类,专门来获取与数据库的链接(Connection),固然啦,为了更加灵活,咱们的工具类是读取配置文件的方式来作的
 /* * 链接数据库的driver,url,username,password经过配置文件来配置,能够增长灵活性 * 当咱们须要切换数据库的时候,只须要在配置文件中改以上的信息便可 * * */ private static String driver = null; private static String url = null; private static String username = null; private static String password = null; static { try { //获取配置文件的读入流 InputStream inputStream = UtilsDemo.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取配置文件的信息 driver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); //加载驱动类 Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,username,password); } public static void release(Connection connection, Statement statement, ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }
  • 通过上面一层的封装,咱们能够在使用的地方直接使用工具类来获得与数据库的链接…那么比原来就方便不少了!可是呢,每次仍是须要使用Connection去建立一个Statement对象。而且不管是什么方法,其实就是SQL语句和传递进来的参数不一样!
  • 因而,咱们就自定义了一个JDBC的工具类,详情能够看http://blog.csdn.net/hon_3y/article/details/53760782#t6
  • 咱们自定义的工具类其实就是以DbUtils组件为模板来写的,所以咱们在开发的时候就一直使用DbUtils组件了

使用Spring的JDBC

上面已经回顾了一下之前咱们的JDBC开发了,那么看看Spring对JDBC又是怎么优化的spring

首先,想要使用Spring的JDBC模块,就必须引入两个jar文件:sql

  • 引入jar文件数据库

    • spring-jdbc-3.2.5.RELEASE.jar
    • spring-tx-3.2.5.RELEASE.jar
  • 首先仍是看一下咱们原生的JDBC代码:获取Connection是能够抽取出来的,直接使用dataSource来获得Connection就好了编程

public void save() {
        try {
            String sql = "insert into t_dept(deptName) values('test');";
            Connection con = null;
            Statement stmt = null;
            Class.forName("com.mysql.jdbc.Driver");
            // 链接对象
            con = DriverManager.getConnection("jdbc:mysql:///hib_demo", "root", "root");
            // 执行命令对象
            stmt =  con.createStatement();
            // 执行
            stmt.execute(sql);

            // 关闭
            stmt.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 值得注意的是,JDBC对C3P0数据库链接池是有很好的支持的。所以咱们直接可使用Spring的依赖注入,在配置文件中配置dataSource就好了
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>
// IOC容器注入
    private DataSource dataSource;
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


    public void save() {
        try {
            String sql = "insert into t_dept(deptName) values('test');";
            Connection con = null;
            Statement stmt = null;
            // 链接对象
            con = dataSource.getConnection();
            // 执行命令对象
            stmt =  con.createStatement();
            // 执行
            stmt.execute(sql);

            // 关闭
            stmt.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • Spring来提供了JdbcTemplate这么一个类给咱们使用!它封装了DataSource,也就是说咱们能够在Dao中使用JdbcTemplate就好了。markdown

  • 建立dataSource,建立jdbcTemplate对象app

<?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:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///zhongfucheng"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>

    <!--扫描注解-->
    <context:component-scan base-package="bb"/>

    <!-- 2. 建立JdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>
  • userDao
package bb;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/** * Created by ozc on 2017/5/10. */


@Component
public class UserDao implements IUser {

    //使用Spring的自动装配
    @Autowired
    private JdbcTemplate template;

    @Override
    public void save() {
        String sql = "insert into user(name,password) values('zhoggucheng','123')";
        template.update(sql);
    }

}
  • 测试:
@Test
    public void test33() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bb/bean.xml");

        UserDao userDao = (UserDao) ac.getBean("userDao");
        userDao.save();
    }

这里写图片描述


JdbcTemplate查询

咱们要是使用JdbcTemplate查询会发现有不少重载了query()方法ide

这里写图片描述

通常地,若是咱们使用queryForMap(),那么只能封装一行的数据,若是封装多行的数据、那么就会报错!而且,Spring是不知道咱们想把一行数据封装成是什么样的,所以返回值是Map集合…咱们获得Map集合的话还须要咱们本身去转换成本身须要的类型。


咱们通常使用下面这个方法:

这里写图片描述

咱们能够实现RowMapper,告诉Spriing咱们将每行记录封装成怎么样的

public void query(String id) {
        String sql = "select * from USER where password=?";

        List<User> query = template.query(sql, new RowMapper<User>() {


            //将每行记录封装成User对象
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user = new User();
                user.setName(resultSet.getString("name"));
                user.setPassword(resultSet.getString("password"));

                return user;
            }

        },id);


        System.out.println(query);
    }

这里写图片描述


固然了,通常咱们都是将每行记录封装成一个JavaBean对象的,所以直接实现RowMapper,在使用的时候建立就行了

class MyResult implements RowMapper<Dept>{

        // 如何封装一行记录
        @Override
        public Dept mapRow(ResultSet rs, int index) throws SQLException {
            Dept dept = new Dept();
            dept.setDeptId(rs.getInt("deptId"));
            dept.setDeptName(rs.getString("deptName"));
            return dept;
        }

    }
相关文章
相关标签/搜索