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>
  • 相关阅读:
    android的布局管理器
    android控件---自定义带文本的ImageButton
    新建android项目src和layout文件夹中没有内容的问题
    android控件---spinner
    maven+springMVC+mybatis+junit详细搭建过程 ***
    JUnit4的使用
    Jenkins集成Docker镜像实现自动发布
    Docker部署tomcat及应用
    Docker的离线安装
    Java中的static关键字解析
  • 原文地址:https://www.cnblogs.com/davidgu/p/6270675.html
Copyright © 2011-2022 走看看