Spring对象JDBC的支持

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的支持

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值