Mybatis Mapper Dynamic Proxy mode

Source: Internet
Author: User

Directory structure and configuration files are simpler than the original DAO method

Simply a usermapper interface, put together the configuration file, namespace the address of the configuration file to determine the JDK Dynamic agent object

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE mapperpublic "-//mybatis.org//dtd Mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><!--namespace: Namespaces for isolating SQL -<!--developing dao,1. Namespace with dynamic agents must be consistent with Mapper interface Classpath -<Mappernamespace= "Com.swift.dao.UserMapper">    <!--query users based on user ID -    <!--2. The ID must match the Mapper interface method name -    <!--3. ParameterType must be consistent with the interface method parameter type -    <!--4. Resulttype must be consistent with the interface method return value type -    <SelectID= "Queryuserbyusername"ParameterType= "String"Resulttype= "Com.swift.pojo.User">SELECT * FROM user where username = #{username}</Select></Mapper>

MyBatis The mapping section of the configuration file is easier

<?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 -<Configuration>    <PropertiesResource= "Db.properties"></Properties>    <Environmentsdefault= "Development">        <!--data sources for development -        <EnvironmentID= "Development">            <TransactionManagertype= "JDBC" />            <DataSourcetype= "Pooled">                < Propertyname= "Driver"value= "${jdbc.driver}" />                < Propertyname= "url"value= "${jdbc.url}" />                < Propertyname= "username"value= "${jdbc.username}" />                < Propertyname= "Password"value= "${jdbc.password}" />            </DataSource>        </Environment>    </Environments>    <!--Load SQL mapping file -    <mappers>    <!--When you move the SQL mapping file to the interface file (the way that the JDK proxy does not need to implement the Class), you can replace the resource property with the Class property by using the same file name under a package (the interface is renamed Usermapper.java not called Userdao). -        <!--<mapper resource= "Mapper/usermapper.xml"/> -        <!--<mapper class= "Com.swift.dao.UserMapper"/> -        <!--If there are many interfaces and mappings of the same name in this package, then a package name can be loaded to map the -        < Packagename= "Com.swift.dao"/>    </mappers></Configuration>

The above configuration file and mapping file are similar to the previous SSH framework

But also to understand alias aliases

If you do not pay attention to this, often will make you dizzy feeling, do not understand who is who, but once notice, still feel pretty convenient

Aliases can be defined individually
<typealias alias= "user" type= "Com.swift.pojo.User"/>

It's like giving someone a nickname, a nickname is actually him, later called a nickname on the line

You can also use packages to define aliases in bulk

<package name= "Com.swift.dao"/>
<package name= "Com.swift.pojo"/>

Then the interface or class under these packages can use a nickname (that is, the class name can be lowercase, or it doesn't matter if it's case)

But be careful not to move the class name of the dynamic agent, out of builderexception, the agent object is not created

The new configuration file

<?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 -<Configuration>    <PropertiesResource= "Db.properties"></Properties>    <typealiases>        <!--single alias definition -        <!--<typealias alias= "user" type= "com.swift.pojo.User"/> -        <!--bulk alias definition, scan the class under the entire package, alias class name ( case insensitive) -        < Packagename= "Com.swift.dao" />        < Packagename= "Com.swift.pojo" />    </typealiases>    <Environmentsdefault= "Development">        <!--data sources for development -        <EnvironmentID= "Development">            <TransactionManagertype= "JDBC" />            <DataSourcetype= "Pooled">                < Propertyname= "Driver"value= "${jdbc.driver}" />                < Propertyname= "url"value= "${jdbc.url}" />                < Propertyname= "username"value= "${jdbc.username}" />                < Propertyname= "Password"value= "${jdbc.password}" />            </DataSource>        </Environment>    </Environments>    <!--Load SQL mapping file -    <mappers>    <!--When you move the SQL mapping file to the interface file (the way that the JDK proxy does not need to implement the Class), you can replace the resource property with the Class property by using the same file name under a package (the interface is renamed Usermapper.java not called Userdao). -        <!--<mapper resource= "Mapper/usermapper.xml"/> -        <!--<mapper class= "Com.swift.dao.UserMapper"/> -        <!--If there are many interfaces and mappings of the same name in this package, then a package name can be loaded to map the -        < Packagename= "Com.swift.dao"/>    </mappers></Configuration>

The location of the profile property is fixed because the constraint cannot be changed

The mapping file is as follows:

I don't know, I'll be ignorant.

Mybatis Mapper Dynamic Proxy mode

Related Article

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.