spring jdbc使用总结及示例代码

来源:互联网 发布:美编软件 编辑:程序博客网 时间:2024/06/06 04:58

spring对jdbc的支持

为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架。

作为 SpringJDBC 框架的核心, JDBC Template的设计目的是为不同类型的 JDBC 操作提供模板方法.

每个模板方法都能控制整个过程, 并允许覆盖过程中的特定任务.

通过这种方式, 可以在尽可能保留灵活性的情况下, 将数据库存取的工作量降到最低.

new spring jdbc java projectmaven demo

整个项目目录

第一步:pom文件

一个基本的springjdbc的java工程需要

mysql-connect-java

spring-context

spring-jdbc

c3p0

  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>5.1.36</version>  <scope>runtime</scope></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context</artifactId>  <version>4.3.8.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-jdbc</artifactId>  <version>4.3.8.RELEASE</version></dependency><dependency>  <groupId>com.mchange</groupId>  <artifactId>c3p0</artifactId>  <version>0.9.2.1</version></dependency><dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>4.12</version></dependency>

第二步:配置db.properties文件

jdbc.user=rootjdbc.password=rootjdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///testjdbc.initPoolSize=5jdbc.maxPoolSize=10

第三步:配置applicationContext.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"       xsi:schemaLocation="       http://www.springframework.org/sahema/beansa       http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-4.0.xsd">            <!--导入资源文件-->    <context:property-placeholder location="classpath:db.properties"/>    <!--配置C3P0数据源-->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="user" value="${jdbc.user}"/>        <property name="password" value="${jdbc.password}"></property>        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>        <property name="driverClass" value="${jdbc.driverClass}"></property>        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>    </bean>    <!--配置spring的JdbcTemplate模板类-->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource"/>    </bean></beans>

第四步:检测连接是否建立

在jdbc package下新建testConn.java类

private ApplicationContext ctx = null;@Testpublic void testDataSource() throws SQLException {DataSource dataSource = ctx.getBean(DataSource.class);System.out.println(dataSource.getConnection());}

测试1:用sql 语句和参数更新数据库

public intupdate(String sql,

                  Object... args)

          throwsorg.springframework.dao.DataAccessException

private ApplicationContext ctx = null;private JdbcTemplate jdbcTemplate;{ctx = new ClassPathXmlApplicationContext("applicationContext.xml");jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");}/** * 执行 INSERT, UPDATE, DELETE */@Testpublic void testUpdate(){String sql = "UPDATE employees SET last_name = ? WHERE id = ?";jdbcTemplate.update(sql, "Jack", 5);}

测试2:批量更新

public int[]batchUpdate(String sql,

                        java.util.List<Object[]> batchArgs)

                 throws org.springframework.dao.DataAccessException

/** *  执行批量更新:批量的INSET UPDATE DELETE *  最后一个参数是List<Object[]> *  因为修改一条记录需要一个 Object 的数组, 那么多条不就需要多个 Object 的数组 */@Testpublic void testBatchUpdate(){String sql = "insert into employees(last_name, email, dept_id) values (?,?,?)";List<Object[]> batchArgs = new ArrayList<>();batchArgs.add(new Object[]{"aa", "aa@ss.com", 1});batchArgs.add(new Object[]{"bb", "bb@ss.com", 2});batchArgs.add(new Object[]{"cc", "cc@ss.com", 3});batchArgs.add(new Object[]{"dd", "dd@ss.com", 4});jdbcTemplate.batchUpdate(sql,batchArgs);}

测试3:查询单行记录,得到一个对象

public <T>T queryForObject(String sql,

     org.springframework.jdbc.core.RowMapper<T> rowMapper,

      Object... args)

                    throws org.springframework.dao.DataAccessException

    /**     * 从数据库中获取一条记录, 实际得到对应的一个对象     * 注意不是调用 queryForObject(String sql, Class<Employee> requiredType, Object... args) 方法!     * 而需要调用 queryForObject(String sql, RowMapper<Employee> rowMapper, Object... args)方法     * 1. 其中的 RowMapper 指定如何去映射结果集的行, 常用的实现类为 BeanPropertyRowMapper     * 2. 使用 SQL 中列的别名完成列名和类的属性名的映射. 例如 last_name lastName     * 3. 不支持级联属性. JdbcTemplate 到底是一个 JDBC 的小工具, 而不是 ORM 框架     */    @Test    public void testQueryForObject(){        String sql = "select id, last_name as lastName, email, dept_id as deptId from employees where id = ?";        //如何映射结果集的行        RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);        Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, 1);        System.out.println(employee);    }

测试4:查询多行记录,得到一个List<Object>

public <T>java.util.List<T> query(String sql,

        org.springframework.jdbc.core.RowMapper<T> rowMapper,

         Object... args)

         throwsorg.springframework.dao.DataAccessException
    /**     * 获取实体类的集合     * 注意调用的不是 queryForList 方法     */    @Test    public void testQueryForList(){        String sql = "select id, last_name lastName, email, dept_id deptId from employees where id < ?";        RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);        List<Employee> list = jdbcTemplate.query(sql, rowMapper, 2);        System.out.println(list);    }

测试5:获取单列值或统计值

public <T>T queryForObject(String sql,

                        Class<T>requiredType,

                        Object... args)

                    throwsorg.springframework.dao.DataAccessException
    /**     * 获取单个列的值     * 使用 queryForObject(String sql, Class<T> requiredType, Object... args)     */    @Test    public void testQueryForColumnObject(){        String sql = "select last_name from employees where id = ?";        String lastName = jdbcTemplate.queryForObject(sql, String.class, 1);        System.out.println(lastName);    }    /**     * 获取统计查询值     * 使用 queryForObject(String sql, Class<T> requiredType, Object... args)     */    @Test    public void testQueryForCount(){        String sql = "select count(id) from employees";        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);        System.out.println(count);    }

测试6:在dao中使用jdbcTemplate

1. 在dao package下建Employee.java

@Repositorypublic class EmployeeDao {    //能够使用Autowired注解,要求JdbcTemplate的名称jdbcTemplate要与applicationContext中//配置的spring JdbcTemplate模板类的id相同?//每次使用都创建了一个JdbcTemplate的新实例了吗?    @Autowired    private JdbcTemplate jdbcTemplate;    public Employee get(Integer id){        String sql = "SELECT id, last_name lastName, email FROM employees WHERE id = ?";        RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);        Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, id);        return employee;    }}

2. 在applicationContext.xml中添加配置

    <context:component-scan base-package="hfi.bellychang"/>

3. 在TestConn.java中测试

private EmployeeDao employeeDao;{employeeDao = ctx.getBean(EmployeeDao.class);}    /**     * 测试EmployeeDao方法     */    @Test    public void testEmployeeDao(){        System.out.println(employeeDao.get(1));    }

JdbcTemplate 类被设计成为线程安全的, 所以可以在 IOC 容器中声明它的单个实例, 并将这个实例注入到所有的 DAO 实例中。

实现方法:

      使用AbstractBaseDao

public abstract class AbstractBaseDao {    @Autowired    public JdbcTemplate jdbcTemplate;    @Autowired    public NamedParameterJdbcTemplate namedParameterJdbcTemplate;    public JdbcTemplate getJdbcTemplate() {        return jdbcTemplate;    }    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate(){        return namedParameterJdbcTemplate;    }}
所有其他的Dao实现类都extendsAbstractBaseDao,于是this.getJdbcTemplate就可以获取jdbcTemplate对象了

测试7:在jdbctemplate中使用具名参数

在经典的 JDBC 用法中, SQL参数是用占位符 ? 表示,并且受到位置的限制. 定位参数的问题在于, 一旦参数的顺序发生变化, 就必须改变参数绑定.

在 Spring JDBC框架中, 绑定 SQL 参数的另一种选择是使用具名参数(named parameter).

具名参数: SQL 按名称(以冒号开头)而不是按位置进行指定. 具名参数更易于维护, 也提升了可读性. 具名参数由框架类在运行时用占位符取代

具名参数只在NamedParameterJdbcTemplate 中得到支持

1. 在springmvc-db.xml中配置

    <!-- 配置 NamedParameterJdbcTemplate, 该对象可以使用具名参数,         其没有无参数的构造器, 所以必须为其构造器指定参数 -->    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">        <constructor-arg type="javax.sql.DataSource" ref="dataSource"/>    </bean>

2. 测试方法

private NamedParameterJdbcTemplate namedParameterJdbcTemplate;namedParameterJdbcTemplate = (NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplate");/**     * 使用具名参数时, 可以使用 update(String sql, SqlParameterSource paramSource) 方法进行更新操作     * 这样可以实现将一作用个对象转化为数据库中一条记录的功能     * 1. SQL语句中的参数名和类的属性一致!     * 2. 使用SqlParameterSource 的 BeanPropertySqlParameterSource 实现类作为参数.     */    @Test    public void testNamedParameterJdbcTemplate(){        String sql = "INSERT INTO employees(last_name, email, dept_id) VALUES(:lastName,:email,:deptId)";        Employee employee = new Employee();        employee.setLastName("XYZ");        employee.setEmail("xyz@sina.com");        employee.setDeptId(3);        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(employee);        namedParameterJdbcTemplate.update(sql, parameterSource);    }
代码下载:http://download.csdn.net/download/u013905744/10038339