當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring JdbcTemplate配置
生活随笔
收集整理的這篇文章主要介紹了
Spring JdbcTemplate配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
1. pom.xml,需要的spring jar包
2. application.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:aop="http://www.springframework.org/schema/aop"xmlns:c="http://www.springframework.org/schema/c"xmlns:cache="http://www.springframework.org/schema/cache"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:lang="http://www.springframework.org/schema/lang"xmlns:p="http://www.springframework.org/schema/p"xmlns:task="http://www.springframework.org/schema/task"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/cache?http://www.springframework.org/schema/cache/spring-cache-3.2.xsdhttp://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc?http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee?http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/lang?http://www.springframework.org/schema/lang/spring-lang-3.2.xsdhttp://www.springframework.org/schema/task?http://www.springframework.org/schema/task/spring-task-3.2.xsdhttp://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/util?http://www.springframework.org/schema/util/spring-util-3.2.xsd"><!--?開啟注解?--><context:annotation-config/><context:component-scan?base-package="com.jd"/><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--?加載jdbc配置文件?--><!--?<beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property?name="locations"><list><value>classpath*:/*.properties</value></list></property></bean>?--><!--?加載jdbc配置文件?--><context:property-placeholder?location="jdbc.properties"/><!--?定義dataSource?--><bean?id="dataSource"?class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property?name="driverClassName"?value="${jdbc.driverClassName}"?/><property?name="url"?value="${jdbc.url}"?/><property?name="username"?value="${jdbc.username}"?/><property?name="password"?value="${jdbc.password}"?/></bean><!--?定義jdbcTemplate?--><bean?id="jdbcTemplate"?class="org.springframework.jdbc.core.JdbcTemplate"><property?name="dataSource"?ref="dataSource"?/></bean><!--?定義?studentDao?bean,注入jdbcTemplate--><bean?id="studentDao"?class="com.jd.dao.impl.StudentDaoImpl"><property?name="template"?ref="jdbcTemplate"></property></bean> </beans>3. StudentDao.java
package?com.jd.dao;import?java.util.List;public?interface?StudentDao<T>?{public?void?saveStudent();public?T?queryStudent(int?id);public?List<T>?queryStudentList(); }4. StudentDaoImpl.java
package?com.jd.dao.impl;import?java.sql.ResultSet; import?java.sql.SQLException; import?java.util.List;import?javax.annotation.Resource;import?org.springframework.jdbc.core.JdbcTemplate; import?org.springframework.jdbc.core.RowMapper;import?com.jd.dao.StudentDao; import?com.jd.vo.Student;public?class?StudentDaoImpl<T>?implements?StudentDao<T>{//private?DataSource?dataSource;private?JdbcTemplate?template;/*public?DataSource?getDataSource()?{return?dataSource;}*//***?注入dataSource*?@param?dataSource*//*@Resourcepublic?void?setDataSource(DataSource?dataSource)?{this.dataSource?=?dataSource;this.template?=?new?JdbcTemplate(dataSource);}*/public?JdbcTemplate?getTemplate()?{return?template;}/***?注解注入*?@param?template*/@Resourcepublic?void?setTemplate(JdbcTemplate?template)?{this.template?=?template;}public?void?saveStudent()?{/*try?{Connection?conn?=?dataSource.getConnection();Student?s?=?new?Student();s.setId(4);s.setName("張四");String?sql?=?"insert?into?student?values(?,?)";PreparedStatement?state?=?conn.prepareStatement(sql);state.setInt(1,?s.getId());state.setString(2,?s.getName());state.execute();System.out.println("保存學生成功");}?catch?(SQLException?e)?{e.printStackTrace();}*/Student?s?=?new?Student();s.setId(7);s.setName("張七");template.update("insert?into?student?values(?,?)",?new?Object[]{s.getId(),s.getName()},?new?int[]{java.sql.Types.INTEGER,java.sql.Types.VARCHAR});System.out.println("保存學生成功");}@SuppressWarnings({?"rawtypes",?"unchecked"?})public?T?queryStudent(int?id)?{//回調函數RowMapper?rowMapper?=?new?RowMapper(){public?Object?mapRow(ResultSet?rs,?int?rowNum)?throws?SQLException?{Student?stu?=?new?Student();stu.setId(rs.getInt(1));stu.setName(rs.getString(2));return?stu;}};System.out.println("查詢學生id:"?+?id);????return?(T)template.queryForObject("select?*?from?Student?where?id=?",?new?Object[]{id},?new?int[]{java.sql.Types.INTEGER},?rowMapper);}@SuppressWarnings({?"unchecked",?"rawtypes"?})public?List<T>?queryStudentList()?{RowMapper?rowMapper?=?new?RowMapper(){public?Object?mapRow(ResultSet?rs,?int?rowNum)?throws?SQLException?{Student?stu?=?new?Student();stu.setId(rs.getInt(1));stu.setName(rs.getString(2));return?stu;}};System.out.println("查詢所有學生");????return?(List<T>)template.query("select?*?from?Student",?null,?null,?rowMapper);}}JdbcTemplate主要提供以下五類方法:
execute方法:可以用于執行任何SQL語句,一般用于執行DDL語句;
update方法及batchUpdate方法:update方法用于執行新增、修改、刪除等語句;batchUpdate方法用于執行批處理相關語句;
query方法及queryForXXX方法:用于執行查詢相關語句;
call方法:用于執行存儲過程、函數相關語句。
5. 測試類
- import?java.util.List;import?junit.framework.TestCase;import?org.springframework.context.ApplicationContext; import?org.springframework.context.support.ClassPathXmlApplicationContext;import?com.jd.dao.StudentDao; import?com.jd.vo.Student;public?class?TestSpringJdbcTemplate?extends?TestCase?{public?void?testSpringJdbc(){ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext("application.xml");StudentDao?dao?=??(StudentDao)ctx.getBean("studentDao");dao.saveStudent();Student?stu?=?(Student)?dao.queryStudent(5);System.out.println("學號:"?+?stu.getId()?+?",?姓名:"?+?stu.getName());List<Student>?students?=?dao.queryStudentList();for(Student?stu:students){System.out.println("學號:"?+?stu.getId()?+?",?姓名:"?+?stu.getName());}} }
轉載于:https://my.oschina.net/u/2311010/blog/403459
總結
以上是生活随笔為你收集整理的Spring JdbcTemplate配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《TCP/IP详解卷1:协议》第3章 I
- 下一篇: 浅谈jQuery中 wrap() wra