美文网首页
由Spring jdbc 带来的一些练习

由Spring jdbc 带来的一些练习

作者: 遗忘的大志若愚 | 来源:发表于2017-11-19 21:42 被阅读0次

一,Spring加载.properties文件的方式

将.properties文件放在src文件的根目录,在Spring配置文件里进行配置

//这里使用了Spring di
<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value> classpath:jdbc.properties</value>
        </property>
</bean>

二,获取.properties 文件的属性

jdbc.properties文件

//数据库的一些配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/practice
jdbc.username=root
jdbc.password=123456

获取jdbc.properties的值并给DataSource类的一些属性赋值

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

</bean>

三,模板模式

一些公用的代码将它写在一个类中,在真正的业务中,要使用它,只需要继承这个类或者使用组合的方式

例如,现在想要进行操作数据库的一些操作

模板类

public class Template {
  private DataSource dataSource;


//为了使用set注入(di),所以有DataSource的set方法
  public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
  }
//想要使用构造方法注入
  public Template(DataSource dataSource) {

    this.dataSource = dataSource;
  }

  public Template() {

  }

  public void insert(String sql) throws Exception {
    Connection connection = dataSource.getConnection();
    Statement statement = connection.createStatement();
    statement.execute(sql);
  }

}

在业务类中,直接调用insert方法即可,要想实现增加用户的业务一共有四种方式

1,personDao继承于Template,也继承了setDataSource方法,直接在配置文件中赋值

//java
public class PersonDao extends Template {
 public void savePerson()throws Exception{
    this.insert("INSERT INTO ist_library_user (NAME ) VALUES('xixi')");
  }

}
//xml配置文件
<!-- 第一种方式 -->
    <bean id="personDao" class="com.fiberhome.spring.jdbc.PersonDao">
        <property name="dataSource">
            <ref bean="dataSource" />

        </property>
</bean>

2,Spring中的继承,指定两个bean之间父子关系

//java
public class PersonDao1 extends Template {
  public void savePerson()throws Exception{
    this.insert("INSERT INTO ist_library_user (NAME ) VALUES('xixi')");
  }

}
//xml配置文件
<!--第二种方式 -->
    <bean id="template" class="com.fiberhome.spring.jdbc.Template" >
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>
    <bean id="personDao1" class="com.fiberhome.spring.jdbc.PersonDao1"
        parent="template"></bean>
        

3,使用组合的方式,直接将Template给到personDao2中,然后直接用di赋值

//java
public class PersonDao2 {
private  Template template;

 public void setTemplate(Template template) {
  this.template = template;
}


  public void savePerson()throws Exception{
    template.insert("INSERT INTO ist_library_user (NAME ) VALUES('xixi')");
  }

}
//xml配置文件
    <!-- 第三种方式 -->
    <bean id="personDao2" class="com.fiberhome.spring.jdbc.PersonDao2">

        <property name="template">
            <ref bean="template" />
        </property>

    </bean>

4,使用构造器进行di

//java
public class PersonDao3 extends Template {
  public PersonDao3(DataSource dataSource) {
    super(dataSource);
    // TODO Auto-generated constructor stub
  }

  public void savePerson()throws Exception{
    this.insert("INSERT INTO ist_library_user (NAME ) VALUES('xixi')");
  }

}
//xml配置文件
<bean id="personDao3" class="com.fiberhome.spring.jdbc.PersonDao3" >
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>

相关文章

网友评论

      本文标题:由Spring jdbc 带来的一些练习

      本文链接:https://www.haomeiwen.com/subject/ouipvxtx.html