當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring 数据访问那些事儿(一)spring + jdbc
生活随笔
收集整理的這篇文章主要介紹了
Spring 数据访问那些事儿(一)spring + jdbc
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
從本篇文章開始,將會陸續為大家介紹一些spring訪問數據的方式,從簡單直接的JDBC到JdbcTemplate,再到一些復雜的ORM框架(如mybatis、hibernate)。每篇文章都跟著一個簡單實例。作為系列的開篇,先從spring與jdbc談起,數據庫使用mysql。
1.customer表 CREATE TABLE customer(CUST_ID int(10) unsigned NOT NULL AUTO_INCREMENT,NAME varchar(100) NOT NULL,AGE int(10) unsigned NOT NULL,PRIMARY KEY(CUST_ID)
)ENGINE=InnoDB AUTO_INCREMENT=2 2.通過eclipse創建maven項目,目錄結構如下:
3.項目依賴pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>spring-jdbc</groupId><artifactId>spring-jdbc</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><!-- Spring Core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.1.RELEASE</version></dependency><!-- Spring context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.1.RELEASE</version></dependency><!-- Spring JDBC --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.2.1.RELEASE</version></dependency> <dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><!-- mysql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> </dependencies>
</project> 4.model層 創建一個與customer表對應的實體類,以便存放 Customer對象 package org.thinkingingis.model;public class Customer {private int id;private String name;private int age;public Customer(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}public int getId(){return id;}public void setId(int id){this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
5.DAO層 即數據訪問對象層,負責實際與數據庫打交道。 CustomerDAO package org.thinkingingis.dao;import org.thinkingingis.model.Customer;public interface CustomerDAO {public void insert(Customer customer);public Customer findByCustomerId(int custId);
}
JdbcCustomerDAO package org.thinkingingis.dao.impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.sql.DataSource;import org.thinkingingis.dao.CustomerDAO;
import org.thinkingingis.model.Customer;public class JdbcCustomerDAO implements CustomerDAO {private DataSource dataSource;public void setDataSource(DataSource dataSource){this.dataSource = dataSource;}@Overridepublic void insert(Customer customer) {String sql = "INSERT INTO CUSTOMER (CUST_ID, NAME, AGE) VALUES (?, ?, ?)";Connection conn = null;try {conn = dataSource.getConnection();PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(1, customer.getId());ps.setString(2, customer.getName());ps.setInt(3, customer.getAge());ps.executeUpdate();ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if(conn != null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}@Overridepublic Customer findByCustomerId(int custId) {String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";Connection conn = null;try {conn = dataSource.getConnection();PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(1, custId);Customer customer = null;ResultSet rs = ps.executeQuery();if(rs.next()){customer = new Customer(rs.getInt("CUST_ID"), rs.getString("NAME"), rs.getInt("age"));}rs.close();ps.close();return customer;} catch (SQLException e) {throw new RuntimeException(e);} finally {if(conn != null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}}
6.通過xml文件方式配置數據源與bean 這里將數據源配置與customerDAO的bean分離了,也可以將它們配置在一個文件中,后面的文章會給大家帶來不同的配置方式。 Spring-Module.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><import resource="database/Spring-Datasource.xml" /><import resource="customer/Spring-Customer.xml" /></beans> Spring-Datasource.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/springmvcjdbc" /><property name="username" value="root" /><property name="password" value="123456" /></bean></beans> Spring-Customer.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><bean id="customerDAO" class="org.thinkingingis.dao.impl.JdbcCustomerDAO"><property name="dataSource" ref="dataSource" /></bean></beans>這里指定了JdbcCustomerDAO spring核心容器在需要時會自動進行裝配。 7.controller層 App.java package org.thinkingingis.controller;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.thinkingingis.dao.CustomerDAO;
import org.thinkingingis.model.Customer;public class App {public static void main(String[] args){ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");CustomerDAO customerDao = (CustomerDAO) context.getBean("customerDAO");Customer customer = new Customer(4, "can", 27);customerDao.insert(customer);Customer findCustomer = customerDao.findByCustomerId(1);System.out.println(findCustomer.getId());System.out.println(findCustomer.getName());System.out.println(findCustomer.getAge());}
} 8.結果截圖
至此,通過spring + jdbc的方式訪問數據庫就完成啦,這種方式相比于直接通過jdbc訪問數據庫并沒有簡化操作,過程依然很冗長,下一篇將會為大家帶來通過spring + JdbcTemplate的方式訪問數據庫。
至此,通過spring + jdbc的方式訪問數據庫就完成啦,這種方式相比于直接通過jdbc訪問數據庫并沒有簡化操作,過程依然很冗長,下一篇將會為大家帶來通過spring + JdbcTemplate的方式訪問數據庫。
(如遇到問題,請留言給作者,以便共同探討gis知識。thinkingingis@qq.com)
Wechat公眾號:ThinkingInGIS
歡迎大家關注:)
總結
以上是生活随笔為你收集整理的Spring 数据访问那些事儿(一)spring + jdbc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring4 MVC + REST
- 下一篇: Spring 数据访问那些事儿(二)Sp