导包
编写测试类
//演示JDBC模板
public class Demo {
@Test
public void test() throws PropertyVetoException{
//创建连接池对象
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql:///spring_day03");
ds.setUser("root");
ds.setPassword("a2195261");
//创建Jdbc模板对象
JdbcTemplate jt = new JdbcTemplate(ds);
//书写对象并执行
String sql = "insert into t_user values(null,'jieki') ";
jt.update(sql);
}
}
以上是普通类的
整合到Spring
准备接口
public interface UserDao {
void save(User u);
void delete(Integer id);
void update(User u);
User getById(Integer id);
//查询总记录数据
int getTotalCount();
//查询所有
List<User> getAll();
}
准备实现类
public class UserDaoImpl implements UserDao {
private JdbcTemplate jt;
@Override//增加
public void save(User u) {
String sql = "insert into t_user values(null,?)";
jt.update(sql, u.getName());
}
@Override//删除
public void delete(Integer id) {
String sql = "delete from t_user where id = ?";
jt.update(sql, id);
}
@Override//更新
public void update(User u) {
String sql = "update t_user set name = ? where id = ?";
jt.update(sql,u.getName(),u.getId());
}
@Override//单个查询
public User getById(Integer id) {
String sql = "select * from t_user where id = ?";
return jt.queryForObject(sql,new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// 你只用封装就可以了
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}
}, id);
}
@Override
public int getTotalCount() {
String sql ="Select count(*) from t_user";
Integer queryForObject = jt.queryForObject(sql, Integer.class);
return queryForObject;
}
@Override
public List<User> getAll() {
String sql ="Select * from t_user";
List<User> list=jt.query(sql,new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}
});
return list;
}
public JdbcTemplate getJt() {
return jt;
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
}
配置applicationContext.xml核心配置文件,将连接池注入Spring容器 ,将JdbcTemplate放入容器中,将JdbcTemplate放入容器中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 将连接池放入Spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day03"></property>
<property name="user" value="root"></property>
<property name="password" value="a2195261"></property>
</bean>
<!-- 将JdbcTemplate放入容器中 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- UserDao放入容器中 -->
<bean name="userDao" class="com.itheima.jdbctemplate.UserDaoImpl">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>
测试
//演示JDBC模板
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
@Resource(name="userDao")
private UserDao u;
@Test
public void test2() {
User u2 = new User();
u2.setName("ccc");
u2.setId(1);
//u.save(u2);
//u.delete(2);
//u.update(u2);
//User byId = u.getById(1);
//System.out.println(byId);
//int totalCount = u.getTotalCount();
//System.out.println(totalCount);
List<User> all = u.getAll();
for (User user : all) {
System.out.println(user);
}
}
}
改进可以不注入JdbcTemplate直接继承JdbcDaoSupport
properties文件也可以放外面
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
private JdbcTemplate jt;
@Override//增加
public void save(User u) {
String sql = "insert into t_user values(null,?)";
super.getJdbcTemplate().update(sql, u.getName());
}
@Override//删除
public void delete(Integer id) {
String sql = "delete from t_user where id = ?";
super.getJdbcTemplate().update(sql, id);
}
@Override//更新
public void update(User u) {
String sql = "update t_user set name = ? where id = ?";
super.getJdbcTemplate().update(sql,u.getName(),u.getId());
}
@Override//单个查询
public User getById(Integer id) {
String sql = "select * from t_user where id = ?";
return super.getJdbcTemplate().queryForObject(sql,new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// 你只用封装就可以了
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}
}, id);
}
@Override
public int getTotalCount() {
String sql ="Select count(*) from t_user";
Integer queryForObject = super.getJdbcTemplate().queryForObject(sql, Integer.class);
return queryForObject;
}
@Override
public List<User> getAll() {
String sql ="Select * from t_user";
List<User> list= super.getJdbcTemplate().query(sql,new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}
});
return list;
}
public JdbcTemplate getJt() {
return jt;
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 指定读取配置 db.properties-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 将连接池放入Spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 将JdbcTemplate放入容器中
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> -->
<!-- UserDao放入容器中 -->
<bean name="userDao" class="com.itheima.jdbctemplate.UserDaoImpl">
<!-- <property name="jt" ref="jdbcTemplate"></property> -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>