spring中JdbcTemplate

Spring 为了简化 各种持久化 开发,内部提供了很多模板工具类类
JDBC —org.springframework.jdbc.core.JdbcTemplate
Hibernate — org.springframework.orm.hibernate3.HibernateTemplate
MyBatis — org.springframework.orm.ibatis.SqlMapClientTemplat

Spring JDBC是Spring提供的持久层技术
简化JDBC API开发,使用上和Apache公司的DBUtils框架非常类似
导入必要jar包到工程目录
导入Spring核心开发包到创建工程
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
还需要下载commons-logging日志包
commons-logging-1.1.1.jar
导入JDBC模板开发包
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar

数据库驱动
mysql-connector-java-5.0.8-bin.jar

代码示例:
不使用配置文件 ,操作数据库

package com.my.jdbc;

import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class JdbcTemplateTest {
	
	@Test
	public void demo1(){
		// 1、 先获得数据库连接池 对象 
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		// 获得连接,必须需要jdbc 四个基本参数 
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql:///spring_demo");
		dataSource.setUsername("root");
		dataSource.setPassword("1234");
		
		// 2、通过连接池,构造JdbcTemplate 对象
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		
		// 3、使用JdbcTemplate操作数据库 
		jdbcTemplate.execute("create table user(id int, name varchar(20))");
	}

}

通过配置配置数据库连接池的示例:

applicationContext.xml



	
	
	







	






	
	
		
	
	
	
		
		
		
		
	
	
	
	
		
	
		

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_demo
jdbc.username=root
jdbc.password=1234

测试类JdbcTemplateTest2.java

package com.my.jdbc;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class JdbcTemplateTest2 {

	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate jdbcTemplate ;
	
	@Test
	public void demo(){
		jdbcTemplate.execute("create table users0(id int)");
	}
	
}

使用JdbcTemplate 编写DAO实现数据CURD
代码示例:
Customer.java

package com.my.jdbc;

public class Customer {

	private int id;
	private String name; 
	private int age;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = 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;
	}
	
}

sql语句

CREATE TABLE `customer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CustomerDao.java

package com.my.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

//操作customer表进行增删改查
public class CustomerDao extends JdbcDaoSupport{ // 继承JdbcTemplate注入方法

	//增加客户
	public void save(Customer customer){
		String sql ="insert into customer values(null,?,?)";
		this.getJdbcTemplate().update(sql, customer.getName(),customer.getAge());
	}
	
	//根据id删除
	public void delete(Customer customer){
		String sql = "delete from customer where id = ?";
		this.getJdbcTemplate().update(sql, customer.getId());
	}
	
	//根据id 修改其它所有字段
	public void update(Customer customer){
		String sql = "update customer set name=? , age=? where id =?";
		this.getJdbcTemplate().update(sql, customer.getName() , customer.getAge(), customer.getId());
	}
	
	// 根据id 查找 age
	public int findAgeById(int id){
		String sql = "select age from customer where id = ?";
		return this.getJdbcTemplate().queryForInt(sql , id);
	}
	// 根据id 查询name
	public String findNameById(int id){
		String sql = "select name from customer where id = ?";
		return this.getJdbcTemplate().queryForObject(sql, String.class, id);
	}
	
	// 查询所有客户信息
	public List findAll(){
		String sql = "select * from customer";
		return this.getJdbcTemplate().query(sql, new CustomerRowMapper());
	}
	
	// 根据id 查询某个客户信息
	public Customer findById(int id){
		String sql = "select * from customer where id = ?";
		return this.getJdbcTemplate().queryForObject(sql, new CustomerRowMapper(), id);
	}
	
	private class CustomerRowMapper implements RowMapper {

		@Override
		public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
			Customer customer = new Customer(); 
			customer.setId(rs.getInt("id"));
			customer.setName(rs.getString("name"));
			customer.setAge(rs.getInt("age"));
			return customer;
		}
		
	}
	
	
}

applicationContext.xml




	
	
	
	
		
		
		
		
	
	
	
	
		
	
	
	
	
		
	
	

测试类TestCustmerDao.java

package com.my.jdbc;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestCustmerDao {

	@Autowired
	@Qualifier("customerDao")
	private CustomerDao customerDao ;
	
	@Test
	// 测试数据保存
	public void testSave(){
		Customer customer = new Customer();
		customer.setName("rose");
		customer.setAge(18);
		
		customerDao.save(customer);
	}
	
	@Test
	// 测试数据修改
	public void testUpdate(){
		Customer customer = new Customer();
		customer.setId(3);
		customer.setName("jack");
		customer.setAge(19);
		
		customerDao.update(customer);
	}
	
	@Test
	// 测试数据删除 
	public void testDelete(){
		Customer customer = new Customer();
		customer.setId(1);
		
		customerDao.delete(customer);
	}
	
	@Test
	// 测试 简单查询
	public void testFindAgeNameById(){
		System.out.println(customerDao.findAgeById(2));
		System.out.println(customerDao.findNameById(2));
	}
	
	@Test
	// 测试复杂查询
	public void testFindAll(){
		System.out.println(customerDao.findAll());
		System.out.println(customerDao.findById(2));
	}
	
}

你可能感兴趣的:(spring)