栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring框架配置数据源

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring框架配置数据源

回顾1
手动创建数据源
在pom.xml中

	
          
          mysql
          mysql-connector-java
          5.1.6
      
      
          
          com.alibaba
          druid
          1.1.9
      

测试

public class DruidDataSourceTest {
    @Test
    public void testDruidDataSource() throws Exception{
        //测试手动创建德鲁伊数据源
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        DruidPooledConnection connection = druidDataSource.getConnection();
        System.out.println(connection);
        connection.close();

        //在这里补充一个知识点: 当maven的mysql驱动jar版本是8时, 数据源信息还要有时区信息, 如果是版本5, 就不用
    }
}
12月 15, 2021 11:48:20 上午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
com.mysql.jdbc.JDBC4Connection@7ba18f1b

回顾2
抽取数据库的配置文件(注意 : 为什么抽取成properties而不是xml, 因为xml解析麻烦了点)
在resources下创建jdbc.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000

测试

public class DruidDataSourceTest {
    @Test
    public void testDruidDataSource() throws Exception{
        //测试手动创建德鲁伊数据源, 使用配置文件
        ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc"); //这里的这个方法的参数是配置文件的名称, 简称基名, 加载的是根路径下也即是resources下的配置文件
        //获取配置文件信息
        String driver = resourceBundle.getString("driverClassName");
        String url = resourceBundle.getString("url");
        String username = resourceBundle.getString("username");
        String password = resourceBundle.getString("password");
        String initialSize = resourceBundle.getString("initialSize");
        String maxActive = resourceBundle.getString("maxActive");
        String maxWait = resourceBundle.getString("maxWait");
        //创建数据源对象, 并设置信息
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setInitialSize(Integer.parseInt(initialSize));
        druidDataSource.setMaxActive(Integer.parseInt(maxActive));
        druidDataSource.setMaxWait(Integer.parseInt(maxWait));
        DruidPooledConnection connection = druidDataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }


}
12月 15, 2021 12:12:38 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
com.mysql.jdbc.JDBC4Connection@13c10b87
使用spring框架来配置数据源 (1) 没有抽取配置文件的方式
	
    
        
        
        
        
    
public class DruidDataSourceTest {
    @Test
    public void testDruidDataSource() throws Exception{
        //使用spring容器来获取数据源对象
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        DruidDataSource druidDataSource = app.getBean(DruidDataSource.class);
        DruidPooledConnection connection = druidDataSource.getConnection();
        System.out.println(connection);
        connection.close();

    }
}
12月 15, 2021 12:32:36 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3796751b: startup date [Wed Dec 15 12:32:36 CST 2021]; root of context hierarchy
12月 15, 2021 12:32:36 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
12月 15, 2021 12:32:37 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
com.mysql.jdbc.JDBC4Connection@dc7df28

小结 : 当我们对配置的bean进行资源注入时, 一般是使用的set方法, 也就是使用财产标签, name属性的值就是对应具体的类中的set方法名, 最好知道set方法的样子, 不会弄错, 不同的类的set方法有可能不一样.

(2) 抽取配置文件的方式

applicationContext.xml如果想加载jdbc.properties配置文件获取连接信息
首先必须得context命名空间和约束路径
命名空间

xmlns:context="http://www.springframework.org/schema/context"

约束路径

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

合起来就是


记不住也没事, 其实复制修改就可以

有了context命名空间之后
在applicationContext.xml中加载properties文件

    
    

现在就加载进去了
使用spring表达式就可以拿到properties中的信息
${key}

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
	
    
        
        
        
        
    

大家是不是以为这样就好了
其实有一个bug
这样去测试是有异常的
这里
${username}的读取值为本应为jdbc.properties文件对应的是username的值但实际是系统用户名Administrator(windows系统)

将properties文件的key改变一下就可以了

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test
jdbc.username=root
jdbc.password=root
public class DruidDataSourceTest {
    @Test
    public void testDruidDataSource() throws Exception{
        //使用spring容器来获取数据源对象
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //DruidDataSource druidDataSource = app.getBean(DruidDataSource.class);
        DruidDataSource druidDataSource = (DruidDataSource) app.getBean("druidDataSource");
        DruidPooledConnection connection = druidDataSource.getConnection();
        System.out.println(connection);
        connection.close();

    }
}
12月 15, 2021 2:59:28 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3796751b: startup date [Wed Dec 15 14:59:28 CST 2021]; root of context hierarchy
12月 15, 2021 2:59:28 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
12月 15, 2021 2:59:28 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
com.mysql.jdbc.JDBC4Connection@24959ca4

小结 : 也就是说, 以后jdbc的配置文件的键名, 最好写成这样的形式

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666258.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号