jdbctemplate簡介】

JdbcTemplate簡介
  Spring對數據庫的操作在jdbc上面做了深層次的封裝,使用spring的注入功能,可以把DataSource註冊到JdbcTemplate之中。
  JdbcTemplate位於中。其全限定命名爲org.springframework.jdbc.core.JdbcTemplate。要使用JdbcTemlate還需一個這個包包含了一下事務和異常控制
 
JdbcTemplate主要提供以下五類方法:
  • execute方法:可以用於執行任何SQL語句,一般用於執行DDL語句;
  • update方法及batchUpdate方法:update方法用於執行新增、修改、刪除等語句;batchUpdate方法用於執行批處理相關語句;
  • query方法及queryForXXX方法:用於執行查詢相關語句;
  • call方法:用於執行存儲過程、函數相關語句。

 

下面進行案件分析

 

在src下面新建一個屬性配置文件
[Java] 純文本查看 複製代碼
1
2
3
4
1 jdbc.user=root
2 jdbc.password=123456
3 jdbc.driverClass=com.mysql.jdbc.Driver
4 jdbc.jdbcUrl=jdbc\:mysql\:///test

 

  我們通常將數據庫的配置信息單獨放到一個文件中,這樣也爲了方便後期維護

 

配置Spring配置文件applicationContext.xml

 

[Java] 純文本查看 複製代碼
01
02
03
04
05
06
07
08
09
10
1 <context:property-placeholder location="classpath:db.properties"/> 2 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 3     <property name="user" value="${jdbc.user}"></property>
 4     <property name="password" value="${jdbc.password}"></property>
 5     <property name="driverClass" value="${jdbc.driverClass}"></property>
 6     <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
 7 </bean>
 8
 9 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
10     <property name="dataSource" ref="dataSource"></property>
11 </bean>


 

  第一行代碼:用來讀取db.properties文件中的數據。
  第二行代碼:用來配置一個數據源,這裏數據實現類來自C3P0中的一個屬性類。其中屬性的值就是來自於db.properties
  第九行代碼:配置一個JdbcTemplate實例,並注入一個dataSource數據源


測試代碼

 

1、update()方法

a、通過update插入數據


1 //啓動IoC容器2 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");3 //獲取IoC容器中JdbcTemplate實例4 JdbcTemplate jdbcTemplate=(JdbcTemplate) ctx.getBean("jdbcTemplate");5 String sql="insert into user (name,deptid) values (?,?)";6 int count= jdbcTemplate.update(sql, new Object[]{"caoyc",3});7 System.out.println(count);
 

  這裏update方法,第二參可以爲可變參數。在數據庫中可以看到,數據以被正確插入
 

 

b、通過update修改數據
[Java] 純文本查看 複製代碼
1
2
1 String sql="update user set name=?,deptid=? where id=?";
2 jdbcTemplate.update(sql,new Object[]{"zhh",5,51});


 

c、通過update刪除數據
[Java] 純文本查看 複製代碼
1
2
1 String sql="delete from user where id=?";
2 jdbcTemplate.update(sql,51);



2、batchUpdate()批量插入、更新和刪除方法

a、批量插入
[Java] 純文本查看 複製代碼
1
2
3
4
5
6
7
8
1 String sql="insert into user (name,deptid) values (?,?)";
2
3 List<Object[]> batchArgs=new ArrayList<Object[]>();
4 batchArgs.add(new Object[]{"caoyc",6});
5 batchArgs.add(new Object[]{"zhh",8});
6 batchArgs.add(new Object[]{"cjx",8});
7
8 jdbcTemplate.batchUpdate(sql, batchArgs);



 

  batchUpdate方法第二參數是一個元素爲Object[]數組類型的List集合


3、從數據中讀取數據到實體對象

  先定一個User實體類


 

[Java] 純文本查看 複製代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1 package com.proc;
 2
 3 public class User {
 4     private Integer id;
 5     private String name;
 6     private Integer deptid;
 7     public Integer getId() {
 8         return id;
 9     }
10     public void setId(Integer id) {
11         this.id = id;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public Integer getDeptid() {
20         return deptid;
21     }
22     public void setDeptid(Integer deptid) {
23         this.deptid = deptid;
24     }
25
26     public String toString() {
27         return "User [id=" + id + ", name=" + name + ", deptid=" + deptid + "]";
28     }
29 }




 

a、讀取單個對象
[Java] 純文本查看 複製代碼
1
2
3
4
5
1 String sql="select id,name,deptid from user where id=?";
2
3 RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class);
4 User user= jdbcTemplate.queryForObject(sql, rowMapper,52);
5 System.out.println(user);

 

  輸出結果:
  User [id=52, name=caoyc, deptid=6]

 

【注意】:1、使用BeanProperytRowMapper要求sql數據查詢出來的列和實體屬性需要一一對應。如果數據中列明和屬性名不一致,在sql語句中需要用as重新取一個別名
     2、使用JdbcTemplate對象不能獲取關聯對象

 

b、讀取多個對象

 

[Java] 純文本查看 複製代碼
1
2
3
4
5
6
7
1 String sql="select id,name,deptid from user";
2
3 RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class);
4 List<User> users= jdbcTemplate.query(sql, rowMapper);
5 for (User user : users) {
6     System.out.println(user);
7 }


 

輸出結果
...
User [id=49, name=姓名49, deptid=5]
User [id=50, name=姓名50, deptid=8]
User [id=52, name=caoyc, deptid=6]
User [id=53, name=zhh, deptid=8]
User [id=54, name=cjx, deptid=8]
---

 

c、獲取某個記錄某列或者count、avg、sum等函數返回唯一值
[Java] 純文本查看 複製代碼
1
2
3
4
1 String sql="select count(*) from user";
2
3 int count= jdbcTemplate.queryForObject(sql, Integer.class);
4 System.out.println(count);



 

在實際開發中可以怎樣用
UserDao.java

 

[Java] 純文本查看 複製代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
1 package com.proc; 2
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 5 import org.springframework.jdbc.core.JdbcTemplate;
 6 import org.springframework.jdbc.core.RowMapper;
 7 import org.springframework.stereotype.Repository;
 8
 9 @Repository
10 public class UserDao {
11
12     @Autowired
13     private JdbcTemplate jdbcTemplate;
14    
15     public User get(int id){
16         String sql="select id,name,deptid from user where id=?";
17         RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class);
18         return jdbcTemplate.queryForObject(sql, rowMapper,id);
19     }
20 }



 

xml配置:

 

[XML] 純文本查看 複製代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/aop [url]http://www.springframework.org/schema/aop/spring-aop-4.3.xsd[/url]
 7         [url]http://www.springframework.org/schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]
 8         [url]http://www.springframework.org/schema/context[/url] [url]http://www.springframework.org/schema/context/spring-context-4.3.xsd[/url]">
 9
10 <context:component-scan base-package="com.proc"></context:component-scan>
11 <context:property-placeholder location="classpath:db.properties"/>
12 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
13     <property name="user" value="${jdbc.user}"></property>
14     <property name="password" value="${jdbc.password}"></property>
15     <property name="driverClass" value="${jdbc.driverClass}"></property>
16     <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
17 </bean>
18
19 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
20     <property name="dataSource" ref="dataSource"></property>
21 </bean>
22 </beans>



 

代碼測試

1 UserDao userDao=(UserDao) ctx.getBean("userDao");2 System.out.println(userDao.get(53));

更多免費技術資料可關注:annalin1203

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章