日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring29: JdbcTemplate详解

發布時間:2025/6/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。