1. Customer 表

在这个例子中,我们使用的是MySQL数据库。
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 com.yiibai.customer.model;

import java.sql.Timestamp;

public class Customer
{
int custId;
String name;
int age;
//getter and setter methods }

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

Customer Dao 接口.

package com.yiibai.customer.dao;

import com.yiibai.customer.model.Customer;

public interface CustomerDAO
{
public void insert(Customer customer);
public Customer findByCustomerId(int custId);
}
客户的DAO实现,使用 JDBC 发出简单的 insert 和 select SQL语句。
package com.yiibai.customer.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.yiibai.customer.dao.CustomerDAO;
import com.yiibai.customer.model.Customer; public class JdbcCustomerDAO implements CustomerDAO
{
private DataSource dataSource; public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} 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.getCustId());
ps.setString(2, customer.getName());
ps.setInt(3, customer.getAge());
ps.executeUpdate();
ps.close(); } catch (SQLException e) {
throw new RuntimeException(e); } finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
} public 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) {}
}
}
}
}

5. Spring bean配置

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

File : 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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerDAO" class="com.yiibai.customer.dao.impl.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean> </beans>

File : 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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.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/yiibaijava" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean> </beans>

File : 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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <import resource="database/Spring-Datasource.xml" />
<import resource="customer/Spring-Customer.xml" /> </beans>
6.项目结构
本实例完整目录结构。
7.运行它
package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yiibai.customer.dao.CustomerDAO;
import com.yiibai.customer.model.Customer; public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml"); CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO");
Customer customer = new Customer(1, "yiibai",29);
customerDAO.insert(customer); Customer customer1 = customerDAO.findByCustomerId(1);
System.out.println(customer1); }
}

输出结果:

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

最新文章

  1. 第四十三章 微服务CICD(5)- gitlab + jenkins + docker + dockerregsitry
  2. CSU 1081 集训队分组
  3. 遍历nsarray
  4. css3 动画 执行一次
  5. 用python matplotlib 画图
  6. Form_Form Builder编译fmb/library/menu方式总结(汇总)
  7. UVa 11729 Commando War 突击战
  8. 打造强大的BaseModel(2):让Model实现自动映射,将字典转化成Model
  9. windows下安装Django
  10. 深入理解 JavaScript 异步系列(2)—— jquery的解决方案
  11. [补] 如何在windows下用IDA优雅调试ELF
  12. php+smarty轻松开发微社区/微论坛
  13. android 之 Hnadler 、Message 、Looper
  14. Cocos Creator学习目录
  15. 等价类计数(Polya定理/Burnside引理)学习笔记
  16. python下编译py成pyc和pyo和pyd
  17. EL和JSTL的关系
  18. 求其中同一个主叫号码的两次通话之间间隔大于10秒的通话记录ID
  19. Nginx + Keepalived 实例(测试可行)
  20. 笨办法学Python(十八)

热门文章

  1. final修饰的变量是引用不能改变还是引用的对象不能改变
  2. Hibernate根据配置文件,生成建表语句
  3. Linux软件管理器(如何使用软件管理器来管理软件)2---安装及管理Linux应用程序
  4. IDE按住ctrl 打开单元 无效时 的方法
  5. HDU 3746 Cyclic Nacklace(KMP找循环节)
  6. (转)Opencv卷积操作
  7. Sublime Text 2之Emmet插件安装及使用
  8. CentOS6.9下安装MariaDB10.2.11
  9. 小技巧:tar命令打包目录时,排除文件和目录的命令
  10. hadoop环境安装及错误总结