Spring学习之Spring JDBC
来源:互联网 发布:网络平台不收费标准 编辑:程序博客网 时间:2024/05/19 22:58
Spring学习之Spring JDBC
前言
在日常的开发中,经常需要跟数据库打交道,而JDBC所提供的数据库操作API由于过于底层,而导致了操作非常不方便,所以很多情况下我们会自己进行一个简单的封装,例如之前自己动手写的JDBC封装,这两天在学习Spring的时候,看到Spring也有提供对应的JDBC封装,本着学习的态度,看了一下对应的操作,虽然说现在一般会使用MyBatis等开源框架,不过增加了解还是不错的,接下来的内容简单记录使用Spring JDBC完成数据库的增删改查操作。
Spring JDBC 实战
在使用Spring JDBC的时候,最好是为其注入一个数据源,数据源本身具有良好的池化技术等的特点,比如常用的DBCP、C3P0等数据源,配置如下所示
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--开启组件自动扫描--> <context:component-scan base-package="cn.xuhuanfeng.jdbc"/> <!--配置DBCP数据源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/spring"/> <property name="username" value="root"/> <property name="password" value="huanfeng"/> </bean> <!--配置JdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--注入数据源--> <property name="dataSource" ref="dataSource"/> </bean></beans>
由于JdbcTemplate对JDBC做了薄层封装,所以通过JdbcTemplate来操作数据库变得非常简单了,由于增删改操作均属于更新操作,并且对应的操作内容比较简单,所以这里直接将他们放在一起,具体操作如下所示
@Repositorypublic class Jdbc { // 注入JdbcTemplate对象 @Autowired private JdbcTemplate jdbcTemplate; public void insert(String name, int age){ String sql = "insert into user(name, age) values(?, ?)"; // 通过可变参数注入对应的参数即可 int result = jdbcTemplate.update(sql, name, age); } public void update(String name, int age){ String sql = "update user set age = ? where name = ?"; int result = jdbcTemplate.update(sql, age, name); } public void delete(String name){ String sql = "delete from user where name = ?"; int result = jdbcTemplate.update(sql, name); }}
从上面的代码可以看出,使用Spring JDBC对数据库进行操作非常方便,只需要提供对应的SQL以及参数即可
由于查询操作涉及的返回内容不同,Spring JDBC提供了不同的机制,所以这里将其大致的操作罗列出来
public void findByName(String name){ String sql = "select name, id, age from user where name = ?"; // 传入一个RowMapper,然后编写对应的映射规则 User user = jdbcTemplate.queryForObject(sql, new Object[]{name}, new RowMapper<User>() { @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { User user1 = new User(); user1.setId(resultSet.getInt("id")); user1.setName(resultSet.getString("name")); user1.setAge(resultSet.getInt("age")); return user1; } }); } public void count(){ String sql = "select count(*) from user"; // 查询单值 int count = jdbcTemplate.queryForInt(sql); } public void findAllUser(){ String sql = "select id, name, age from user"; // 提供一个对应的映射规则 List<User> userList = jdbcTemplate.query(sql, new RowMapper<User>() { @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { User user = new User(); user.setId(resultSet.getInt("id")); user.setAge(resultSet.getInt("age")); user.setName(resultSet.getString("name")); return user; } }); } public void queryAllUser(){ String sql = "select id, name, age from user"; // 返回列表,这里需要注意的是,返回的Map其中的每个entry封装了列名以及对应的值 List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql); }
可以看到,Spring JDBC对JDBC进行封装之后,提供了更加方便的操作,极大地提升了效率,而且,由于Spring的处理,我们在使用的时候不需要关心Connection、Statement对象的关闭问题
总结
本小节主要学习了Sping JDBC操作进行学习,并且通过了简单的案例,使用Spring JDBC完成对数据库的增删改查,体会Spring JDBC的方便性
阅读全文
0 0
- Spring学习之Spring JDBC
- Spring学习笔记之jdbc模版
- spring学习笔记: Spring Jdbc
- spring-guide之spring-jdbc
- Spring学习笔记之Spring与JDBC整合
- Spring学习笔记之通过Spring和JDBC征服数据库
- 学习sharding-jdbc 之spring+mybatis+sharding-jdbc整合
- 学习 spring jdbc
- 学习Spring JDBC Framework
- spring学习--集成jdbc
- Spring学习--JDBC
- Spring之JDBC
- Spring DAO之JDBC
- Spring DAO之JDBC
- Spring之JDBC
- Spring JDBC之NamedParameterJdbcTemplate
- spring jdbc之query
- spring之JDBC
- PAT1023~~~
- php_lang_ref:Language Reference >> References Explained >> What References Are
- 字符设备驱动高级篇3——自动创建字符设备驱动的设备文件
- php_lang_ref:Language Reference >> References Explained >> What References Are Not
- 文章标题
- Spring学习之Spring JDBC
- php_lang_ref:Language Reference >> References Explained >> What References Do
- 后缀数组
- php_lang_ref:Language Reference >> References Explained >> Passing by Reference
- 机器学习中的正则化处理
- Oracle-SQL命令复习总结_1
- Round 3 D
- 第一章面试题整理
- 字符设备驱动高级篇4——设备类(自动创建和删除设备文件的作用)相关代码分析