Spring中JDBC的模板的使用

矫情吗;* 2022-12-06 05:24 195阅读 0赞

Spring中JDBC的模板的使用

Spring的JDBC的模板

Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。Spring对持久层也提供了解决方案:ORM模块和JDBC的模板。

创建项目,引入jar包:
引入基本开发包:4 + 2。
数据库驱动包。
需要引入spring_aop的jar包,不然报错。
Spring的JDBC模板的jar包。


1、创建表

  1. CREATE TABLE `t_act` (
  2. `no` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(20) DEFAULT NULL,
  4. `balance` double(8,2) DEFAULT '0.00',
  5. PRIMARY KEY (`no`)
  6. ) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8

2、使用JDBC的模板:保存数据

  1. @Test
  2. public void test01() {
  3. // 创建连接池对象
  4. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  5. // 设置连接数据库的基本信息
  6. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  7. dataSource.setUrl("jdbc:mysql://localhost:3306/study_test");
  8. dataSource.setUsername("root");
  9. dataSource.setPassword("666666");
  10. // 创建jdbc模板
  11. JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  12. jdbcTemplate.update("insert into t_act values(null, ?, ?)", "王五", 2000d);
  13. }

3、将连接池和模板交给Spring管理

(1)编写Spring的配置文件

  1. <!-- 配置Spring内置的连接池 -->
  2. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  3. <!-- 属性注入 -->
  4. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  5. <property name="url" value="jdbc:mysql://localhost:3306/study_test" />
  6. <property name="username" value="user" />
  7. <property name="password" value="666666" />
  8. </bean>
  9. <!-- 配置Spring的jdbc模板 -->
  10. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  11. <property name="dataSource" ref="dataSource" />
  12. </bean>

(2)再使用Jdbc的模板

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(value = "classpath:applicationContext.xml")
  3. public class JdbcTest02 {
  4. @Resource(name = "jdbcTemplate")
  5. private JdbcTemplate jdbcTemplate;
  6. @Test
  7. public void test01() {
  8. jdbcTemplate.update("insert into t_act values(null, ?, ?)", "赵六", 2000d);
  9. }
  10. }

4、使用开源的数据库连接池

(1)DBCP的使用

在Spring配置文件中配置DBCP连接池

  1. <!-- 配置DBCP的数据库连接池 -->
  2. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  3. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  4. <property name="url" value="jdbc:mysql://localhost:3306/study_test" />
  5. <property name="username" value="root" />
  6. <property name="password" value="666666" />
  7. </bean>
  8. <!-- 配置Spring的jdbc模板 -->
  9. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  10. <property name="dataSource" ref="dataSource" />
  11. </bean>

(2)C3P0的使用

在Spring配置文件中配置c3p0连接池

  1. <!-- 配置C3P0的数据库连接池,注意该类中的属性名称 -->
  2. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  3. <property name="driverClass" value="com.mysql.jdbc.Driver" />
  4. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/study_test" />
  5. <property name="user" value="root" />
  6. <property name="password" value="666666" />
  7. </bean>
  8. <!-- 配置Spring的jdbc模板 -->
  9. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  10. <property name="dataSource" ref="dataSource" />
  11. </bean>

5、抽取数据库连接信息到属性文件

在实际开发中,一般不会把数据库的连接信息直接写在配置文件中。
而是另外写在属性文件中,在配置文件中去读取配置文件中的信息。

(1)定义一个属性文件jdbc.properties

  1. jdbc.driverClass=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/study_test
  3. jdbc.username=root
  4. jdbc.password=666666

(2)在Spring的配置文件中引入属性文件

第一种:通过一个bean标签引入属性文件(很少使用)

  1. <!-- 第一种:通过bean标签的引入(这种方式很少使用) -->
  2. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  3. <property name="location" value="classpath:jdbc.properties" />
  4. </bean>

第二种:通过context标签引入属性文件(常用)

  1. <!-- 第二种:通过context标签的引入 -->
  2. <context:property-placeholder location="classpath:jdbc.properties" />

(3)在连接池中获取属性文件的值

  1. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  2. <property name="driverClass" value="${jdbc.driverClass}" />
  3. <property name="jdbcUrl" value="${jdbc.url}" />
  4. <property name="user" value="${jdbc.username}" />
  5. <property name="password" value="${jdbc.password}" />
  6. </bean>

(4)最终Spring配置文件内容为

  1. <!-- 第二种:通过context标签的引入 -->
  2. <context:property-placeholder location="classpath:jdbc.properties" />
  3. <!-- 配置C3P0的数据库连接池,注意该类中的属性名称,值读取属性文件 -->
  4. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  5. <property name="driverClass" value="${jdbc.driverClass}" />
  6. <property name="jdbcUrl" value="${jdbc.url}" />
  7. <property name="user" value="${jdbc.username}" />
  8. <property name="password" value="${jdbc.password}" />
  9. </bean>
  10. <!-- 配置Spring的jdbc模板 -->
  11. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  12. <property name="dataSource" ref="dataSource" />
  13. </bean>

6、使用JDBC的模板完成CRUD的操作

(1)保存操作

  1. // insert操作:增删改用update(),查询用query()
  2. @Test
  3. public void test01() {
  4. jdbcTemplate.update("insert into t_act values(null, ?, ?)", "赵六", 2000d);
  5. }

(2)删除操作

  1. // delete操作
  2. @Test
  3. public void test02() {
  4. jdbcTemplate.update("delete from t_act where id=?", 1009);
  5. }

(3)修改操作

  1. // update操作
  2. @Test
  3. public void test03() {
  4. jdbcTemplate.update("update t_act set balance = ? where id = ?", 1000d, 1004);
  5. }

(4)查询操作

(01)查询某个属性:

  1. // select操作:查询某一个属性
  2. @Test
  3. public void test04() {
  4. // 查询一个字段
  5. Double balance = jdbcTemplate.queryForObject("select balance from t_act where id=?", Double.class, 1001);
  6. System.out.println(balance); // 2000.0
  7. // 查询统计个数
  8. Long count = jdbcTemplate.queryForObject("select count(*) from t_act", Long.class);
  9. System.out.println(count); // 4
  10. }

(02)查询返回一条记录,封装到对象中:

  1. // 查询一条记录,封装到一个对象中
  2. @Test
  3. public void test05() {
  4. Account account = jdbcTemplate.queryForObject("select * from t_act where id=?", new MyRowMapper(), 1001);
  5. System.out.println(account); // Account{id=1001, name='张三', balance=2000.0}
  6. }

(03)查询多条记录,封装到List集合中:

  1. // 查询多条记录,封装到List集合中
  2. @Test
  3. public void test06() {
  4. List<Account> list = jdbcTemplate.query("select * from t_act", new MyRowMapper());
  5. for (Account a : list) {
  6. System.out.println(a);
  7. }
  8. }

(04)数据封装

  1. public class Account {
  2. private Integer id;
  3. private String name;
  4. private Double balance;
  5. // ......
  6. }
  7. class MyRowMapper implements RowMapper<Account> {
  8. @Override
  9. public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
  10. Account act = new Account();
  11. act.setId(rs.getInt("id"));
  12. act.setName(rs.getString("name"));
  13. act.setBalance(rs.getDouble("balance"));
  14. return act;
  15. }
  16. }

发表评论

表情:
评论列表 (有 0 条评论,195人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring JDBC

    1.1 简介   JDBC(Java Data Base Connectivity)是一种用于执行 SQL 语句的 Java APl,可以为多种关系型数据库提供统一访问,