当前位置:Gxlcms > 数据库问题 > Spring+JDBC实例

Spring+JDBC实例

时间:2021-07-01 10:21:17 帮助过:49人阅读

1. Customer 表

在这个例子中,我们使用的是MySQL数据库。
  1. CREATE TABLE `customer` (
  2. `CUST_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. `NAME` varchar(100) NOT NULL,
  4. `AGE` int(10) unsigned NOT NULL,
  5. PRIMARY KEY (`CUST_ID`)
  6. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2. Customer模型

添加一个客户模型用来存储用户的数据。
  1. package com.yiibai.customer.model;
  2. import java.sql.Timestamp;
  3. public class Customer
  4. {
  5. int custId;
  6. String name;
  7. int age;
  8. //getter and setter methods
  9. }

4. 数据访问对象 (DAO) 模式

Customer Dao 接口.

  1. package com.yiibai.customer.dao;
  2. import com.yiibai.customer.model.Customer;
  3. public interface CustomerDAO
  4. {
  5. public void insert(Customer customer);
  6. public Customer findByCustomerId(int custId);
  7. }
客户的DAO实现,使用 JDBC 发出简单的 insert 和 select SQL语句。
  1. package com.yiibai.customer.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import javax.sql.DataSource;
  7. import com.yiibai.customer.dao.CustomerDAO;
  8. import com.yiibai.customer.model.Customer;
  9. public class JdbcCustomerDAO implements CustomerDAO
  10. {
  11. private DataSource dataSource;
  12. public void setDataSource(DataSource dataSource) {
  13. this.dataSource = dataSource;
  14. }
  15. public void insert(Customer customer){
  16. String sql = "INSERT INTO CUSTOMER " +
  17. "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
  18. Connection conn = null;
  19. try {
  20. conn = dataSource.getConnection();
  21. PreparedStatement ps = conn.prepareStatement(sql);
  22. ps.setInt(1, customer.getCustId());
  23. ps.setString(2, customer.getName());
  24. ps.setInt(3, customer.getAge());
  25. ps.executeUpdate();
  26. ps.close();
  27. } catch (SQLException e) {
  28. throw new RuntimeException(e);
  29. } finally {
  30. if (conn != null) {
  31. try {
  32. conn.close();
  33. } catch (SQLException e) {}
  34. }
  35. }
  36. }
  37. public Customer findByCustomerId(int custId){
  38. String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";
  39. Connection conn = null;
  40. try {
  41. conn = dataSource.getConnection();
  42. PreparedStatement ps = conn.prepareStatement(sql);
  43. ps.setInt(1, custId);
  44. Customer customer = null;
  45. ResultSet rs = ps.executeQuery();
  46. if (rs.next()) {
  47. customer = new Customer(
  48. rs.getInt("CUST_ID"),
  49. rs.getString("NAME"),
  50. rs.getInt("Age")
  51. );
  52. }
  53. rs.close();
  54. ps.close();
  55. return customer;
  56. } catch (SQLException e) {
  57. throw new RuntimeException(e);
  58. } finally {
  59. if (conn != null) {
  60. try {
  61. conn.close();
  62. } catch (SQLException e) {}
  63. }
  64. }
  65. }
  66. }

5. Spring bean配置

创建 customerDAO 和数据源在 Spring bean 配置文件中。

File : Spring-Customer.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="customerDAO" class="com.yiibai.customer.dao.impl.JdbcCustomerDAO">
  6. <property name="dataSource" ref="dataSource" />
  7. </bean>
  8. </beans>

File : Spring-Datasource.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="dataSource"
  6. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  7. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  8. <property name="url" value="jdbc:mysql://localhost:3306/yiibaijava" />
  9. <property name="username" value="root" />
  10. <property name="password" value="password" />
  11. </bean>
  12. </beans>

File : Spring-Module.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <import resource="database/Spring-Datasource.xml" />
  6. <import resource="customer/Spring-Customer.xml" />
  7. </beans>
6.项目结构 本实例完整目录结构。
技术分享 技术分享 7.运行它
  1. package com.yiibai.common;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.yiibai.customer.dao.CustomerDAO;
  5. import com.yiibai.customer.model.Customer;
  6. public class App
  7. {
  8. public static void main( String[] args )
  9. {
  10. ApplicationContext context =
  11. new ClassPathXmlApplicationContext("applicationContext.xml");
  12. CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO");
  13. Customer customer = new Customer(1, "yiibai",29);
  14. customerDAO.insert(customer);
  15. Customer customer1 = customerDAO.findByCustomerId(1);
  16. System.out.println(customer1);
  17. }
  18. }

输出结果:

  1. Customer [age=29, custId=1, name=yiibai]
  下载源代码 – http://pan.baidu.com/s/1SR2GI

Spring+JDBC实例

标签:dash   目录   void   配置文件   div   getc   .sql   getbean   localhost   

人气教程排行