• MyBatis


    1. 简单的select映射

    <mapper namespace="com.mybatis3.mappers.StudentMapper">
        <select id="findStudentById" parameterType="int" resultType="Student">
            select stud_id as studId, name, email, dob 
            from Students 
            where stud_id=#{studId}
         </select>
    </mapper>

    映射接口:

    package com.mybatis3.mappers;
    public interface StudentMapper
    {
     Student findStudentById(Integer id);
    }

    2. 简单的insert映射

    <insert id="insertStudent" parameterType="Student">
         INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL, PHONE)
         VALUES(#{studId},#{name},#{email},#{phone})
    </insert>

     映射接口:

    package com.mybatis3.mappers;
    public interface StudentMapper
    {
     int insertStudent(Student student);
    }

     自增主键:

    useGeneratedKeys="true" keyProperty="studId"

    <insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="studId">
     INSERT INTO STUDENTS(NAME, EMAIL, PHONE)
     VALUES(#{name},#{email},#{phone})
    </insert>

    3. 简单的update映射

    <update id="updateStudent" parameterType="Student">
         UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, PHONE=#{phone}
         WHERE STUD_ID=#{studId}
    </update>

    4. 简单的delete映射

    <delete id="deleteStudent" parameterType="int">
         DELETE FROM STUDENTS WHERE STUD_ID=#{studId}
    </delete>

     映射接口:

    package com.mybatis3.mappers;
    public interface StudentMapper
    {
     int deleteStudent(int studId);
    }

    5. 简单的结果映射

    resultMap

    <resultMap id="StudentResult" type="com.mybatis3.domain.Student">
        <id property="studId" column="stud_id"/>
        <result property="name" column="name"/>
        <result property="email" column="email"/>
        <result property="phone" column="phone"/>
    </resultMap>
    
    <select id="findAllStudents" resultMap="StudentResult" >
        SELECT * FROM STUDENTS
    </select>
    
    <select id="findStudentById" parameterType="int" resultMap="StudentResult">
        SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}
    </select>
  • 相关阅读:
    SQL code for the partitoned example
    去掉重复的列名.txt
    末公开的存储过程.txt
    LauncherClass
    Retina时代的前端视觉优化
    获得数据库中表字段的名字.txt
    StaticConstructor.cs
    UNION ALL实现的分级汇总示例.sql
    SignAndSeal
    在现有数据库上还原时的数据文件处理示例.sql
  • 原文地址:https://www.cnblogs.com/davidgu/p/6270675.html
Copyright © 2020-2023  润新知