MyBatis (i) Mapper dynamic agent

Source: Internet
Author: User

Because DAO development, will create entity class object every time, will pass in fixed query value such as ID, there is hard coding problem, so adopt mapper dynamic proxy (do not create entity class object, need interface only, automatically generate by mapper) as before MyBatis (a) step, However, you need to make changes to the Mapper.xml file:

Namespace: must be the full path of the interface class (<mapper namespace= "" >)

ID: Must be the method name of the interface (<select id= ""/>)

ParameterType: Must be a parameter type inside the interface method

Resulttype: Must be the return value of the interface method (if the List<t> collection type is returned, it must be the entity class represented by the T generic)

Mapper.xml:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Mapper
Public "-//mybatis.org//dtd Mapper 3.0//en"
"Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--SQL statement mapping, namespace is to isolate SQL statements--
<!--mapper Dynamic proxy mode--
<!--namespace the same classpath as the Mapper interface--
<!--Mapper interface method name is the same as the ID of mapper
< The parameters of the Mapper interface method are the same as the ParameterType!--
<!--The return type of the Mapper interface method is the same as Resulttype
<mapper namespace= "Com.xr.mybatis1.User" >


<!--SQL query operation, Resulttype: Result return type #{} represents a placeholder, which prevents SQL injection-
<select id= "Selectbyid" resulttype= "Com.xr.mybatis.User" parametertype= "int" >
SELECT * from UU where Id=#{id}
</select>
<select id= "Selectbyname" resulttype= "Com.xr.mybatis.User" parametertype= "java.lang.String" >
SELECT * from UU where name like '%${value}% '
</select>


<!--inserting data--
<insert id= "Insert" parametertype= "Com.xr.mybatis.User" >
<!--<selectkey keyproperty= "id" order= "after" >
Select LAST_INSERT_ID ()
</selectKey>
Insert into UU (Name,money) VALUES (#{name},#{money})
</insert>

<!--Delete data--
<delete id= "Delete" >
Delete from UU where Id=#{id}
</delete>

<!--modifying Data--
<update id= "Update" parametertype= "Com.xr.mybatis.User" >
Update UU set Name=#{name} where Id=#{id}
</update>
</mapper>

Summarize:

MyBatis can solve the JDBC problem:

1:jdbc each time there are loading drivers, creating connections, and so on------sqlmapconfig.xml, using JDBC things, connection pooling processing

2: Modify SQL statement, modify Java code-----Use mapper,xml file, map SQL statement

The 3:JDBC parameter is too cumbersome because the where condition of the SQL statement is not necessarily, the parameter needs to correspond to the placeholder one by one-----MyBatis automatically maps the Java object to the SQL statement, specified by ParameterType in statement

4: The result set parsing is too cumbersome, SQL changes will lead to parsing code changes, and need to traverse-----before parsing mybatis automatically maps SQL execution results to Java objects, specified by statement Resulttype

MyBatis Advantages: Lightweight framework, simple, convenient, can write raw SQL, control SQL execution performance, flexibility is very high. (You must write SQL statements)

MyBatis (i) Mapper dynamic agent

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.