Two main configuration files in MyBatis

There are two main configuration files in mybatis:

    1. mybatis-config.xml, which is the core configuration file , mainly configures the information of connecting to the database and the location information of the SQL mapping file .

The details of the configuration file are as follows:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<!-- The main configuration file provides the database link information and the location information of the sql mapping file -->

<configuration>

    <!--

        The mybatis-config.xml file is added to the log configuration, and the executed sql statement and parameter information can be output on the console

        settings: control the global behavior of mybatis

    -->

    <settings>

        <!-- Set mybatis output log -->

        <setting name="logImpl" value="STDOUT_LOGGING" />

    </settings>

<!--Alias ​​-->

<typeAliases>

        <!--

                type: is used to specify the class that needs to be aliased

                alias: is the name used to specify the alias

                When using an alias, it is not case-sensitive. For example, if the alias of the downlink code is student, it will be written as sTudent when using it, and no error will be reported.

                The location of the alias is used in the resultType of the query statement in the xxxMapper.xml sql mapping file

Aliases cannot be used in the name space in the mapping file

        -->

 

        <!--<typeAliastype="com.yjg.domain.Student"alias="student"></typeAlias>-->

 

        <!--

                The following writing method is more concise. It will automatically alias all the classes under the specified package. The alias is the simplified class name. If there are many classes under the specified package, it is very convenient to write in this way.

        -->

        <packagename="com.yjg.domain"/>

</typeAliases>

    <!--

        There may be several database information in the file, the value of default here should be the id value of one of the database environments ,

        In this file, it can be "development" or "online" -->

    <environments default="development">

        <!--

            A configuration environment for database information

            id: is a unique value, custom, indicating the name of the environment

        -->

        <environment id="development">

            <!--

                transactionManager: Indicates the transaction type of mybatis ( JDBC or MANAGED )

                type="JDBC" (indicates that the commit and rollback of the Connection object in jdbc are used for transaction processing)

                type="MANAGED "   indicates that it is handed over to other containers to manage affairs, such as WebLogic, JBOSS, etc. If there is no administrative

container, there are no transactions. There is no transaction meaning: as long as a DML statement is executed, it is submitted once.

 

            -->

            <transactionManager type="JDBC"/>

            <!--

                dataSource: Indicates the data source, connected to the database

                The attribute value of type: UNPOOLED|POOLED|JNDI

                         type="UNPOOLED"  indicates that no connection pool is used

                         type= " POOLED "    means use connection pool

                         type="JNDI"  indicates that the JNDI technology provided by the server is used to obtain the DataSource object. Different servers can obtain DataSource objects differently. If it is not a web or maven war project, JNDI cannot be used.

            -->

            <dataSource type="POOLED">

                <!--

                    driver, url, username, password are fixed and cannot be customized

                -->

                <!-- The driver class name of the database -->

                <property name="driver" value="com.mysql.jdbc.Driver"/>

                <!-- URL string to connect to the database -->

                <property name="url" value="jdbc:mysql://localhost:3306/bjpowernode"/>

                <!-- Username to access the database -->

                <property name="username" value="root"/>

                <!-- Password to access the database -->

                <property name="password" value="******"/>

            </dataSource>

        </environment>

        <!--The following is the configuration environment information of another database-->

        <environment id="online">

            <transactionManager type="JDBC"/>

            <dataSource type="POOLED">

                <property name="driver" value="com.mysql.jdbc.Driver"/>

                <property name="url" value="jdbc:mysql://localhost:3306/bjpowernode"/>

                <property name="username" value="root"/>

                <property name="password" value="******"/>

            </dataSource>

        </environment>

    </environments>

    <!-- mappers is the location of the specified sql mapping file -->

    <mappers>

        <!--

                  There can be multiple <mapper> tags, each <mapper> specifies the location of a sql mapping file

                  resource: is the path information starting from the class path, starting from target/classes

                  The specific operation is: compile the code in maven (compile command), and then go to the target/classes directory to find the StudentDao.xml file,

                  Right-click on the mapper file and select the copy path command to get the fully qualified name of the file

         -->

         <!--

                  There are four configuration methods here, namely:

                  <mapper resource="studentDao.xml"/> This way the studentDao.xml file must be placed under the resource directory

                  <mapper url="d:///studentDao.xml"/> This method must have a studentDao.xml file under the d disk, which is not conducive to the transplantation of the project

                  <mapper class="com.yjg.dao.StudentDao"/> In this way, the studentDao interface and the studentDao.xml file must be put together under the same directory

                  <package name="com.yjg.dao"/> This method is used the most. At this time, both the studentDao interface and the studentDao.xml file need to be placed under the dao package and the interface name should be consistent with the main name of the .xml file. In this way, the dao All xxx.xml files under the package are configured, easy to use

                  Note: Both xxxMapper.xml and xxxDao.xml here refer to the mapping file of sql

        -->

        <package name="com.yjg.dao"/>

    </mappers>

</configuration>

    2. XxxxMapper.xml, this file is a configuration file for SQL mapping specially used to write SQL statements. (One database table corresponds to one)

        The t_user table generally corresponds to a UserMapper.xml

        The t_student table generally corresponds to a StudentMapper.xml

   The details of the file are as follows:

<?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">

<!-- namespace: called a namespace, its value is unique and can be a custom string, here you are required to use the fully qualified name of the dao interface. -->

<mapper namespace="com.yjg.dao.StudentDao">

    <!--

        id: Indicates the unique identifier of the query statement. Mybatis will find the sql statement to be executed according to the id value, and requires the use of the method name in the interface.

        resultType: Indicates the result type, which is the type of the Java object obtained after the ResultSet obtained after the execution of the SQL statement is traversed, and its value is the fully qualified name of the type

    -->

    <select id="selectStudents" resultType="com.yjg.domain.Student">

              select id,name,email,age from student order by id

    </select>

  <-- Note: placeholder #{}, write inside curly braces: property name of pojo (or domain, bean, entity) class -->

<!--

        Get the auto-generated primary key when inserting data

        useGeneratedKeys="true" means to use the automatically generated primary key value, if "false" means not to use the automatically generated primary key value

        keyProperty="id" indicates that the primary key value is assigned to the id attribute of the Student object

-->

    <insert id="insertStudent">

        insert into student values (null,#{name},#{email},#{age})

    </insert>

    <!-- Query student information based on id -->

    <!-- 

           Please pay attention to observe that the value of resultType is the fully qualified name of the Student class. Such a name is usually very long. You can write an alias here. The alias is defined in the mybatis-config.xml file

     -->

    <select id="selectById" resultType="com.yjg.domain.Student">

        select

            name,email,age

        from

            student

        where

            id = #{id} //The characters inside {} here are arbitrary, but it is not recommended to write casually, it is best to write the attribute name of the corresponding entity class by seeing the name and knowing it

    </select>

</mapper>

The following is a description of each section in the mapping file:

<!-- This file is a sql mapping file (English name: sql mapper). Writing sql statements here mybatis will execute the sql statements in the file

    1. Specify the constraints file section:

        <!DOCTYPE mapper

            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

        Among them: mybatis-3-mapper.dtd is the name of the constraint file, and its extension is .dtd

        Its function is: to limit and check the tag attributes that appear in the current file must meet the requirements of mybatis

    2. <mapper></mapper> is the root tag of the current file just like the root tag in the HTML file is <html></html>

        Among them, the attribute namespace: is called a namespace, and its value is unique, which can be a custom string. Here you are required to use the fully qualified name of the dao interface.

    3. Specific tags can be used in this file to indicate specific operations on the database

        <select></select>: Indicates the query, write the query statement here

            id: Indicates the unique identifier of the query statement. Mybatis will find the SQL statement to be executed according to the id value, and requires the use of the method name in the interface.

            resultType: Indicates the result type, which is the type of the Java object obtained after the ResultSet obtained after the execution of the SQL statement is traversed, and its value is the fully qualified name of the type

        <update></update>: indicates update, write update statement in this tag

        <insert></insert>: means insert, write insert statement in this tag

        <delete></delete>: means delete, write delete statement in this tag

-->

Guess you like

Origin blog.csdn.net/heliuerya/article/details/131239189