JAVA框架 Spring 调用jdbcsuport简化开发

一)使用DAO的jdbcsuport来简化开发

首先来清楚一个概念:

我们在进行配置文件来进行依赖注入的时候,主要是通过set方法来进行设置的。

正常我们使用spring的jdbctemplate的时候,我们需要注入DataSource和jdbctemplate两个类。而jdbcsuport帮我们做了这些事情。所以我们只需要在Dao层继承这类即可。

一起来看jdbcsuport源码:

首先提供了字段jdbctemplate字段:

然后分别给提供了该字段的set方法和get方法:

也就是说我们在获取该字段(private修饰),设置该字段(提供注入),可以给配置文件进行依赖注入。

然后在看:

如果获取jdbctemplate的时候,是null,会调用createJdbcTemplate方法,new jdbctemplate的对象。

所以现在我们可以不依赖注入:jdbctemplate。只是注入DataSource即可。

这种方法,其实并不好,因为spring 一直强调低耦合,实例化交给IOC处理。

完整代码例子:

Dao'层:

 1 //
 2 // Source code recreated from a .class file by IntelliJ IDEA
 3 // (powered by Fernflower decompiler)
 4 //
 5 
 6 package org.springframework.jdbc.core.support;
 7 
 8 import java.sql.Connection;
 9 import javax.sql.DataSource;
10 import org.springframework.dao.support.DaoSupport;
11 import org.springframework.jdbc.CannotGetJdbcConnectionException;
12 import org.springframework.jdbc.core.JdbcTemplate;
13 import org.springframework.jdbc.datasource.DataSourceUtils;
14 import org.springframework.jdbc.support.SQLExceptionTranslator;
15 
16 public abstract class JdbcDaoSupport extends DaoSupport {
17     private JdbcTemplate jdbcTemplate;
18 
19     public JdbcDaoSupport() {
20     }
21 
22     public final void setDataSource(DataSource dataSource) {
23         if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
24             this.jdbcTemplate = this.createJdbcTemplate(dataSource);
25             this.initTemplateConfig();
26         }
27 
28     }
29 
30     protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
31         return new JdbcTemplate(dataSource);
32     }
33 
34     public final DataSource getDataSource() {
35         return this.jdbcTemplate != null ? this.jdbcTemplate.getDataSource() : null;
36     }
37 
38     public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
39         this.jdbcTemplate = jdbcTemplate;
40         this.initTemplateConfig();
41     }
42 
43     public final JdbcTemplate getJdbcTemplate() {
44         return this.jdbcTemplate;
45     }
46 
47     protected void initTemplateConfig() {
48     }
49 
50     protected void checkDaoConfig() {
51         if (this.jdbcTemplate == null) {
52             throw new IllegalArgumentException("'dataSource' or 'jdbcTemplate' is required");
53         }
54     }
55 
56     protected final SQLExceptionTranslator getExceptionTranslator() {
57         return this.getJdbcTemplate().getExceptionTranslator();
58     }
59 
60     protected final Connection getConnection() throws CannotGetJdbcConnectionException {
61         return DataSourceUtils.getConnection(this.getDataSource());
62     }
63 
64     protected final void releaseConnection(Connection con) {
65         DataSourceUtils.releaseConnection(con, this.getDataSource());
66     }
67 }

service层:

 1 package jd.com.UserService;
 2 
 3 
 4 import jd.com.UserDao.userdao;
 5 import org.springframework.stereotype.Service;
 6 
 7 import javax.annotation.Resource;
 8 
 9 
10 public class UserServiceImpl implements UserService {
11 
12     private userdao userdaoIMpl;
13 
14     public void setUserdaoIMpl(userdao userdaoIMpl) {
15         this.userdaoIMpl = userdaoIMpl;
16     }
17 
18     @Override
19     public void toaccount(int mon1, int mon2) {
20 
21         System.out.println(this.userdaoIMpl);
22         userdaoIMpl.addMoney(mon1);
23         userdaoIMpl.delMoney(mon2);
24     }
25 }

配置文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8     http://www.springframework.org/schema/beans/spring-beans.xsd
 9     http://www.springframework.org/schema/context
10     http://www.springframework.org/schema/context/spring-context.xsd
11     http://www.springframework.org/schema/aop
12     http://www.springframework.org/schema/aop/spring-aop.xsd
13     http://www.springframework.org/schema/tx 
14     http://www.springframework.org/schema/tx/spring-tx.xsd">
15 
16 
17 
18 
19     <context:component-scan base-package="jd.com" />
20 
21     <bean  id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
22         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
23         <property name="url"  value="jdbc:mysql://localhost:3306/day_spring" />
24         <property name="username" value="root" />
25         <property name="password" value="root" />
26     </bean>
27     <bean class="jd.com.UserDao.userdaoIMpl" id="userdao">
28         <property name="dataSource" ref="dataSource" />
29     </bean>
30     <bean id="userService" class="jd.com.UserService.UserServiceImpl">
31         <property name="userdaoIMpl" ref="userdao" />
32     </bean>
33 
34 </beans>

注意:

       这里采用的xml配置文件方式进行注入和依赖注入。

  如果都使用注解,会导致datasource无法注入的情况。

个人认为合理方式:不继承jdbcsuport这个类。

原文地址:https://www.cnblogs.com/evilliu/p/8897371.html