Spring+ibatis integration (XML configuration file)

Source: Internet
Author: User

The first step is to build the environment of spring, the document structure of the project such as:



The relevant jar packages required are as follows



The second step when the environment is set up, you can write code, before beginning to outline the general process, in the business layer Personserviceimpl to inject Persondao.

1.personserviceimpl.java as follows


<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >/** * @author Wenxue.nong *@2014-12-30 * @version 1.0 * @PersonServiceImpl. java */package Com.test.services.impl; Import Java.util.list;import Com.test.bean.person;import Com.test.dao.persondao;import Com.test.services.personservice;public class Personserviceimpl implements Personservice {// Define the component to inject and implement its setter method private Persondao persondao;public void Setpersondao (Persondao persondao) {This.persondao = Persondao;}        Business method Public       list<person> Findperson () {persondao.fingpersonlist (); return null;}} </span></span>

Of course, he realized a simple interface, the implementation of software interface programming, the interface is as follows Personservice
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >package com.test.services;import java.util.list;import com.test.bean.person;/** * @author wenxue.nong *@ Creation time: 2014-12-30 * @version 1.0 *@ file name: Personservice.java */public interface Personservice {public  List<person > Findperson ();} </span></span>

2. Create the components that need to be injected, here is the DAO layer of the Operational database

Persondaoimpl.java

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >package com.test.dao.impl;import java.util.list;import Com.test.bean.person;import com.test.dao.PersonDao;/** * @author Wenxue.nong *@ creation time: 2014-12-30 * @version 1.0 *@ file name: Persondaoimpl.java */public class Persondaoimpl implements Persondao {        /** 's business here only outputs a word to the console if it can output proof of injection success *       /Public list<person> fingpersonlist () { System.out.println ("I am the findpersonlist of the DAO Layer"); return null;}} </span></span>
interfaces implemented by the Persondaoimpl

Persondao.java

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >/** * @author Wenxue.nong *@2014-12-30 * @version 1.0 * @PersonDao. java */package com.test.dao;import java.util.List; Import com.test.bean.person;/** * @author Wenxue.nong *@ creation time: 2014-12-30 * @version 1.0 *@ file name: Persondao.java */public Interface Persondao {public abstract list<person> fingpersonlist ();} </span></span>

3. Through the configuration file Applicationcontext.xml (name can you do my casual, but generally to make sense)

Applicationcontext.xml

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp:// Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd " ><!--inject-->< through the configuration file!--Persondao to the Spring container management--><bean id= "Persondao" class= " Com.test.dao.impl.PersonDaoImpl "></bean><!--the Personservice to the spring container management, and through the setter way to inject Persondao-- > <bean id= "personservice" class= "Com.test.services.impl.PersonServiceImpl" > <property name= "pe Rsondao "ref=" Persondao "></property> </bean> </beans></span&Gt;</span> 
so our spring project is done, and then we can test it.

4. The test class is as follows

Persontest.java

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >/** * @author Wenxue.nong *@2014-12-29 * @version 1.0 * @PersonTest. java */package com.test;import Static Org.junit.assert.*;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import com.test.services.personservice;/** *@ Author Wenxue.nong *@ creation time: 2014-12-29 * @version 1.0 *@ file name: Persontest.java */public class Persontest {@Testpublic void tes T () {//Start spring container applicationcontext t = new Classpathxmlapplicationcontext ("Config/applicationcontext.xml");// Gets the managed Beanpersonservice Personservice = (personservice) t.getbean ("Personservice") from the container through the ID;// Get the bean after the call inside the business Method Personservice.findperson ();}} </span></span>

4. The results are as follows
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > December 30, 2014 2:05:42 pm org.springframework.context.support.AbstractApplicationContext Preparerefresh Info: refreshing org[email protected]36c14a61:startup Date [Tue Dec 14:05:42 CST 2014]; Root of the context hierarchy December 30, 2014 2:05:42 pm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [Config/applicationcontext.xml] December 30, 2014 2 : 05:42 pm Org.springframework.beans.factory.support.DefaultListableBeanFactory preinstantiatesingletons Info: Pre-instantiating singletons in Org.s[email protected]1efb880e:defining beans [persondao,personservice]; Root of factory hierarchy I am the persondaoimpl</span></span> of the DAO layer
See
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > I am the persondaoimpl</span></span> of the DAO layer
prove that Persondaoimpl has been injected into the personserviceimpl.

The third step when spring has been able to inject success, the next integration Ibatis

How to set the first to import his related package into the project the following connection database is definitely used to drive this is the MySQL driver, connected pool to connect to the connection pool Commons-pool.jar, the data source used to Commons-dbcp.jar,ibatis jar package, Ibatis-sqlmap.jar

Ibatis has two profiles, the total configuration file Sqlmapconfig.xml and xxx.xml corresponds to a domain, here is person.xml

The domain on our side is as follows

Person.java

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >/** * @author Wenxue.nong *@2014-12-30 * @version 1.0 * @Person. java */package com.test.bean;/** * @author wenxue.nong *@ Created time: 2014-12-30 * @version 1.0 *@ file name: Person.java */public class Person {private string id;private string name;} </span></span>
person.xml corresponds to the configuration as follows, and we give him a query SQL statement
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE sqlmap Public "-//ibatis.apache.org//dtd SQL Map 2.0//en" "Http://ibatis.apache.org/dtd/sql-map-2.dtd" > <!--person's configuration--><sqlmap ><typealias type= "Com.test.bean.Person" alias= "person"/>    <select Id= "Ibateisselectperson" resultclass= "person" >     select * from person   </select> </sqlmap></ Span></span>


The Sqlmapconfig.xml is configured as follows

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE sqlmapconfig Public "-//ibatis.com//dtd SQL Map Config 2.0//en" "http://ibatis.apache.org/dtd/ Sql-map-config-2.dtd "><sqlmapconfig><sqlmap resource=" Mapper/person.xml "/></sqlMapConfig> </span></span>
Configure the ibatis configuration file can be integrated with spring Ibatis (integration sounds tall on the look of the applicationcontext.xml in the configuration Ibatis)

The configuration file for the integrated Applicationcontext.xml is as follows

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp:// Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd " ><!--inject-->< through the configuration file!--Persondao to the Spring container management--><bean id= "Persondao" class= " Com.test.dao.impl.PersonDaoImpl "><property name=" sqlmapclient "ref=" sqlmapclient "/><property name="    DataSource "ref=" DataSource "/></bean><!--personservice to spring container management, and is injected by setter mode Persondao- <bean id= "Personservice" class= "Com.test.services.impl.PersonServiceImpl" > <property namE= "Persondao" ref= "Persondao" ></property> </bean> <!--Property-placeholder is a property walker that locates a property file- -<context:property-placeholder location= "classpath:config/application.properties"/> <bean id= "DataSo Urce "class=" Org.apache.commons.dbcp.BasicDataSource "> <property name=" driverclassname "value=" ${jdbc.driver   } "/> <property name=" url "value=" ${jdbc.url} "/><property name=" username "value=" ${jdbc.username} "/> <property name= "Password" value= "${jdbc.password}"/> </bean> <!--injected Ibatis--<be An id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" > <property name= " Configlocation "> <value>classpath:config/sqlMapConfig.xml</value> </property> </bean>& Lt;/beans></span></span>

Persondaoimpl.java changed to the following, compared to the above more inherited
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >sqlmapclientdaosupport class, which has Sqlmapclient and DataSource properties, and implements the setter method, so you can <span style= "FONT-SIZE:14PX;" Injection </span></span></span> in >applicationcontext.xml
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >/** * @author Wenxue.nong *@2014-12-30 * @version 1.0 * @PersonDaoImpl. java */package Com.test.dao.impl;import Java.util.list;import Org.springframework.orm.ibatis.support.sqlmapclientdaosupport;import Com.test.bean.Person; Import com.test.dao.persondao;/** * @author Wenxue.nong *@ creation time: 2014-12-30 * @version 1.0 *@ File name: Persondaoimpl.java */ public class Persondaoimpl extends Sqlmapclientdaosupport implements Persondao {/** * @author wenxue.nong * @return * @time 2 014-12-30 am 10:56:23 */public list<person> fingpersonlist () {System.out.println ("I Am persondaoimpl of the DAO Layer"); list<person> L = this.getsqlmapclienttemplate (). queryForList ("Ibateisselectperson"); return l;}} </span></span>

Personserviceimpl.java changed a little bit into a return value
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >/** * @author Wenxue.nong *@2014-12-30 * @version 1.0 * @PersonServiceImpl. java */package Com.test.services.impl; Import Java.util.list;import Com.test.bean.person;import Com.test.dao.persondao;import com.test.services.personservice;/** * @author Wenxue.nong *@ creation time: 2014-12-30 * @version 1.0 *@ File name: Personserviceimpl.java */public class Personserviceimpl implements Personservice {private Persondao Persondao; public void Setpersondao (Persondao persondao) {This.persondao = Persondao;} /** * Only do one way * @author Wenxue.nong * @return * @time 2014-12-30 morning 10:52:05 */public list<person> Findperson () {list< person> L = persondao.fingpersonlist (); return l;}} </span></span>

Test class
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >/** * @author Wenxue.nong *@2014-12-29 * @version 1.0 * @PersonTest. java */package com.test;import Static Org.junit.assert.*;import Java.util.list;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.test.bean.person;import com.test.services.personservice;/** * @author Wenxue.nong *@ creation time: 2014-12-29 * @version 1.0 *@ File name: Persontest.java */ public class Persontest {@Testpublic void Test () {//Start spring container applicationcontext t = new Classpathxmlapplicationcontext ( "Config/applicationcontext.xml")//Get managed Beanpersonservice Personservice = (personservice) T.getbean from the container through the ID (" Personservice ");//Get the Bean after the call inside the business method list<person> L = Personservice.findperson (); System.out.println (l.get (0));}} </span></span>


The results are as follows


<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > December 30, 2014 3:24:43 pm org.springframework.context.support.AbstractApplicationContext Preparerefresh Info: refreshing org[email protected]26d0fffc:startup date [Tue Dec 15:24:43 CST 2014]; Root of the context hierarchy December 30, 2014 3:24:43 pm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [Config/applicationcontext.xml] December 30, 2014 3 : 24:43 pm Org.springframework.core.io.support.PropertiesLoaderSupport loadproperties Info: Loading properties file from Class path resource [Config/application.properties] December 30, 2014 3:24:43 pm Org.springframework.beans.factory.support.DefaultListableBeanFactory preinstantiatesingletons Information: pre-instantiating singletons in org.s[email protected]45890909:defining beans [Persondao,personservice, Org.springframework.context.support.propertysourcesplaceholderconfigurer#0,datasource,sqlmapclient];Root of factory hierarchy I am the DAO layer's Persondaoimpl Proof injection success [email protected] proves that data can be read from the database </span></span >

The above is the process code for spring integrated Ibatis.

Note: In connection with the database to change to their own database can be application.properties in the flexible modification

Application.properties

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > #develop dbjdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc\:mysql\://localhost\:3306/person?useunicode\=true &characterEncoding\=utf-8jdbc.username=rootjdbc.password=root</span></span>

The following is the source code, the source has person.sql files, in the import of their own project as long as the SRC and Webroot copied to the project on the line, when the test error remember to import the JUnit 4 jar package

Person.sql to import your own database with Navicat


Download link Click to open link






Spring+ibatis integration (XML configuration 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.