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

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

帖一下项目整体架构:

1、数据库建表

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=1 DEFAULT CHARSET=utf8;

2、实体类:与数据库表customer字段一一匹配

package entity;
public class Customer {
private int cust_id;
private String name;
private int age;
public Customer() {
super();
// TODO Auto-generated constructor stub
}
public Customer(int cust_id, String name, int age) {
super();
this.cust_id = cust_id;
this.name = name;
this.age = age;
}
public int getCust_id() {
return cust_id;
}
public void setCust_id(int cust_id) {
this.cust_id = cust_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;
}
}

3、接口dao和实现类

package dao;

import entity.Customer;

public interface CustomerDao {
public void insert(Customer customer);
public Customer findByCustomer(int custId);
} --------------------------------------------------------------------------------------------
package dao.impl; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import javax.sql.DataSource; import dao.CustomerDao;
import entity.Customer; public class CustomerDaoImpl implements CustomerDao { private DataSource dataSource; public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} @Override
public 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.getCust_id());
ps.setString(2, customer.getName());
ps.setInt(3, customer.getAge());
ps.executeUpdate();
ps.close(); } catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} @Override
public Customer findByCustomer(int custId) {
String sql="select * from customer where cust_id = ?";
Connection conn=null;
Customer customer=null;
try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, custId);
ResultSet rs = ps.executeQuery();
if(rs.next()){
customer = new Customer();
customer.setCust_id(rs.getInt("cust_id"));
customer.setName(rs.getString("name"));
customer.setAge(rs.getInt("age")); }
rs.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return customer;
} }

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean> </beans>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="customerDAO" class="dao.impl.CustomerDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <import resource="Spring-Datasource.xml"/>
<import resource="Spring-Customer.xml"/> </beans>

5、测试类

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.CustomerDao;
import entity.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(1,"yiibai",29);
customerDao.insert(customer); Customer customer1=customerDao.findByCustomer(1);
System.out.println(customer1.getName()); }
}

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

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. C++ Primer笔记 容器和算法(2)

    erase 删除后  返回的是删除元素的后一个迭代器位置 int main() { //怎样正确的删除全部元素 循环 int a[]={1,2,3,4,5,6,7,8,9}; vector<in ...

  2. [CoreOS 转载] CoreOS实践指南(一)

    转载:http://www.csdn.net/article/2014-12-29/2823356 摘要:CoreOS是一个采用了高度精简的系统内核及外围定制的操作系统.ThoughtWorks的软件 ...

  3. 每天一个linux命令(4) df命令

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...

  4. python求线性回归斜率

    一. 先说我对这个题目的理解 直线的x,y方程是这样的:y = kx+b, k就是斜率. 求线性回归斜率, 就是说 有这么一组(x, y)的对应值——样本.如果有四组,就说样本量是4.根据这些样本,做 ...

  5. c++ primer读书笔记之c++11(四)

    1  带有作用域的枚举 scoped-enumeration 相信大家都用过枚举量,都是不带有作用域的,在头文件中定义需要特别注意不要出现重名的情况.为了解决这种问题,c++11提供了带作用于的枚举. ...

  6. 【转&参考】MySQL利用frm和ibd文件进行数据恢复

    MySQL利用frm和idb文件进行数据恢复 源MySQL现状: 版本:5.6.* 存储引擎:innodb存储引擎 要恢复数据库:skill 重点要恢复表:slot_value 已有的文件: 备份了所 ...

  7. django 中多字段主键(复合、联合主键)

    django中不支持双主键.多主键. 要实现类似功能可以: classMeta: unique_together=(("driver","restaurant" ...

  8. 【神经网络】BP反向传播神经网络

    BP算法细节 参数说明:假设有n层.J表示代价函数,和上面的E是同样的意思,只不过用不同的字母写而已. 分析:要想知道第l层的第i个结点的残差,必须知道层已经计算出来了残差,你只要把后面一层的每个结点 ...

  9. maven copy jar 插件

     插件比较特殊 eclipse下的 首先声明插件 <pluginManagement> <plugin> <groupId>org.apache.maven.plu ...

  10. 用Python脚本在豆瓣音乐人小站上下载未开放下载的歌曲

    [本文出自天外归云的博客园] 第一步,去你要下载的音乐人小站页面: 第二步,点开要下载的歌,在播放页面F12进入调试模式,在Network视图下可以看到mp3文件所在的url: 第三步,上脚本(需安装 ...