Using Mabtis (1) in the development process of SSM framework learning



1. MaBatis The

purpose of this article is to introduce how to use mabtis in the development process, how to operate the database by yourself and how to establish the relationship between the tables. When writing multiple methods in the Mapper interface file to operate the database, you need to In the corresponding xml file, write multiple select, insert, delete and other statements. At this time, you can use the result mapping method to perform unified matching processing in the xml file. The code is as follows:



<!--namespace: generally write the corresponding Interface path-->

<mapper namespace="com.lanou.mapper.StudentMapper">


    <!--MyBatis result mapping:

        If the attribute name of the entity class is inconsistent with the column name of the database table,

     Need to use result mapping to ensure data consistency

     requirements: all mappings need to be written on top of the sql statement

     -->

    <resultMap id="BaseMap" type="com.lanou.bean.Student">

        <!--

           column: The column name of the database table.property: The attribute name of the entity class

           id: Only this is special and the

           others can use result

        -->

        <id column="id" property="id"/>

        <result column="name" property="name"/>

    </resultMap>

; When writing select and other execution statements, you can use resultMap=”ID” for mapping, for example:



<select id="allListStudent" resultMap="BaseMap">


        SELECT * FROM student


    </select>

III. In the database query statement, Mybats provides a method to collect the query conditions into a label and set its ID, so that when the parameters of the corresponding conditions are used, the operation can be performed according to the ID, which is convenient to use. The examples are as follows:



<!--Save a sql fragment for repeated use-->

    <sql id="WaHaHa">

        id,name

    </sql>


    <select id="allListStudent& quot; resultMap="BaseMap">


        SELECT

         <include refid="WaHaHa"/>

         FROM student


    </select>

Fourth, when adding, deleting, modifying and checking the database and other operations, sometimes it is necessary to consider the operation according to a certain parameter, or directly operate the entity class Object, the code is as follows:



public interface StudentMapper {

    //Query all students

    List

 

    allListStudent();

    //If there are multiple parameters, you need to use @Param annotation to specify the alias of the parameter

    void insertStudent(@Param("sname") String name );

    //Insert student directly

    void insertStu(Student student);

    //Delete

    void deleteStudent(@Param("sid") Integer id);

    //Update data

    void updateStudent(Student student);

}


  

5. According to the above code, it can be known that the parameter alias is specified in the way of annotation @Param. In the xml file, it should correspond to the set alias. The Type in it needs to use the parameterType, which is mapped to the specific entity class but The parameterType can be writable or not, the xml code is as follows:




 

  

        INSERT INTO student VALUES (NULL,#{sname});

   

 

  

       INSERT INTO student VALUES (NULL ,#{name})


 

  

       DELETE FROM student WHERE id=#{sid};


 

  

      UPDATE student SET name=#{name} WHERE id = #{id};


  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486900&siteId=291194637