读取配置文件连接数据库

spring读取配置文件的三种方式:
读取property文件,username是个关键字,默认是操作用户Administrator

需要导入jar包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>




1.bean来读取配置文件(不推荐使用):配置文件中没定义username默认读取Administrator
定义了username后读取本地配置的信息
 
<bean id=""  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:property文件路径"></property>
</bean>


2.context 命名空间 读取配置文件  username代表读取Administrator,本地定义username没用


配置:版本信息可以自己更改
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="">中加入 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd


<context:property-placeholder  location="propert配置文件路径"/>


<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
 
    <property name="driverClassName"  value="${driverClassName}" />
 
     <property name="url" value="${url}" />
 
     <property name="username" value="${usernamed}" />
 
     <property name="password" value="${password}" />
 
    </bean>


<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

3.定义配置类


@Configuration
public class Myconf {
    @Value("${url}")
    private String url;
    @Value("${driverClassName}")
    private String driverClassName;
    @Value("${usernamed}")
    private String username;
    @Value("${password}")
    private String password;



//返回值类型:class类名,方法名=id
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dmds = new DriverManagerDataSource();
        dmds.setUrl(url);
        dmds.setDriverClassName(driverClassName);
        dmds.setUsername(username);
        dmds.setPassword(password);
        return dmds;
    }

xml文件中保留:
<context:property-placeholder  location="propert配置文件路径"/>
<!--扫描注解的范围-->
<context:component-scan base-package="lesson2"></context:component-scan>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 读取保存App.config配置文件的完整源码参考(转) http://smartsoft.5d6d.com/thread-6550-1-1.html C# 读取保存App.config配置文件的完整源码参考 最近出差在北京做一个小项目,项目里需要读取配置文件的小功能,觉得挺有参考意义的就把代码发上来给大家参考一下。我们选择了直接用微软的读取配置文件的方法。 这个是程序的运行设计效果,就是把这些参数可以进行灵活设置,灵活保存设置状态。 程序编译后自动会产生相应的配置文件,是跟项目的名称一样的配置文件读取配置文件及保存配置的具体代码参考如下,希望能给你节省一些时间,直接复制粘贴这个代码就可以用了: //------------------------------------------------------------ // All Rights Reserved , Copyright (C) 2010 , CDPF , Ltd. //------------------------------------------------------------ using System; using System.Configuration; using System.Windows.Forms; using Utilities; namespace DirectSeeding { /// /// FrmConfig /// 读取配置文件 /// /// 修改纪录 /// /// 2011.01.14 版本: 1.0 JiRiGaLa 完善程序的注释等、从新整理代码。 /// /// 版本:1.0 /// /// /// JiRiGaLa /// 2011.01.14 /// /// public partial class FrmConfig : Form { public FrmConfig() { InitializeComponent(); } /// /// 读取配置文件 /// private void GetConfig() { this.txtWriteFileName.Text = ConfigurationManager.AppSettings["WriteFileName"]; this.txtWritePath.Text = ConfigurationManager.AppSettings["WritePath"].Replace("|", Environment.NewLine); this.txtPostMessageURL.Text = ConfigurationManager.AppSettings["PostMessageURL"]; this.txtLeasedLineURL.Text = ConfigurationManager.AppSettings["LeasedLineURL"]; } private void FrmDirectSeeding_Load(object sender, EventArgs e) { this.GetConfig(); } /// /// 保存配置文件 /// private void SaveConfig() { // 写入参数设置 Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); configuration.AppSettings.Settings["WriteFileName"].Value = this.txtWriteFileName.Text; configuration.AppSettings.Settings["Wri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值