Use of the 22spring_jdbctemplatem Template Tool class-configuration using an external properties file (properties)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.