How to Define a MySql datasource bean via XML in Spring

前端 未结 3 1003
不思量自难忘°
不思量自难忘° 2020-12-09 05:09

I\'ve looked over the documentation to define a bean. I\'m just unclear on what class file to use for a Mysql database. Can anyone fill in the bean definition below?

3条回答
  •  离开以前
    2020-12-09 05:28

    Use this class org.springframework.jdbc.datasource.DriverManagerDataSource - DriverManagerDataSource. As a best practice its better if we isolate the database values into a .properties file and configure it into our spring servlet xml configuration. In the below example the properties are stored as key-value pairs and we access the value using the corresponding key.

    applicationContext-dataSource.xml:

    
    
    
        
        
        
        
        
    
    
    
    

    jdbc.propeties file:

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/sample_db
    jdbc.username=root
    jdbc.password=sec3ret
    

提交回复
热议问题