Development of mapper Agent form in MyBatis and spring integration

Source: Internet
Author: User
Tags connection pooling

1. Importing the JAR Package

2. Subcontracting

    • Cogfig: Storing configuration files
    • Mapper: Storing Mappings and interfaces
    • Pojo: Storing entity classes
    • Test: Testing the Code

3. Writing the configuration file

Sqlmapconfig.xml

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE configurationpublic "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><Configuration>    <!--Configure aliases -    <typealiases>        < Packagename= "Com.pojo"/>    </typealiases></Configuration>

Log4j.properties

# Global Logging Configurationlog4j.rootlogger=debug, stdout# Console output...log4j.appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.layout= org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%5p [%t]-%m%n

Db.properties

jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/mybatis01?characterencoding= Utf-8jdbc.username=rootjdbc.password=toor

Applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"xmlns:p= "http://www.springframework.org/schema/p"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"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-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.0.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-4.0.xsd ">    </Beans>

4. Writing entity classes, interfaces, mappings

User.java

 Public classUserImplementsSerializable {Private Static Final LongSerialversionuid = 1L; PrivateInteger ID; PrivateString username;//User name    PrivateString sex;//Sex    PrivateDate birthday;//Birthday    PrivateString address;//AddressSet/get ........ .............}

Usermapper.java

 Public Interface usermapper {        /**     * ID Check user@return         */  Public User Finduserbyid (Integer ID);    }

Usermapper.xml

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE mapperpublic "-//mybatis.org//dtd Mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><Mappernamespace= "Com.mapper.UserMapper">        <!--Check user by ID -    <SelectID= "Finduserbyid"ParameterType= "Int"Resulttype= "User">SELECT * from user where id = #{id}</Select></Mapper>

5. Writing Applicationcontext.xml

Configure database connection pooling

    <!--Load configuration file -    <Context:property-placeholder Location= "Classpath:com/config/db.properties"/>        <!--Database Connection Pool -    <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"Destroy-method= "Close">        < Propertyname= "Driverclassname"value= "${jdbc.driver}" />        < Propertyname= "url"value= "${jdbc.url}" />        < Propertyname= "username"value= "${jdbc.username}" />        < Propertyname= "Password"value= "${jdbc.password}" />        < Propertyname= "Maxactive"value= "Ten" />        < Propertyname= "Maxidle"value= "5" />    </Bean>

Managing the MyBatis Plant

    <!--managing the MyBatis plant -    <BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">        < Propertyname= "DataSource"ref= "DataSource"/>        < Propertyname= "Configlocation"value= "Classpath:com/config/sqlmapconfig.xml"/>    </Bean>

Configuring Mapper Proxy objects

    <!--Mapper Proxy configuration mode one , configure Mapper proxy object -    <BeanID= "Usermapper"class= "Org.mybatis.spring.mapper.MapperFactoryBean">        < Propertyname= "Mapperinterface"value= "Com.mapper.UserMapper"/>        < Propertyname= "Sqlsessionfactory"ref= "Sqlsessionfactory"/>    </Bean>

6. Testing

 public  class   Demo1 {@Test  public  void   M01 () { //  load configuration file  classpathxmlapplicationcontext context = new  Clas        Spathxmlapplicationcontext ("Classpath:com/config/applicationcontext.xml"  //  get mapper  usermapper mapper = (use        Rmapper) Context.getbean ("Usermapper"  //  Execute SQL  User user = Mapper.finduser        Byid (10

How to configure Mapper Agent II ( recommended recommended)

The first configuration method is too cumbersome, how many mapper need to configure how many times

 <!--  Mapper dynamic Agent configuration scan  -->  <  bean  Span style= "COLOR: #ff0000" >class  = " Org.mybatis.spring.mapper.MapperScannerConfigurer " >  <!--  Basic package  -->  <  property  name  = "Basepackage"   value  = "Com.mapper"     />  </ bean  >  

Modify how you get the bean

Usermapper mapper = (usermapper) Context.getbean (usermapper.  Class);

Development of mapper Agent form in MyBatis and spring integration

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.