Spring系列-JDBC实例

前言: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());
		
	}
}

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

posted @ 2017-05-26 16:59  sincoolvip  阅读(151)  评论(0编辑  收藏  举报