zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Mysql添加远程访问权限
    Android下安装应用不成功解决
    Unity 编译apk启动出异常
    Java 实现函数回调
    C# 实现函数回调
    北京数字认证无领导小组讨论总结
    深圳市共进电子 嵌入式软件工程师笔试题
    北京君正和博彦科技笔试体会及总结
    9月5日 华为2014校园招聘的机试题目_C语言版答案
    ios客户端base64上传图片到java服务器遇到的问题
  • 原文地址:https://www.cnblogs.com/davidgu/p/6270675.html
Copyright © 2011-2022 走看看