2017-11-10 22:55:45
Spring Support for persistence layer:
- JDBC:org.springframework.jdbc.core.JdbcTemplate
- Hibernate3.0:org.springframework.orm.hibernate3.hibernatetemplate
- IBatis (MyBatis): org.springframework.orm.ibatis.SqlMapClientTemplate
- JPA:org.springframework.orm.jpa.JpaTemplate
public class JDBC1 { @Test public Void Demo () { //Create Connection pool drivermanagerdatasource DataSource = new Drivermanagerdatasource (); Configuration parameter datasource.setdriverclassname ("Com.mysql.jdbc.Driver"); Datasource.seturl ("Jdbc:mysql://localhost:3306/testdb"); Datasource.setusername ("root"); Datasource.setpassword ("hy1102"); Use the JDBC template jdbctemplate jdbctemplate = new JdbcTemplate (dataSource); Jdbctemplate.execute ("CREATE table user2 (ID int primary KEY auto_increment,name varchar)");} }
Obviously it is inconvenient to create a connection pool with code in the program every time, so we can use spring to help us configure this class.
There are three types of data sources commonly used:
- Spring data source implementation class, Drivermanagerdatasource;
- DBCP data source, Basicdatasource;
- C3P0 data source, Combopooleddatasource;
First, the configuration ofDrivermanagerdatasource
Configuration file:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!--Configure connection pooling-- < Bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" > <property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "jdbc:mysql:// Localhost:3306/testdb "/> <property name=" username "value=" host "/> <property name=" Password " Value= "hy1102"/> </bean> <!--definition template-- <bean id= "JdbcTemplate" class= " Org.springframework.jdbc.core.JdbcTemplate "> <property name=" DataSource "ref=" DataSource "/> </bean></beans>
Test class:
@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:config4.xml") public class JDBC2 { @ Resource (name = "JdbcTemplate") private jdbctemplate JT; @Test Public Void Demo () { Jt.execute ("...");} }
Second,dbcp data source, Basicdatasource configuration
You first need to introduce two jar packages, namely COMMONS.DBCP and Commons.pool. is slightly different from the previous one, but the difference is small.
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "> <!--Configure connection pooling--<!--<bean id=" DataSource "class=" Org.springframework.jdbc.datasource.DriverManagerDataSource ">--> <!--<property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/>--> <!--<property name= "url" value= "jdbc:mysql:/ /localhost:3306/testdb "/>--> <!--<property name=" username "value=" host "/>--> <!--<PR Operty name= "Password" value= "hy1102"/>--> <!--</bean>--> <!--configuration DBCP Connection Pool--<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "driverclassname" value= "COM.M Ysql.jdbc.Driver "/> <property name="URL "value=" Jdbc:mysql://localhost:3306/testdb "/> <property name=" username "value=" host "/> <prop Erty name= "Password" value= "hy1102"/> </bean> <!--definition template--<bean id= "JdbcTemplate" class= "org.sp Ringframework.jdbc.core.JdbcTemplate "> <property name=" dataSource "ref=" DataSource "/> </bean>< /beans>
Third,c3p0 data source, Combopooleddatasource configuration
First, the jar package C3p0-0.9.1.2.jar is introduced.
C3P0 configuration parameters and the first two are not quite the same, this should be noted.
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "> <!--Configure connection pooling--<!--<bean id=" DataSource "class=" Org.springframework.jdbc.datasource.DriverManagerDataSource ">--> <!--<property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/>--> <!--<property name= "url" value= "jdbc:mysql:/ /localhost:3306/testdb "/>--> <!--<property name=" username "value=" host "/>--> <!--<property Name= "Password" value= "hy1102"/>--> <!--</bean>--> <!--configuration DBCP Connection pool-<!--<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >--> <!--<property name= "Driverclassname" Value= "Com.mysql.jdbc.Driver"/>--> <!--<property name= "url" value= "Jdbc:mysql://localhost:3306/testdb"/>--> <!--<property name= "username" val Ue= "Host"/>--> <!--<property name= "password" value= "hy1102"/>--> <!--</bean>--> <!--Configure C3P0 Connection pool--<bean id= "datasource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > < Property Name= "Driverclass" value= "Com.mysql.jdbc.Driver"/> <property name= "Jdbcurl" value= "jdbc:mysql:// Localhost:3306/testdb "/> <property name=" user "value=" host "/> <property name=" password "Valu E= "hy1102"/> </bean> <!--definition template--<bean id= "JdbcTemplate" class= "ORG.SPRINGFRAMEWORK.JDBC.CORE.J Dbctemplate "> <property name=" dataSource "ref=" DataSource "/> </bean></beans>
Iv. writing parameter settings to the properties file
Method One:
Create a new jdbc.properties file under the SRC folder
Jdbc.driver = Com.mysql.jdbc.Driverjdbc.url = Jdbc:mysql://localhost:3306/testdbjdbc.user = Rootjdbc.password = hy1102
Configuration file:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "> <!--Configure connection pooling--<!--<bean id=" DataSource "class=" Org.springframework.jdbc.datasource.DriverManagerDataSource ">--> <!--<property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/>--> <!--<property name= "url" value= "jdbc:mysql:/ /localhost:3306/testdb "/>--> <!--<property name=" username "value=" host "/>--> <!--<property Name= "Password" value= "hy1102"/>--> <!--</bean>--> <!--configuration DBCP Connection pool-<!--<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >--> <!--<property name= "Driverclassname" Value= "Com.mysql.jdbc.Driver"/>--> <!--<property name= "url" value= "Jdbc:mysql://localhost:3306/testdb"/>--> <!--<property name= "username" val Ue= "Host"/>--> <!--<property name= "password" value= "hy1102"/>--> <!--</bean>--> <!--introduce this property file--<bean class= "Org.springframework.context.support.PropertySourcesPlaceholderConfigurer" > <property name= "Location" value= "Classpath:jdbc.properties"/> </bean> <!--configuration c3p0 Connection Pool- -<bean id= "datasource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "Driverc Lass "value=" ${jdbc.driver} "/> <property name=" Jdbcurl "value=" ${jdbc.url} "/> <property name= "User" value= "${jdbc.user}"/> <property name= "password" value= "${jdbc.password}"/> </bean> <!--definition template--<bean id= "JdbcTemplate" class= "Org.springframework.jdbc.core.JdbcTemplate" > <propert Y name= "DataSource" ref= "DatasouRce "/> </bean></beans>
Method Two:
Use the Context tab.
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "> <!--Configure connection pooling--<!--<bean id=" DataSource "class=" Org.springframework.jdbc.datasource.DriverManagerDataSource ">--> <!--<property name=" Driverclassname "Value=" Com.mysql.jdbc.Driver "/>--> <!--<property name=" url "value=" jdbc:mysql://localhost:3306/ TestDB "/>--> <!--<property name=" username "value=" host "/>--> <!--<property name=" Password "V Alue= "hy1102"/>--> <!--</bean>--> <!--configuration DBCP Connection pool-<!--<bean ID= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >--> <!--<property name= "Driverclassnam E "value=" Com.mysql.jdbc.Driver "/>--> <!--<property name=" url "value=" jdbc:mysql://localhost:3306/test DB "/>--> <!--<property name=" username "value=" host "/>--> <!--<property name=" Passwor D "value=" hy1102 "/>--> <!--</bean>--> <!--introduce this property file--<!--<bean class=" Org.springfra Mework.context.support.PropertySourcesPlaceholderConfigurer ">--> <!--<property name=" Location "value= "Classpath:jdbc.properties"/>--> <!--</bean>--> <!--using context tags to introduce property files--<context:prop Erty-placeholder location= "Classpath:jdbc.properties"/> <!--Configure C3P0 Connection pool--<bean id= "DataSource" Clas s= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "driverclass" value= "${jdbc.driver}"/> <property name= "JdbCUrl "value=" ${jdbc.url} "/> <property name=" user "value=" ${jdbc.user} "/> <property name=" pas Sword "value=" ${jdbc.password} "/> </bean> <!--definition template--<bean id=" JdbcTemplate "class=" Org.sprin Gframework.jdbc.core.JdbcTemplate "> <property name=" dataSource "ref=" DataSource "/> </bean></be Ans>
Java spring-jdbctemplate