前言:spring沾过一点点,但细节不了解,实例能力也不行,决定从头学起吧。

没有理论,只有实例代码,理论自行百度多的很的很

帖一下项目整体架构:

1、数据库建表

  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 entity;
  2. public class Customer {
  3. private int cust_id;
  4. private String name;
  5. private int age;
  6. public Customer() {
  7. super();
  8. // TODO Auto-generated constructor stub
  9. }
  10. public Customer(int cust_id, String name, int age) {
  11. super();
  12. this.cust_id = cust_id;
  13. this.name = name;
  14. this.age = age;
  15. }
  16. public int getCust_id() {
  17. return cust_id;
  18. }
  19. public void setCust_id(int cust_id) {
  20. this.cust_id = cust_id;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. public int getAge() {
  29. return age;
  30. }
  31. public void setAge(int age) {
  32. this.age = age;
  33. }
  34. }

3、接口dao和实现类

  1. package dao;
  2.  
  3. import entity.Customer;
  4.  
  5. public interface CustomerDao {
  6. public void insert(Customer customer);
  7. public Customer findByCustomer(int custId);
  8. }
  9.  
  10. --------------------------------------------------------------------------------------------
  11. package dao.impl;
  12.  
  13. import java.sql.Connection;
  14. import java.sql.PreparedStatement;
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17.  
  18. import javax.sql.DataSource;
  19.  
  20. import dao.CustomerDao;
  21. import entity.Customer;
  22.  
  23. public class CustomerDaoImpl implements CustomerDao {
  24.  
  25. private DataSource dataSource;
  26.  
  27. public void setDataSource(DataSource dataSource) {
  28. this.dataSource = dataSource;
  29. }
  30.  
  31. @Override
  32. public void insert(Customer customer) {
  33. String sql = "insert into customer"
  34. +"(cust_id,name,age) values(?,?,?)";
  35. Connection conn=null;
  36.  
  37. try {
  38. conn = dataSource.getConnection();//获取数据库连接
  39. PreparedStatement ps = conn.prepareStatement(sql);
  40. ps.setInt(1, customer.getCust_id());
  41. ps.setString(2, customer.getName());
  42. ps.setInt(3, customer.getAge());
  43. ps.executeUpdate();
  44. ps.close();
  45.  
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }finally{
  49. if(conn!=null){
  50. try {
  51. conn.close();
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. }
  58.  
  59. @Override
  60. public Customer findByCustomer(int custId) {
  61. String sql="select * from customer where cust_id = ?";
  62. Connection conn=null;
  63. Customer customer=null;
  64. try {
  65. conn = dataSource.getConnection();
  66. PreparedStatement ps = conn.prepareStatement(sql);
  67. ps.setInt(1, custId);
  68. ResultSet rs = ps.executeQuery();
  69. if(rs.next()){
  70. customer = new Customer();
  71. customer.setCust_id(rs.getInt("cust_id"));
  72. customer.setName(rs.getString("name"));
  73. customer.setAge(rs.getInt("age"));
  74.  
  75. }
  76. rs.close();
  77. ps.close();
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }finally{
  81. if(conn!=null){
  82. try {
  83. conn.close();
  84. } catch (SQLException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. return customer;
  90. }
  91.  
  92. }

4、spring配置文件:这里把数据源和业务拆分成2个配置

(1)Spring-Datasource.xml :数据源datasource配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7.  
  8. <bean id="dataSource"
  9. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  10. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  11. <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
  12. <property name="username" value="root"></property>
  13. <property name="password" value="root"></property>
  14. </bean>
  15.  
  16. </beans>

(2)Spring-Customer.xml :customer模块配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7.  
  8. <bean id="customerDAO" class="dao.impl.CustomerDaoImpl">
  9. <property name="dataSource" ref="dataSource"></property>
  10. </bean>
  11.  
  12. </beans>

(3)Spring-Module.xml :组件组合配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7.  
  8. <import resource="Spring-Datasource.xml"/>
  9. <import resource="Spring-Customer.xml"/>
  10.  
  11. </beans>

5、测试类

  1. package test;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. import dao.CustomerDao;
  7. import entity.Customer;
  8.  
  9. public class App {
  10. public static void main(String[] args) {
  11. ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
  12. CustomerDao customerDao=(CustomerDao) context.getBean("customerDAO");
  13. Customer customer=new Customer(1,"yiibai",29);
  14. customerDao.insert(customer);
  15.  
  16. Customer customer1=customerDao.findByCustomer(1);
  17. System.out.println(customer1.getName());
  18.  
  19. }
  20. }

个人总结:会用不代表理解,但多用几次绝对可以自行了解!如果看不了繁杂的理论,那就多动手几次吧

Spring系列-JDBC实例的更多相关文章

  1. Spring系列之JDBC对不同数据库异常如何抽象的?

    前言 使用Spring-Jdbc的情况下,在有些场景中,我们需要根据数据库报的异常类型的不同,来编写我们的业务代码.比如说,我们有这样一段逻辑,如果我们新插入的记录,存在唯一约束冲突,就会返回给客户端 ...

  2. 【Spring】Spring系列4之Spring支持JDBC

    4.Spring支持JDBC 4.1.使用JdbcTemplate简化JDBC开发 也可以这么用(不推荐): 4.2.使用NamedParameterJdbcTemplate

  3. SpringMVC系列(十五)Spring MVC与Spring整合时实例被创建两次的解决方案以及Spring 的 IOC 容器和 SpringMVC 的 IOC 容器的关系

    一.Spring MVC与Spring整合时实例被创建两次的解决方案 1.问题产生的原因 Spring MVC的配置文件和Spring的配置文件里面都使用了扫描注解<context:compon ...

  4. Spring 系列: Spring 框架简介 -7个部分

    Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...

  5. Spring实战6:利用Spring和JDBC访问数据库

    主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...

  6. Spring 系列: Spring 框架简介(转载)

    Spring 系列: Spring 框架简介 http://www.ibm.com/developerworks/cn/java/wa-spring1/ Spring AOP 和 IOC 容器入门 在 ...

  7. 【SSH框架】之Spring系列(一)

    微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系! 1.前言 前面更新过几篇关于 Struts2 框架和 Hibernate 框架的文章,但鉴于这两 ...

  8. 狗鱼IT教程:推介最强最全的Spring系列教程

    Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建. 简单来说,Spring是一个分层的JavaSE/EEfull-stack( ...

  9. Spring系列之AOP的原理及手动实现

    目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 引入 到目前为止,我们已经完成了简易的IOC和DI的功能,虽然相比如Spring来说肯定是非常简陋的,但是毕竟我 ...

随机推荐

  1. android: getDimension, getDimensionPixelOffset 和getDimensionPixelSize 区别

    ◆结论: getDimension 获取某个dimen的值,如果是dp或sp的单位,将其乘以density,如果是px,则不乘   返回float getDimensionPixelOffset 获取 ...

  2. .NET MVC结构框架下的微信扫码支付模式二 API接口开发测试

    直接上干货 ,我们的宗旨就是为人民服务.授人以鱼不如授人以渔.不吹毛求疵.不浮夸.不虚伪.不忽悠.一切都是为了社会共同进步,繁荣昌盛,小程序猿.大程序猿.老程序猿还是嫩程序猿,希望这个社会不要太急功近 ...

  3. webstorm的快捷键和zencoding

    1.webstorm快捷键: 生成viewport, meta:vp IntelliJ-Idea 的快捷键 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*…*/ ) Shift+F6 ...

  4. python2/3中 将base64数据写成图片,并将图片数据转为16进制数据的方法、bytes/string的区别

    1.python2将base64数据写成图片,并将数据转为16进制字符串的方法 import binascii img = u'R0lGODlhagAeAIcAAAAAAAAARAAAiAAAzABE ...

  5. 因默认包扫描问题导致的SpringBoot项目无法启动问题

    启动SpringBoot项目的时候,提示如下信息: -- :: --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing or ...

  6. 【转】一件有趣的事:我用 Python 爬了爬自己的微信朋友

    偶然了解到 Python 里的 itchat 包,它已经完成了 wechat 的个人账号 API 接口,使爬取个人微信信息更加方便. 于是乎玩心一起,打算爬一下自己的微信. 步骤核心: 网页启动not ...

  7. 8个超炫酷仿苹果应用的HTML5动画

    苹果的产品一直以精美的UI著称,无论是软件应用还是硬件设备.本文主要分享了8个很不错的HTML5动画应用,这些动画正式模仿了苹果的各类应用,有焦点图.钟表.菜单等HTML5应用和jQuery插件,大家 ...

  8. python for

    与其它大多数语言一样,Python 也拥有 for 循环.你到现在还未曾看到它们的唯一原因就是,Python 在其它太多的方面表现出色,通常你不需要它们. 其它大多数语言没有像 Python 一样的强 ...

  9. [datatable]排序时指定某列不可排序

    datatable是一个jquery扩展的表格插件.其提供了强大的表格功能. 官方地址:http://www.datatables.net/ 在官方示例中,对于表格的是否可排序是在初始化中设置的一个值 ...

  10. java基础解疑!!!

    疑问一:0.01+0.09的结果? public class MathTest{ public static void main(String[]args){ double a = 0.01, b = ...