The previous article was written as an XML file to configure the database connection. But for convenience, we actually use the properties file to configure the database. Modifying the properties file is easier than modifying the XML file.
The practice is:
You will often need to repair the attribute parameter values, configure to the standalone properties file, and then introduce the properties into the XML file
The structure of the entire case is shown first:
The first step: Write the properties file. (New Create a db.properties file)
The contents are as follows:
driver= com.mysql.jdbc.driverurl= jdbc:mysql:///spring3_day2username= Rootpassword=root
Step two: Write spring's applicationcontext.xml configuration file
<!-- Introducing peoperties Files
We use the first notation more often, and the second one is not commonly used because it is complex.
-
<!-- - <!-- -
<!-- the second way to introduce the peoperties file--
<Beanclass= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">< Propertyname= "Locations"value= "Classpath:db.properties"/></Bean> <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> < Propertyname= "Driverclass"value= "${driver}"/><!-- Here the Driver,url,username,password are based on the DB. The Peoperties file is written-- < Propertyname= "Jdbcurl"value= "${url}"></ Property> < Propertyname= "User"value= "${username}"></ Property>< Propertyname= "Password"value= "${password}"></ Property></Bean> <BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate">< Propertyname= "DataSource"ref= "DataSource"/>
Step three: Write the Juit test code. (Jdbctest.java file)
@RunWith (Springjunit4classrunner. Class) @ContextConfiguration (Locations= "Classpath:applicationContext.xml")public class jdbctest { // for annotations (based on type annotations), without this annotation, how spring assigns JdbcTemplate to a value @Autowired private jdbctemplate jdbctemplate; @Test publicvoid jdbctemplatetest () { Jdbctemplate.execute ( "CREATE table person6 (ID int primary key,name varchar)");} }
Use of the 22spring_jdbctemplatem Template Tool Class-Configuration (properties) using an external properties file