Spring篇--07 spring jdbc
一、spring jdbc
1.spring jdbc是什么?
spring对jdbc的封装,使用spring jdbc访问数据库,可以不用谢一些重复性的代码,比如获取连接、关闭连接等。
2.如何使用?
step1:导包
spring-webmvc,spring-jdbc,mysql-connector,dbcp,junit
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lcz</groupId>
<artifactId>spring_jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
</dependencies>
</project>
step2:添加spring配置文件
<?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:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
">
<!-- 配置连接池 -->
<util:properties id="config" location="classpath:db.properties"/>
<!--配置连接池 -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{config.driver}"/>
<property name="url" value="#{config.url}"/>
<property name="username" value="#{config.username}"/>
<property name="password" value="#{config.password}"/>
</bean>
<!-- 配置JdbcTempate -->
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"/>
</bean>
<!-- 配置组件扫描 -->
<context:component-scan base-package="dao"/>
</beans>
step3:配置JdbcTemplate
注:JdbcTemplate提供了一些方法,用来访问数据库
JdbcTempate
step4:调用JdbcTemplate提供的方法来访问数据库
注:通过将JdbcTemplate注入到Dao中
package dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import entity.Emp;
@Repository("empDao")
public class EmpDao {
@Resource(name="jt")
private JdbcTemplate template;
public void save(Emp emp) {
String sql="insert into emp(name,age) values(?,?)";
Object[] params=new Object[] {emp.getName(),emp.getAge()};
template.update(sql,params);
}
public List<Emp> findAll(){
List<Emp> emps=new ArrayList<Emp>();
String sql="select *from emp";
emps=template.query(sql, new EmpRowMapper());
return emps;
}
public Emp findById(int id) {
Emp emp=null;
String sql="select *from emp where id=?";
Object[] args=new Object[]{id};
emp=template.queryForObject(sql, args,new EmpRowMapper());
return emp;
}
public void modify(Emp emp) {
String sql="update emp set name=?,age=? where id=?";
Object[] args=new Object[] {emp.getName(),emp.getAge(),emp.getId()};
template.update(sql,args);
}
public void delete(int id) {
String sql="delete from emp where id=?";
Object[] args=new Object[]{id};
template.update(sql,args);
}
//告诉JdbcTemplate如何将resultset中的一条 记录转换成对应的entity对象
class EmpRowMapper implements RowMapper<Emp>{
//rs:要处理的结果集(ResultSet)
//index:当前正在处理的记录的下标(从0开始的)
public Emp mapRow(ResultSet rs, int rowNum) throws SQLException {
Emp emp=new Emp();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setAge(rs.getInt("age"));
return emp;
}
}
}