spring中JdbcTemplate
来源:互联网 发布:mac系统如何格式化u盘 编辑:程序博客网 时间:2024/05/29 17:10
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
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置数据库连接池 --> <!-- spring 内置连接池 --><!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> --><!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> --><!-- <property name="url" value="jdbc:mysql:///spring_demo"></property> --><!-- <property name="username" value="root"></property> --><!-- <property name="password" value="1234"></property> --><!-- </bean> --> <!-- DBCP连接池 --><!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> --><!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> --><!-- <property name="url" value="jdbc:mysql:///spring_demo"></property> --><!-- <property name="username" value="root"></property> --><!-- <property name="password" value="1234"></property> --><!-- </bean> --> <!-- 引入外部properties 属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 将连接池交给JdbcTemplate 构造模板对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean></beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:///spring_demojdbc.username=rootjdbc.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<Customer> 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<Customer> { @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
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 将连接池交给JdbcTemplate 构造模板对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 将JdbcTemplate注入给 customerDao --> <bean id="customerDao" class="com.my.jdbc.CustomerDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean></beans>
测试类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中jdbcTemplate
- Spring中JdbcTemplate小结
- Spring中JdbcTemplate实现
- Spring中jdbcTemplate
- spring中JdbcTemplate
- Spring中jdbcTemplate用法
- spring中JdbcTemplate
- Spring中JdbcTemplate详解
- Spring中JdbcTemplate的使用
- Spring中HibernateDaoSuport、HibernateTemplate、jdbcTemplate
- spring中JDBCTemplate的使用
- spring 中JdbcTemplate的使用
- spring中JdbcTemplate的使用
- Spring中jdbcTemplate的用法
- Spring中JdbcTemplate的使用
- spring boot中使用JdbcTemplate
- 在Spring中配置jdbcTemplate
- Spring中JdbcTemplate中使用RowMapper
- Android 之 控件的背景样式总结
- 正则案例
- STM32学习之普通定时器是否能定时1us?
- BZOJ 3944 Sum
- qt学习笔记:Qt标准对话框之QInputDialog
- spring中JdbcTemplate
- 一个maven项目pom配置
- git别名
- hnust
- WebSocket 是什么原理?为什么可以实现持久连接?
- LeetCode 202. Happy Number (迭代,各位平方和)
- BZOJ 4320: ShangHai2006 Homework
- asd
- Struts2配置图解