DataSource能够集中管理数据库连接,降低维护工作量,使部署更简单;
Spring的DataSource配置:(Spring数据源配置)这里使用dbcp,还有非常多其它的如c3p0,jdbc,jndi等
DataSource和定义普通Bean一样,指定id、类别,注入属性;
网站停止时,应该调用DataSource的close方法,通过destroy-method属性配置实现;

1,在Spring配置文件配置DataSource的bean:


class="org.apache.commons.dbcp.BasicDataSource" :是DataSource实现类
配置SessionFactory ,注入:


2,数据库连接信息写在属性文件里,在src新建配置文件:jdbc.properties

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/jboa
jdbc.username = root
jdbc.password = jerome
在Spring配置文件引入jdbc.properties配置文件,採用PropertyPlaceholderConfigurer能够引入属性文件,在Spring配置文件里採用${jdbc.url}的方式引用属性值

接下来能够把一下的Hibernat的配置数据连接信息凝视:

3,配置hibernateProperties属性,用到了Property类型的属性的配置配置SessionFactory :

Hibernate能够凝视:

5,通过mappingDirectoryLocations 或者mappingLocations属性配置映射文件位置,把全部的资源文件引进Spring配置:配置SessionFactory

DataSource指定一个方法关闭:

<bean >
配置好了。Hibernate能够删除了;执行測试成功;





相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
  • 2022-12-23
  • 2021-06-25
猜你喜欢
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2022-02-24
相关资源
相似解决方案