Configuring MyBatis in Spring

Source: Internet
Author: User

      First configure Proxool data sources in spring,  we're going to src/main/resources/spring-config/. Set up the spring-mybatis.xml. Put the sqlsession in the spring container manage:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
	Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 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/con Text/spring-context-4.1.xsd Http://www.springframework.org/schema/mvc Http://www.springframework . org/schema/mvc/spring-mvc-4.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.or G/schema/aop/spring-aop-4.1.xsd "> <!--configuration sqlsessionfactory--> <bean id=" sqlsessionfactory "class=" org.m Ybatis.spring.SqlSessionFactoryBean "> <property name="DataSource "ref=" DataSource "/> <property name=" mapperlocations "value=" Classpath:test/mybabitsmapping/*.xml "/> </bean> <!--configuration scanner--> <bean class=" Org.mybatis.spring.mapper.MapperScannerConfigurer "&
        Gt <property name= "Basepackage" value= "Test.ssm.mapper"/> <property name= "Sqlsessionfactorybeanname" value=     
            "Sqlsessionfactory"/> </bean> <bean id= "sqlsession" class= "Org.mybatis.spring.SqlSessionTemplate" > <constructor-arg index= "0" ref= "sqlsessionfactory"/> </bean> </beans>


Now test, generate the following document with the MyBatis build tool:



Open a JUnit test because this is a test that is out of the web environment, so you need to manually load the Proxool connection pool:

Package test.testmybatis;

Import Java.io.File;
Import Org.junit.After;
Import Org.junit.Assert;
Import Org.junit.Before;
Import Org.junit.Test;
Import org.logicalcobwebs.proxool.ProxoolException;
Import Org.logicalcobwebs.proxool.ProxoolFacade;
Import Org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
Import Org.mybatis.spring.SqlSessionTemplate;
Import Org.springframework.context.ApplicationContext;

Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Test.ssm.mapper.TaUserMapper;

Import Test.ssm.model.TaUser; 
	public class Test1 {ApplicationContext context; public void Loadproxool () throws proxoolexception{try {//Get configuration file handle String lvspath=class.class.getclass (). Getreso
			Urce ("/"). GetPath (); Lvspath=lvspath+ ". /..
			/src/main/webapp/web-inf/proxool.xml ";
			File File = new file (Lvspath);
			
		The unit test does not start the Web service, so you want to load Proxool configurator jaxpconfigurator.configure (Lvspath, false). catch (Proxoolexception e) {throw e;
		@Before public void SetUp () throws Exception {Loadproxool ();
	Context =new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
	@After public void teardown () throws Exception {proxoolfacade.shutdown (0); @Test public void Test () {org.mybatis.spring.SqlSessionTemplate lvsqlsession= (sqlsessiontemplate) Context.getbean
		("sqlsession");		 
	    Tausermapper lvusermapper= Lvsqlsession.getmapper (tausermapper.class);
	Tauser Lvuseritem=lvusermapper.selectbyprimarykey ("admin");//update from Desktop Assert.assertnotnull (LvUserItem);
 }

}

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.