Spring对象jdbc的支持首先是导入jar包
需要的jar包和配置文件如下:
然后是导入jar包,这里使用的方法是eclipse配置jar包的方法可以参考我之前的文章
http://blog.csdn.net/weixin_39135506/article/details/75095294
这里面有eclipse配置jar包的方法,接下来就是我的文件结构图:
数据库结构如下:
Dept.java的代码如下:
UserDao.java代码如下:
package mytest;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 根据id查询数据
*
* @param id
* @return
*/
public Dept findById(int id) {
String sql = "select * from t_dept where deptId=?";
List depts = jdbcTemplate.query(sql, new rowMapper(), id);
return (depts != null && depts.size() > 0) ? depts.get(0) : null;
}
class rowMapper implements RowMapper {
// 如何封装一行记录
@Override
public Dept mapRow(ResultSet resultSet, int i) throws SQLException {
Dept dept = new Dept();
dept.setDeptId(resultSet.getInt("deptId"));
dept.setDeptName(resultSet.getString("deptName"));
return dept;
}
}
}
depts = jdbcTemplate.query(sql, new rowMapper(), id);
return (depts != null && depts.size() > 0) ? depts.get(0) : null;
}
class rowMapper implements RowMapper {
// 如何封装一行记录
@Override
public Dept mapRow(ResultSet resultSet, int i) throws SQLException {
Dept dept = new Dept();
dept.setDeptId(resultSet.getInt("deptId"));
dept.setDeptName(resultSet.getString("deptName"));
return dept;
}
}
}
bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1. 数据源对象: C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<!-- 2. 创建JdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- dao 实例 -->
<bean id="userDao" class="myTest.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
App.java代码如下
运行结果如下:
到此就实现了对jdbc的支持