spring29: JdbcTemplate详解
生活随笔
收集整理的這篇文章主要介紹了
spring29: JdbcTemplate详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
spring提供用于操作jdbc工具類,類似DBUtils, 依賴連接池DataSource(數據源)。
通過api?
import org.apache.commons.dbcp.BasicDataSource; import org.springframework.jdbc.core.JdbcTemplate;public class TestApi {public static void main(String[] args){// 1. 創建數據源(連接池) dbcpBasicDataSource basicDataSource = new BasicDataSource();// 基本四項basicDataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); //驅動basicDataSource.setUrl("jdbc:oracle:thin:@//127.0.0.1:1521/orcl");basicDataSource.setUsername("scott");basicDataSource.setPassword("123456");// 2. 創建模板JdbcTemplate jdbcTemplate = new JdbcTemplate(basicDataSource);// 3. 通過api操作jdbcTemplate.update("insert into course(cid, cname) values(?,?)", "306","ios編程");} }?
配置dbcp
? 配置文件
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置數據源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property><property name="url" value="jdbc:oracle:thin:@//127.0.0.1:1521/orcl"></property><property name="username" value="scott"></property><property name="password" value="123456"></property></bean><!-- 創建模板,需要注入數據源 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置dao --><bean id="courseDao" class="com.atchina.d_spring_jdbctemplate.dbcp.CourseDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean></beans>Dao類?
package com.atchina.d_spring_jdbctemplate.dbcp;import org.springframework.jdbc.core.JdbcTemplate;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class CourseDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}public void update(Course c){String sql = "update course a set cname=? where cid = ?";jdbcTemplate.update(sql, c.getCname(), c.getId());} }測試類?
package com.atchina.d_spring_jdbctemplate.dbcp;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class TestDBCP {@Testpublic void demo01(){Course c = new Course();c.setCname("數據庫原理");c.setId(306);String xmlPath = "com/atchina/d_spring_jdbctemplate/dbcp/applicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);CourseDao dao = (CourseDao)applicationContext.getBean("courseDao");dao.update(c);} }配置c3p0
? 配置文件
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置數據源c3p0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property><property name="jdbcUrl" value="jdbc:oracle:thin:@//127.0.0.1:1521/orcl"></property><property name="user" value="scott"></property><property name="password" value="123456"></property></bean><!-- 創建模板,需要注入數據源 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置dao --><bean id="courseDao" class="com.atchina.d_spring_jdbctemplate.c3p0.CourseDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean></beans>?測試類
package com.atchina.d_spring_jdbctemplate.c3p0;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class TestC3P0 {@Testpublic void demo01(){Course c = new Course();c.setCname("操作系統");c.setId(306);String xmlPath = "com/atchina/d_spring_jdbctemplate/c3p0/applicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);CourseDao dao = (CourseDao)applicationContext.getBean("courseDao");dao.update(c);} }使用JdbcDaoSupport
import java.util.List;import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class CourseDao extends JdbcDaoSupport{public void update(Course c){String sql = "update course a set cname=? where cid = ?";this.getJdbcTemplate().update(sql, c.getCname(), c.getCid());}public List<Course> findAll(){String sql = "select * from course";return this.getJdbcTemplate().query(sql, BeanPropertyRowMapper.newInstance(Course.class));} }?配置文件
? ? JdbcDaoSupport中有JdbcTemplate的屬性,所以在配置文件中可以不用配置jdbcTemplate,直接配置一個數據源即可。jdbcDaoSupport會根據數據源創建一個jdbcTemplate。
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置數據源c3p0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property><property name="jdbcUrl" value="jdbc:oracle:thin:@//127.0.0.1:1521/orcl"></property><property name="user" value="scott"></property><property name="password" value="123456"></property></bean><!-- 配置dao --><bean id="courseDao" class="com.atchina.d_spring_jdbctemplate.support.CourseDao"><property name="dataSource" ref="dataSource"></property></bean></beans>使用properties文件
? 配置properties文件
jdbc.driverClass=oracle.jdbc.driver.OracleDriver jdbc.jdbcUrl=jdbc:oracle:thin:@//127.0.0.1:1521/orcl jdbc.user=scott jdbc.password=123456配置spring文件
<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 加載配置文件"classpaht:"前綴表示src下在配置文件之后通過${key}獲得內容 --><context:property-placeholder location="classpath:com/atchina/d_spring_jdbctemplate/properties/jdbcinfo.properties"/><!-- 配置數據源c3p0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- 配置dao --><bean id="courseDao" class="com.atchina.d_spring_jdbctemplate.properties.CourseDao"><property name="dataSource" ref="dataSource"></property></bean></beans>dao類
package com.atchina.d_spring_jdbctemplate.properties;import java.util.List;import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class CourseDao extends JdbcDaoSupport{public void update(Course c){String sql = "update course a set cname=? where cid = ?";this.getJdbcTemplate().update(sql, c.getCname(), c.getCid());}public List<Course> findAll(){String sql = "select * from course";return this.getJdbcTemplate().query(sql, BeanPropertyRowMapper.newInstance(Course.class));}public Course getById(int cid){String sql = "select * from course where cid = ? ";return this.getJdbcTemplate().queryForObject(sql, BeanPropertyRowMapper.newInstance(Course.class), cid);} }測試類
package com.atchina.d_spring_jdbctemplate.properties;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class TestProperties {@Testpublic void demo01(){String xmlPath = "com/atchina/d_spring_jdbctemplate/properties/applicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);CourseDao dao = (CourseDao)applicationContext.getBean("courseDao");Course cr = dao.getById(306);System.out.println(cr);} }?
總結
以上是生活随笔為你收集整理的spring29: JdbcTemplate详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式:设计模式七大原则
- 下一篇: spring30: 事务