1. 程式人生 > >Spring(七)

Spring(七)

template path exc apach == let 技術 aos nds

Spring JDBC

1.需要的實體類和數據庫

技術分享圖片

技術分享圖片

2.需要的dao

技術分享圖片

 1 package com.xdf.dao;
 2 
 3 import com.xdf.bean.Student;
 4 import org.springframework.jdbc.core.RowMapper;
 5 import org.springframework.jdbc.core.support.JdbcDaoSupport;
 6 
 7 import java.io.Serializable;
 8 import java.sql.ResultSet;
 9 import java.sql.SQLException;
10 import java.util.List; 11 12 /** 13 * dao的實現類 14 * 書寫sql 15 * JdbcDaoSupport====>BaseDao 16 * JdbcDaoSupport有兩個重要的屬性 17 * 01.jdbcTemplate 模版 18 * 02.需要 dataSource ===>數據源 19 * 20 * JdbcDaoSupport 所有的增刪改 都是update 21 * 查詢是query 22 * 23 */ 24 public class StudentDaoImpl extends
JdbcDaoSupport implements StudentDao { 25 26 public int add(Student student) { 27 String sql="insert into student values(?,?)"; 28 return getJdbcTemplate().update(sql,student.getsId(),student.getsName()); 29 } 30 31 public int del(Serializable id) { 32 String sql="delete from student where sid=?";
33 return getJdbcTemplate().update(sql,id); 34 } 35 36 public int update(Student student) { 37 String sql="update student set sname=? where sid=?"; 38 return getJdbcTemplate().update(sql,student.getsName(),student.getsId()); 39 } 40 41 public List<Student> getAll() { 42 String sql="select * from student"; 43 //使用匿名內部類 44 return getJdbcTemplate().query(sql,new RowMapper<Student>(){ 45 /** 46 * 這裏的ResultSet是返回一個對象! 47 * 我們之前使用的是 一個結果集! 48 */ 49 public Student mapRow(ResultSet rs, int rowNum) throws SQLException { 50 //創建student對象 依次返回 51 Student student=new Student(); 52 student.setsId(rs.getInt("sid")); 53 student.setsName(rs.getString("sname")); 54 return student; 55 } 56 }); 57 } 58 }

3.需要的service

技術分享圖片

 1 package com.xdf.service;
 2 
 3 import com.xdf.bean.Student;
 4 import com.xdf.dao.StudentDao;
 5 
 6 import java.io.Serializable;
 7 import java.util.List;
 8 
 9 public class StudentServiceImpl implements   StudentService {
10 
11     //交給spring容器實例化dao層對象
12     private StudentDao dao;
13 
14     public StudentDao getDao() {
15         return dao;
16     }
17 
18     public void setDao(StudentDao dao) {
19         this.dao = dao;
20     }
21 
22     public int add(Student student) {
23         return dao.add(student);
24     }
25 
26     public int del(Serializable id) {
27         return dao.del(id);
28     }
29 
30     public int update(Student student) {
31         return dao.update(student);
32     }
33 
34     public List<Student> getAll() {
35         return dao.getAll();
36     }
37 }

4.需要的jdbc.properties

技術分享圖片

5.需要的核心配置文件

 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:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xmlns:p="http://www.springframework.org/schema/p"
 8        xmlns:c="http://www.springframework.org/schema/c"
 9        xsi:schemaLocation="
10         http://www.springframework.org/schema/beans
11         http://www.springframework.org/schema/beans/spring-beans.xsd
12         http://www.springframework.org/schema/context
13         http://www.springframework.org/schema/context/spring-context.xsd
14         http://www.springframework.org/schema/tx
15         http://www.springframework.org/schema/tx/spring-tx.xsd
16         http://www.springframework.org/schema/aop
17         http://www.springframework.org/schema/aop/spring-aop.xsd">
18     <!--引入需要的jdbc.properties文件-->
19     <context:property-placeholder location="classpath:jdbc.properties"/>
20 
21     <!--01.使用spring自帶的數據源
22     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
23          <property name="driverClassName" value="${jdbc.driverClassName}"/>
24          <property name="url" value="${jdbc.url}"/>
25          <property name="username" value="${jdbc.userName}"/>
26          <property name="password" value="${jdbc.password}"/>
27     </bean>-->
28 
29     <!--02.使用c3p0自帶的數據源
30     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
31          <property name="driverClass" value="${jdbc.driverClassName}"/>
32          <property name="jdbcUrl" value="${jdbc.url}"/>
33          <property name="user" value="${jdbc.userName}"/>
34          <property name="password" value="${jdbc.password}"/>
35     </bean>-->
36 
37 
38     <!--03.使用dbcp自帶的數據源-->
39     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
40          <property name="driverClassName" value="${jdbc.driverClassName}"/>
41          <property name="url" value="${jdbc.url}"/>
42          <property name="username" value="${jdbc.userName}"/>
43          <property name="password" value="${jdbc.password}"/>
44     </bean>
45     <!--配置dao層對象-->
46     <bean  id="studentDao" class="com.xdf.dao.StudentDaoImpl">
47         <property name="dataSource" ref="dataSource"/>
48     </bean>
49 
50     <!-- 配置service層對象-->
51      <bean  id="studentService" class="com.xdf.service.StudentServiceImpl">
52          <property name="dao" ref="studentDao"/>
53      </bean>
54 </beans>

6.測試類

技術分享圖片

    未完待續!!!

Spring(七)