mybatis in SQL mapping files

First, some of the top elements of the SQL mapping file:

Root element node mapping file, only one property namespace (namespace): 1.mapper
action:
A used to distinguish Mapper, globally unique;.
B binding DAO interface, i.e. the interface oriented programming, when a namespace binding. after the interface, you can not write the class that implements this interface, mybatis will find the corresponding mapper configured to perform the statement by the fully qualified name of the interface, the interface must therefore namespace naming keep the same name.
2.cache: Configuration of the cache given namespace;
3.cache-REF: Reference to a cache configuration from another namespace;
4.resultMap: databases used to describe the relationship between the object and the result set;
5.sql: SQL can be reused blocks can be referenced by other statements;
6.insert: mapping insert statements;
7.update: map update statement;
8.delete: mapping delete statement;
9.select: mapping query;

Second, the use select to complete a single query:

<!-- 根据用户名查询用户列表(模糊查询) -->
<select id="getUserListByUserName" resultType="User" parameterType="string">
select * from smbms_user where username like CONCAT('%',#{username},'%')
</select>
//Id:命名空间的唯一标识符,可以被用来引用这条语句;
//ParameterType:表示查询语句传入参数类型的完全限定名或者别名,它支持基本参数类型和复杂参数类型
//reultType:查询语句返回结果集类型的完全限定名或者别名,别名的使用方式和parameterType是一样的

Alias ​​java type mappings

Aliases Mapping Types
double Double
long Long
byte Byte
boolean Boolean
short Short
date Date
int Int
map Map
integer Integer
hashmap HashMap
arraylist ArrayList
list List

Third, the use resultMap results

Some attributes used The resultMap:
(. 1) constructor
- for, when the class is instantiated, the result is injected into the constructor
- idArg - ID parameter; as a result of the ID tag can help improve the overall performance
- arg - will be injected into a common result constructor
(2) id - an ID result; mark as a result of ID can help improve the overall performance
(3) result - is injected into a field or JavaBean property general result
(. 4) association
- associated with a complex type ; packaged many results of this type
- nested result mappings - correlation can be designated as a resultMap element, or a reference to a
(5) collection - collection of complex types
- nested result mappings - set may be specified as a resultMap element, or a reference
(. 6) discriminator
- use the resulting value used to determine which resultMap
- case - based on a result of some value map
- nested result mappings - a case is itself a mapping result, it is possible to contain many of the same elements, or it may refer to an external resultMap.

1.id: unique identification, this reference is used to select the value of id attribute element resultMap
2.type: mapping result indicates that type resultMap
3.result: simple attributes used to indicate which column represents the attribute of a query from the database field name, property indicates check out the field corresponding to the value assigned to that property questions of the object.

Four, resultMap and resulttype difference:

1.resultType: Direct indicates the return type, comprising a base data type and complex data type;
2.resultMap: resultMap external reference defined, corresponding to the resultMap external id, returns a result showing a map where the resultMap, its general application scenario They are: database field information and object properties are inconsistent or need to do complex joint inquiry in order to freely control the mapping result.

Five, resultMap and resulttype contact:

In fact, the return value is mybatis Map type key-value pairs exist, resultType for simple query more convenient point, but resultMap for more complex queries using the union will be relatively simple point.

Six, resultMap automatic map level:

resultMap default mapping level PARTIAL, if necessary to meet the requirements, you need to set mybatis automatic map level (autoMappingBehavior) is NONE, which prohibits automatic matching, modify mybatis-config.xml:

<settings>
  <!—设置resultMap的自动映射级别为NONE(禁止自动匹配)-->
  <setting name = “autoMappingBehavior” value=”NONE”/>
 </settings>

Premise automatic mapping field names and attribute names have been required, under the default map level, if not, can not be done automatically mapped.

Seven, to increase the use of insert complete operation

Example:

<insert id="add" parameterType="cn.smbms.pojo.user.User">
  insert into smbms_user (userid,user_name,userpassword,useremail,username,sex,brithday,usercode)
   values (#{userid},#{user_name},#{userpassword},#{useremail},#{username},
    #{sex},#{brithday},#{usercode})
</insert>

Eight, using the modified sentence modification operations

Example:

<update id="modify" parameterType="cn.smbms.user.dao.addUserMapper">
  update smbms_user set user_name=#{user_name},userpassword=#{userpassword},useremail=#{useremail},
   username=#{username},sex=#{sex},brithday=#{brithday},usercode=#{usercode}
    where  userid=#{userid}
</update>

Nine, using @Param notes passed to achieve multi-parameter:

The @Param ( "userpassword") String pwd , the parameter corresponding to rename userPassword, the SQL required # mapped name} {annotation, such as {#} userPassword
benefits: a package carrying object does not require a direct for two parameters to the parameters, clear.

Ten, in mybatis in how to choose the parameters of the way?

In general, if more than four parameters, we will select the objects into the packaging parameters (especially when conventional packaging and modify operations, large field, encapsulated as an object reference into a more convenient);
for a fixed parameter of the service method, the best multi-parameter into the parameter, because this method is more flexible, more readable code, can clearly be seen in the interface methods need to pass parameters.

If the parameter is passed in and out when the data type, whether it is multi-parameter or parameters into a single parameter to the Senate, need to use @Param annotations to pass parameters.

XI, use the delete complete the deletion:

<delete id="deleteUserByUserid" parameterType="Integer">
  delete from smbms_user where userid = #{userid}
</delete>

XII resultMap using advanced mapping results: MybatisTest04

Association: mapping JavaBean of a "complex type" properties, processing only one to one association mapping.
Collection: process mapping many relationship

Thirteen, resultMap the map level:

NONE: Disable automatic mapping;
the PARTIAL: default mapping automatically match mapping attributes, internal embedded Association, except Collection;
FULL: Automatic Matching Mapping

Fourteen, mybatis cache:

1. cache:

Based PerpetualCache (mybatis comes) hashmap the local cache, within the session scope, or refreshed when the session is closed, to be emptied;

2. The secondary cache:

Secondary cache is global caching, beyond the session, can be shared by all sqlsession, it requires mybatis open setting of the configuration file.
The result is a cache of SQL statements, the results of the secondary cache object.
Secondary cache configuration:
set to global configuration, disposed in mybatis:

<settings>
 <setting name=”cacheEnabled” value=true/>
</settings>
<mapper namespace=”………………”>
 <cache
  Eviction = “FIFO”
  flushIntrval =6000”
  size =512”
  readOnly =true/>
 ………………
</mapper>
Published 15 original articles · won praise 10 · views 575

Guess you like

Origin blog.csdn.net/weixin_44439445/article/details/102991510