Common configuration files in mybatis

Configuration files in mybatis

1. Global profile

1.1 properties: read external resources

properties file, read in the form of key value, with =

For example:

Create the db.properties file under resources:

  Load in global configuration file:

    <properties  resource="db.properties">
        <property name="userName" value="root"/>
        <property name="password" value="1234"/>
    </properties>

Read configuration location:

<property name="username" value="${userName}"/>
<property name="password" value="${password}"/>

  If a property is configured in more than one place, MyBatis will be loaded in the following order:

  • First read the properties specified in the properties element body.

  • Then read the property file under the classpath according to the resource attribute in the properties element, or read the property file according to the path specified by the url attribute, and overwrite the previously read property with the same name.

  • Finally, read the property passed as a method parameter and overwrite the previously read property with the same name.

Therefore, the attributes passed through the method parameters have the highest priority, followed by the configuration file specified in the resource/url attribute, and the attributes specified in the properties element have the lowest priority.

1.2 setting: global parameter configuration

For example:

    <settings>
        <!--Enable L2 cache-->
        <setting name="cacheEnabled" value="true"/>
    </settings>

1.3 typeAliases: type aliases

Type alias is to set a shorter name. It is related to XML configuration. Its significance is to reduce the redundancy of fully qualified names.

<typeAliases>
        <!--
        Alias a single class
        type Properties: given pojo Full path of class
        alias Property: a given alias can be given arbitrarily. The general recommendation is pojo The first lowercase type of the class-->
         <typeAlias type="com.jing.pojo.Student" alias="student"/>
        <!--Batch alias
        name Property is the package path to be aliased in batch
        The default alias is the lowercase initial of the class name under the package, and others remain the same-->
        <package name="com.jing.pojo"/>
</typeAliases>

mybatis-config.xml:

studentmapper.xml:

1.4 environments: environment variables

 <!--Configure data sources-->
    <environments default="development">
        <!--Environment configuration, multiple environments can be configured-->
        <environment id="development">
            <!--Transaction manager usage JDBC Transaction manager for-->
            <transactionManager type="JDBC"/>
            <!--data source  Pool type data source-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/jing"/>
                <property name="username" value="${userName}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

1.5 mappers: mapper

The configuration file of defined SQL statements needs to tell mybatis how to find these SQL statements

It mainly provides three forms:

  <!--Mapping file import-->
    <mappers>
        <!--resource Method: specify a single mapping file,-->
        <mapper resource="mapper/studentmapper.xml"/>
        
        <!--class Method: by mapper To load a single mapping file mapper Interface documents and mapper.xml In the same directory with the same name-->
        <mapper class="com.jing.mapper.StudentMapper"/>
        
        <!--package Method: batch load mapping files mapper Interface documents and mapper.xml In the same directory with the same name-->
        <package name="com.jing.mapper"/>
    </mappers>

2.mapper.xml configuration file parsing

2.1 query operation:

 <!--
    resultMap:Explicitly specify mapping relationships
    id Attribute: name
    type Attributes: mapped Java of pojo Fully qualified name of class
    id Labels: using primary keys id Tag. Generally, there is only one returned result id label
    result Labels: use of non primary attributes result The mapping relationship is explicitly specified by the label
      property Attributes: mapping java Property name for
      column Properties: field names in the database
      javaType: java Type of property name
      jdbcType: Type of database field name
      typeHandler: Specify type converter
    -->
​
    <resultMap id="studentMap" type="student">
        <id property="id" column="SID"/>
        <result property="Sname" column="Sname"/>
        <result property="Ssex" column="Ssex"/>
        <result property="Sage" column="Sage"/>
    </resultMap>
​
    <!--
    select Labels: query operation labels
    id: statementID,Generally, it is the method name in the corresponding interface, which is unique under the same namespace,
    parameterType: Indicates the parameter type
    parameterMap: Indicates that the parameter type input parameter is Map type
    resultType:Specify return type
    resultMap: Specifies the return result type
​
    #{30} : represents a placeholder  ?
    -->
    <select id="selectStudentById"  parameterType="int" resultMap="studentMap">
        select * from student where SID = #{id}
    </select>

Similarities and differences between resultMap and resultType:

Same point: Specifies the of the returned result

Difference: resultType: given the mapped java object, the automatic mapping is completed. If the database field name is consistent with the java field, it is recommended to choose resultType

resultMap: explicit mapping relationship needs to be specified. If the database field name is inconsistent with the java field, resultMap must be selected to explicitly specify the mapping relationship

2.2 deleting

  <!--delete Labels: deleting data
    id: statementID,Generally, it is the method name in the corresponding interface, which is unique under the same namespace,
    parameterType:Input parameter type-->

    <delete id="deleteStudentById" parameterType="int"  >
        delete from student where SID = #{id}
    </delete>
​

2.3 insertion operation

<!--insert Labels: inserting data
    id: statementID,Generally, it is the method name in the corresponding interface, which is unique under the same namespace,
    parameterType:Input parameter type
    useGeneratedKeys: Indicates whether to use data auto increment primary key true|false
    keyProperty:Corresponding to primary key Java Property name of the class ()
    keyColumn:The field name of the database corresponding to the primary key-->
    <insert id="insertStudent" parameterType="student"  keyProperty="" keyColumn="" useGeneratedKeys="true">
</insert>
   

2.4 change operation

<!--update Labels: change operations
        id: statementID,Generally, it is the method name in the corresponding interface, which is unique under the same namespace,
        parameterType:Input parameter type
       keyProperty:Corresponding to primary key Java Property name of the class-->
    <update id="updateStudent"  parameterType="student">
    </update>

Tags: MySQL JDBC Mybatis mybaties

Posted on Thu, 21 Oct 2021 19:41:56 -0400 by Gighalen