JdbcTemplate功能与用法总结

一、引入jar包,导入log4j.properties

二、步骤

1、定义目标类,提供get,set,tostring方法

2、编写目标类dao层接口

public interface UserDao {
	int add(User user);
	int delete(Long uid);
	int update(User user);
	User queryById(Long uid);
	List<User> queryAll();
	List<User> queryByPage();
	List<User> queryByPage(int currentPage, int pageSize);
	Long queryByCount();
}

 3、目标类dao层实现需要继承JdbcDaoSupport 以达到在配置文件中为实现类注入数据库

让RoleDaoImpl 继承 JdbcDaoSupport, 因为(org.springframework.jdbc.core.support.JdbcDaoSupport)类提供了JdbcTemplate对象以及对应的获取和设置方法.不用自己在实现类中添加JdbcTemplate变量!!(getJdbcTemplate前没有this也可以)
 

/*
 * dao实现类继承JdbcDaoSupport 然后注入数据源datasource
 */
public class UserDaoImpl extends JdbcDaoSupport implements UserDao{

	/*private JdbcTemplate template;	
	public JdbcTemplate getTemplate() {
		return template;
	}
	public void setTemplate(JdbcTemplate template) {
		this.template = template;
	}
*/
	@Override
	public int add(User user) {
		String sql = "insert into tb_user values(?,?,?)";
		/*int num = this.getTemplate().update(sql, user.getUid(),user.getUname(),user.getUpwd());*/
		int num = this.getJdbcTemplate().update(sql, user.getUid(),user.getUname(),user.getUpwd());
		return num;
	}

	@Override
	public int delete(Long uid) {
		String sql = "delete from tb_user where uid=?";
		int num = this.getJdbcTemplate().update(sql, uid);
		return num;
	}
	@Override
	public int update(User user) {
		String sql = "update tb_user set"
				+"uname=?,"
				+"upwd=? where uid=?";
		return this.getJdbcTemplate().update(sql,
				user.getUname(),
				user.getUpwd(),
				user.getUid());
	}
	@Override
	public User queryById(Long uid) {
		String sql = "select * from tb_user where uid=?";
		User user = this.getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),uid);
		return user;
	}

	@Override
	public List<User> queryAll() {
		String sql = "select * from tb_user";	
		return this.getJdbcTemplate().query(sql,  new BeanPropertyRowMapper<User>(User.class));
	}

	@Override
	public List<User> queryByPage(int currentPage,int pageSize) {
		String sql = "select * from tb_user limit ?,?";
		return this.getJdbcTemplate().query(sql, new BeanPropertyRowMapper<User>(User.class),(currentPage-1)*pageSize,pageSize);
		
	}

	@Override
	public List<User> queryByPage() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Long queryByCount() {
		String sql = "select count(*) from tb_user";
		return this.getJdbcTemplate().queryForObject(sql,new  SingleColumnRowMapper<Long>(Long.class));
	}
}

4、数据库jdbc.properties文件(也可以写db.properties确保与配置文件中的一致),位置: src下/ maven项目 resources文件夹下
 

jdbc.username=root
jdbc.password=123
jdbc.url=jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=utf8
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.maxPoolSize=200
jdbc.initialPoolSize=20

 

4、编写IOC配置文件,文件名:applicationContext.xml,位置:src下 / maven项目 resources文件夹下
 

<?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">
         <!-- 加载jdbc.properties配置文件 
         取内容:${jdbc.username}-->
         <context:property-placeholder location="classpath:jdbc.properties"/>        
    <!-- 连接池交给Spring进行管理 -->
   <!--  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	<property name="username" value="root"></property>
    	<property name="password" value="root"></property>
    	<property name="url" value="jdbc:mysql:///hibrnate_1802?useUnicode=true&amp;characterEncoding=utf8"></property>
    	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean> -->        
    <!-- 配置DBCP连接池 -->
   <!--  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    	<property name="username" value="${jdbc.username}"></property>
    	<property name="password" value="${jdbc.password}"></property>
    	<property name="url" value="${jdbc.url}"></property>
    	<property name="driverClassName" value="${jdbc.driverClassName}"></property>
    </bean>  -->    
    <!-- 配置c3p0连接池 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    	<property name="user" value="${jdbc.username}"></property>
    	<property name="password" value="${jdbc.password}"></property>
    	<property name="jdbcUrl" value="${jdbc.url}"></property>
    	<property name="driverClass" value="${jdbc.driverClassName}"></property>
    	<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    	<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
    </bean>    
    
    <!-- 配置UserDao -->
    <bean id="userDao" class="spring_jdbctemplate_02.UserDaoImpl">
    	<!-- 注入连接池 -->
    	<property name="dataSource"  ref="dataSource"></property>
    </bean>
</beans>

 

发布了77 篇原创文章 · 获赞 13 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览