- Spring初学快速入门
- 安装Spring工具套件到Eclipse
- Spring快速入门
- Spring自动装配Bean
- Spring JDBC支持
- Spring JavaConfig
- Spring 依赖注入(DI)
-
Spring Bean基础
- Spring Bean引用例子
- 如何注入值到Spring bean属性
- Spring bean加载多个配置文件
- Spring内部bean实例
- Spring Bean作用域实例
- Spring集合 (List,Set,Map,Properties) 实例
- Spring ListFactoryBean实例
- Spring SetFactoryBean实例
- Spring MapFactoryBean例子
- Spring注入日期到bean属性-CustomDateEditor
- Spring PropertyPlaceholderConfigurer实例
- Spring bean配置继承
- Spring依赖检查
- Spring使用@Required注解依赖检查
- Spring自定义@Required-style注解
- Spring Bean InitializingBean和DisposableBean实例
- Spring Bean init-method 和 destroy-method实例
- Spring @PostConstruct和@PreDestroy实例
- Spring表达式语言
- Spring自动组件扫描
- Spring AOP (面向方面编程)
- Spring AOP + AspectJ框架
- Spring Hibernate支持
- Spring E-mail支持
-
Spring与其它Web框架集成
Spring+JDBC实例
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.zyiz.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.zyiz.customer.dao; import com.zyiz.customer.model.Customer; public interface CustomerDAO { public void insert(Customer customer); public Customer findByCustomerId(int custId); }
客户的DAO实现,使用 JDBC 发出简单的 insert 和 select SQL语句。
package com.zyiz.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.zyiz.customer.dao.CustomerDAO; import com.zyiz.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.zyiz.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/zyizjava" /> <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.zyiz.netmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zyiz.customer.dao.CustomerDAO; import com.zyiz.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, "zyiz",29); customerDAO.insert(customer); Customer customer1 = customerDAO.findByCustomerId(1); System.out.println(customer1); } }
输出结果:
Customer [age=29, custId=1, name=zyiz]
下载源代码 – http://pan.baidu.com/s/1SR2GI
分类导航
- Java教程
- Vim教程
- Swing教程
- Spring教程
- Spring Web Services教程
- Spring MVC教程
- Spring JDBC教程
- Spring Cloud教程
- Spring Boot教程
- Spring Boot CLI教程
- Spring Batch教程
- Spring AOP教程
- PDFBox教程
- JSP教程
- JSF教程
- JPA教程
- Java面向对象设计
- Java设计模式
- Java虚拟机教程
- Java泛型教程
- Java正则表达式教程
- Java数据类型教程
- Java并发编程教程
- Java密码学教程
- Java多线程教程
- Java国际化(i18n)教程
- JavaFX教程
- Java9教程
关注微信小程序
扫描二维码
程序员编程王