标签:
个人总结,转载请注明出处:http://www.cnblogs.com/lidabnu/p/5679354.html
基于Spring4.3.1官方文档总结,官方文档链接http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#jdbc
Spring JDBC能干什么,如下图所示,可见,其目的还是“简化开发”,把通用的业务无关的操作都在框架中做了。
先思考一下使用JDBC操作数据库需要解决哪些问题:
先简要介绍下Spring4.3.1中的JDBC操作相关类:
一个个看要解决的问题:
如何配置数据库连接:
Spring有三种方式配置数据源,即:使用JNDI来利用外部数据源(例如Tomcat等WEB容器)、利用DBCP管理的数据库连接池、以及使用Spring自带的DriveManagerDataSource(这个不支持连接池,因此不能用于生产环境)。这里仅仅介绍DBCP。其使用方式十分简单,就是在配置文件中配置一个DBCP数据源即可。
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/stock"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> <property name="initialSize" value="5"></property> <property name="maxActive" value="10"></property> </bean>
当然,一般情况下,数据库的URL以及用户名、密码等均会在额外的配置文件中配置,所以可以利用SpEL表达式来弄。需要使用context:property-placeholder来配置“配置文件的路径”。
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 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"> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> --> <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> --> <!-- <property name="url" value="jdbc:mysql://localhost:3306/stock"></property> --> <!-- <property name="username" value="root"></property> --> <!-- <property name="password" value="123456"></property> --> <!-- <property name="initialSize" value="5"></property> --> <!-- <property name="maxActive" value="10"></property> --> <!-- </bean> --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/stock"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="initialSize" value="5"></property> <property name="maxActive" value="10"></property> </bean> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <bean id="stockDao" class="testjdbc.dao.StockDao"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- <context:component-scan base-package="stock.dao"></context:component-scan> --> </beans>
如何执行查询:
查询标量:
int countOfActorsNamedJoe = this.jdbcTemplate.queryForObject( "select count(*) from t_actor where first_name = ?", Integer.class, "Joe"); String lastName = this.jdbcTemplate.queryForObject( "select last_name from t_actor where id = ?", new Object[]{1212L}, String.class);
查询对象:使用queryForObject方法,输入参数使用Object数组,重载定义“行-对象”转换对象RowMapper
Stock queryStock(String code) { return getJdbcTemplate().queryForObject("select * from stock where code=?",new Object[]{code}, new RowMapper<Stock>() { public Stock mapRow(ResultSet rs, int arg1) throws SQLException { Stock stock = new Stock(); stock.setCode(rs.getString("code")); stock.setName(rs.getString("name")); stock.setId(rs.getInt("id")); return stock; } }); }
查询一批对象:与queryForObject相同
List<Stock> queryStocks()//不带条件 { return getJdbcTemplate().query(SELECT_SQL, new RowMapper<Stock>() { public Stock mapRow(ResultSet rs, int arg1) throws SQLException { Stock stock = new Stock(); stock.setCode(rs.getString("code")); stock.setName(rs.getString("name")); stock.setId(rs.getInt("id")); return stock; } }); } List<Stock> queryStocks(String codePrefix)//带条件 { return getJdbcTemplate().query("select * from stock where code like ?",new Object[]{codePrefix+"%"}, new RowMapper<Stock>() { public Stock mapRow(ResultSet rs, int arg1) throws SQLException { Stock stock = new Stock(); stock.setCode(rs.getString("code")); stock.setName(rs.getString("name")); stock.setId(rs.getInt("id")); return stock; } }); }
如何执行删除、修改和插入:
int insert(Stock stock) { return getJdbcTemplate().update("Insert into stock(id,code,name) value(?,?,?)", stock.getId(),stock.getCode(),stock.getName()); } int delete(String stockCode) { return getJdbcTemplate().update("delete from stock where code=?", stockCode); }
如何执行批量操作:
批量操作减少了Client端(应用程序)和数据库服务器之间的网络来回,因此可提升性能,下次再单独写一篇
标签:
原文地址:http://www.cnblogs.com/lidabnu/p/5679354.html