Spring 整合 JDBC

首先:

传统的jdbc操作是通过创建数据库连接对象和PreparedStatement来实现。

缺点:

          1.每次jdbc操作都需要创建连接对象和预处理对象

          2.需要开发人员手动处理异常

          3.代码复杂复用率低

try {
			// 原始Jdbc操作
			String sql = "insert into celebrity (c_id,c_name) values (?,?)";
			Connection conn = null;
			PreparedStatement pStatement = null;
			// 加载数据库驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 获取数据库连接对象
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/entertainment?charset=utf8&serverTimezone=UTC", "root", "root");
			// 业务
			pStatement = conn.prepareStatement(sql);
			pStatement.setInt(1, celebrity.getID());
			pStatement.setString(2, celebrity.getName());
			//返回的结果是 the row counts(行数) that were updated
			result = pStatement.executeUpdate();
			// 获取返回结果
			if (0 == result) {
				// 事务回滚
				return result;
			}
			// 关闭资源
			pStatement.close();
			conn.close();

		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}

而Spring框架整合jdbc操作后有诸多好处:

1.集合数据源完成数据库连接对象的管理。

2.通过IOC将需要的对象注入到IOC容器中,解决了对象创建的问题,维护了对象之间的依赖关系。 

3.通过代理模式,将重复代码分离出来,让开发人员专注于业务代码,同时扩展程序的功能提高了代码的复用率。

 

JdbcTemplate主要提供以下几类方法:

            execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
            update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
            query方法及queryForXXX方法:用于执行查询相关语句;
            call方法:用于执行存储过程、函数相关语句。
 

实现        

相关依赖包

spring-jdbc-4.0.5.RELEASE.jar

spring-tx-4.0.5.RELEASE.jar(数据库事务管理)

c3p0\0.9.5.3\c3p0-0.9.5.3.jar(数据源)

mchange-commons-java-0.2.15.jar

mysql-connector-java\6.0.2\mysql-connector-java-6.0.2.jar数据库驱动包

配置数据源和JDBCTemplate对象

Spring对jdbc提供了JdbcTemplate,来简化jdbc操作

<!--注入c3p0数据源对象 -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!--配置c3p0连接池 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver">
		</property>
		<!--数据库连接配置 -->
		<property name="jdbcUrl"
			value="jdbc:mysql://localhost:3306/entertainment?charset=utf8&amp;serverTimezone=UTC&amp;useSSL=false"></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="acquireIncrement" value="2"></property>
	</bean>

    <!--Spring整合jdbc的JDBCTemplate对象  -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

持久层 

@Component("CelebrityDaoImpl")
public class CelebrityDaoImpl implements ICelebrityDao {
	
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
/*
	 * (non-Javadoc)
	 * 
	 * @see com.ncs.dao.ICelebrityDao#getById(int)
	 */
	@Override
	public List<Celebrity> getById(int id) {
		
		String sql="select * from Celebrity";
		
		//获取JDBCTemplate
		List<Celebrity> list=jdbcTemplate.query(sql, new RowMapper<Celebrity>() {
			//封装
			@Override
			public Celebrity mapRow(ResultSet rs, int rowNum) throws SQLException {
				Celebrity celebrity=new Celebrity();
				celebrity.setID(rs.getInt("c_id"));
				celebrity.setName(rs.getString("c_name"));
				return celebrity;
			}
		});

		return list;

	}
}

优点

  • 简化了jdbc的操作
  • 通过代理模式将业务代码与重复代码分离
  • 实现了IOC容器对于对象的创建和依赖管理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值