1. The XML file of spring is configured as follows:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5 Xmlns:tx= "Http://www.springframework.org/schema/tx"6 Xmlns:context= "Http://www.springframework.org/schema/context"7 xsi:schemalocation="8 Http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans-3.1.xsdTen Http://www.springframework.org/schema/tx One http://www.springframework.org/schema/tx/spring-tx-3.1.xsd A Http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context.xsd - HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP the http://www.springframework.org/schema/aop/spring-aop-3.1.xsd - "> - - <!--1. Read the Propertiser configuration file - + <Context:property-placeholder Location= "Classpath:jdbc.properties"/> - <!--2. Jndi Get Data source (using DBCP connection pool) - + <!--when the srping container stops destroying, the close connection pool is triggered and a singleton mode - A <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource" at Destroy-method= "Close"Scope= "Singleton"> - < Propertyname= "Driverclassname"value= "${driver}"/> - < Propertyname= "url"value= "${url}"/> - < Propertyname= "username"value= "${jdbc.username}"/> - < Propertyname= "Password"value= "${password}"/> - in </Bean> - to <!--Management of things using srping-jdbc things management - + <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> - < Propertyname= "DataSource"ref= "DataSource"/> the </Bean> * $ <!--Configuring the MyBatis sqlsessionfactory -Panax Notoginseng <BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean"> - < Propertyname= "DataSource"ref= "DataSource"/> the < Propertyname= "Configlocation"value= "Classpath:mybatis-config.xml"/><!--introducing the Mybits configuration file - + < Propertyname= "Mapperlocations"value= "Classpath:com/bdqn/dao/*.xml"/><!--Mapper file that introduces XML - A </Bean> the + <!--Configuring the MyBatis sqlsessiontemplate - - <BeanID= "Sqlsessiontemplate"class= "Org.mybatis.spring.SqlSessionTemplate"> $ <Constructor-argname= "Sqlsessionfactory"ref= "Sqlsessionfactory"/><!--using construction injection sqlsessionfactory, get sqlsessiontemplate - $ </Bean> - - <!--To inject sqlsessiontemplate into the test class, the test class can use Sqlsessiontemplate. - the <BeanID= "Employeetest"class= "Com.bdqn.test.EmployeeTest"> - < Propertyname= "Sqlsessiontemplate"ref= "Sqlsessiontemplate"/>Wuyi the </Bean> - </Beans>
2. The XML configuration file for MyBatis is as follows:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Configuration3 Public "-//mybatis.org//dtd Config 3.0//en"4 "Http://mybatis.org/dtd/mybatis-3-config.dtd">5 <Configuration>6 <typealiases>7 < Packagename= "Com.bdqn.po"/><!--all the classes inside the PO package have their own class name alias -8 </typealiases>9 Ten </Configuration>
3, because MyBatis SQL statement is written in the XML file, this time using the data source connection pool. So first introduce the test class in the Spring XML file:
<bean id= "Employeetest" class= "Com.bdqn.test.EmployeeTest" >
When you test SQL with Juit, the spring XML file is then load into context: ApplicationContext,
by Applicationcontext.getbean ("Employeetest") to the test class, it can be manipulated throughout the spring container dependency relationship. To get the test class, call the Sqlsession template:
Sqlsessiontemplate , using the template of various additions and deletions of the method, the code is as follows:
1 Packagecom.bdqn.test;2 3 4 Importjava.util.List;5 6 ImportOrg.apache.log4j.Logger;7 Importorg.junit.Test;8 Importorg.mybatis.spring.SqlSessionTemplate;9 ImportOrg.springframework.context.ApplicationContext;Ten ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; One A ImportCom.bdqn.po.Employee; - - Public classEmployeetest { the PrivateLogger Logger = Logger.getlogger (employeetest.class); - - Privatesqlsessiontemplate sqlsessiontemplate; - protected ApplicationContextAplcation =NULL;//attention-Draw Package + Privateemployeetest test; - + A @Test at Public voidgetemployeelist () { -Aplcation =NewClasspathxmlapplicationcontext ("Applicationcontext-spring.xml");//Initialize the context; -Test = (employeetest) aplcation.getbean ("Employeetest"); - -list<employee> list = Test.getsqlsessiontemplate (). SelectList ("Com.bdqn.dao.EmployeeMapper.getEmployeeList"); -Logger.debug ("Employee-----" +list.size ()); in - to } + - the Publicsqlsessiontemplate getsqlsessiontemplate () { * returnsqlsessiontemplate; $ }Panax Notoginseng Public voidsetsqlsessiontemplate (sqlsessiontemplate sqlsessiontemplate) { - This. sqlsessiontemplate =sqlsessiontemplate; the } + A the +}
Note:
Ran the Times with a database connection error:
Access denied for user ' Administrator ' @ ' localhost ' (using Password:yes)
Looking for a long time can not find the reason, the user name, password, driver, etc. absolutely is not wrong, and later in the XML file directly write the value there is no problem, know the problem in this properties file, a change of the test, found to be this: Username=root, As long as the change of this username, Baidu has said it may be introduced in the introduction of the same name as other username. ---wasted the night!!! I remember!!!
Driver=com.mysql.jdbc.driver
Url=jdbc:mysql://127.0.0.1:3306/testdb
Username=root
password=123456
Spring Integrated mybatis framework, JUnit test