Notes on the development of mybatis+jpa

Keywords: Mybatis Oracle xml Database

1. Add, delete, modify and check.

1, For the add operation, if the id is generated automatically, the primary key needs to be returned. Note that if the primary key is of Long type, the int type should also be returned, because mybatis does not support Long.


mybatis ResultMap application

MyBatis returns the configuration of primary key ID when insert ing

In many cases, when inserting data into the database, you need to keep the id of the inserted data for subsequent update operations or storing the id in other tables as a foreign key.

However, by default, the insert operation returns an int value, which is not the primary key id, but the number of rows affected by the current SQL statement...

Next, let's see how MyBatis binds the returned id to the object when using MySQL and Oracle for insert operations.

MySQL usage:

<insert id="insert" parameterType="com.test.User"  keyProperty="userId" useGeneratedKeys="true" >

In the above configuration, "keyProperty" indicates that the returned id is to be saved in the property of the object, "useGeneratedKeys" indicates that the primary key id is self growing mode.

The above configuration in MySQL is OK. It's relatively simple and won't be repeated.

Oracle usage:

<insert id="insert" parameterType="com.test.User">
   <selectKey resultType="INTEGER" order="BEFORE" keyProperty="userId">  
       SELECT SEQ_USER.NEXTVAL as userId from DUAL
    insert into user (user_id, user_name, modified, state)
    values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR},  #{modified,jdbcType=TIMESTAMP}, #{state,jdbcType=INTEGER})

In the usage of Oracle, it should be noted that since Oracle has no self growth term, only sequence, which imitates the self growth form, can no longer use the "useGeneratedKeys" attribute.

Instead, < selectkey > is used to get and assign the ID to the attribute of the object. The ID is inserted normally during the insert operation.


Note: 1. Do not regard the return value of Mapper's insert function as id, in fact, the return value int is only the number of records inserted.


2. Do not get the id of the inserted record in the service layer. Because there are transactions in the service layer, the database does not have this record at this time, so the id cannot be obtained at this time. You should get the id of the inserted record in the Controller layer.



2, Delete operation, return boolean type

3, Many to many relational tables (intermediate tables), how to modify data? A: Delete all and add again

4, Note for < if test > label: it is recommended to use single quotation mark to enclose double quotation mark. If double quotation mark encloses single quotation mark string, string cannot be parsed

<if test='columnA != "exam"   ' > 


mybatis caching mechanism

Generally speaking, if the website traffic is small, the second level cache is enough.

The first level cache of mybatis is in sqlSession, which needs the same sqlSession and takes effect after the transaction is committed. The limitations are great.

If the secondary cache is at the mapper level, the cache is based on the namespace specified in the Mapper.xml configuration file.

<!--mybatis To configure-->
        <!--Enable L2 cache-->
        <setting name="cacheEnabled" value="true"/>
        <typeAlias alias="User" type="com.autohome.model.User" />

        <mapper resource="mapper/UserMapper.xml" />


<!--Mapper.xml File configuration cache-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-// Mapper 3.0//EN"
<!--Namespace and interface are consistent-->
<mapper namespace="com.autohome.dao.UserMapper">

    eviction LRU
    flushInterval Cache time in milliseconds
    size Cache size
    readOnly If so false The cache object must be serializable-->
    <cache eviction="LRU"

    <select id="listAllUser" resultType="User">
        select * from t_userinfo


jpa inserts and updates using database default values



Published 12 original articles, won praise 8, visited 30000+
Private letter follow

Posted by krraleigh on Sun, 01 Mar 2020 19:20:45 -0800