设为首页 加入收藏

TOP

Mybatis 入门实战(2)--简单使用(三)
2023-07-25 21:33:34 】 浏览:76
Tags:Mybatis 简单使
="com.abc.entity.Student"> select * from a_student where 1=1 <if test="name != null and name != ''"> and name like #{name} </if> <if test="homeAddress != null and homeAddress != ''"> and home_address like #{homeAddress} </if> </select> <select id="select2" resultType="map"> select * from a_student where 1=1 <if test="param1 != null and param1 != ''"> and name like #{param1} </if> <if test="param2 != null and param2 != ''"> and home_address like #{param2} </if> </select> <delete id="delete"> delete from a_student where id in <foreach collection="array" item="id" index="index" open="(" close=")" separator=","> #{id} </foreach> </delete> </mapper>

表 a_student 的字段与实体类的属性一一对应(表中字段使用下划线写法,实体类属性使用驼峰写法),字段 id 为自增字段。

6.2、TeacherMapper.java

package com.abc.mapper;

import com.abc.entity.Teacher;
import com.abc.provider.TeacherProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface TeacherMapper {
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert({"insert into a_teacher(create_time,modify_time,name,age,home_address)",
            " values(#{createTime},#{modifyTime},#{name},#{age},#{homeAddress})"})
    void insert(Teacher teacher);

    @Update({"<script>",
            "update a_teacher set id=id",
            "<if test='name != null and name != \"\"'>",
            "   ,name=#{name}",
            "</if>",
            "<if test='age != null'>",
            "   ,age=#{age}",
            "</if>",
            "<if test='homeAddress != null and homeAddress != \"\"'>",
            "   ,home_address=#{homeAddress}",
            "</if>",
            "where id=#{id}",
            "</script>"
    })
    void update(Teacher teacher);

    @Select("select * from a_teacher where id=#{id}")
    Teacher selectById(Long id);

    @Select({"<script>",
            "select * from a_teacher where 1=1",
            "<if test='name != null and name != \"\"'>",
            "   and name like #{name}",
            "</if>",
            "<if test='homeAddress != null and homeAddress != \"\"'>",
            "   and home_address like #{homeAddress}",
            "</if>",
            "</script>"
    })
    List<Teacher> select(@Param("name") String name, @Param("homeAddress") String homeAddress);

    @SelectProvider(type = TeacherProvider.class)
    List<Map<String, Object>> select2(String name, String homeAddress);

    @Delete({"<script>",
            "delete from a_teacher where id in",
            "<foreach collection='array' item='id' index='index' open='(' close=')' separator=','>",
            "   #{id}",
            "</foreach>",
            "</script>"
    })
    void delete(Long[] ids);
}

TeacherMapper 使用注解来编写 SQL,如果使用了动态 SQL,需添加  script 元素;如果 SQL 比较复杂,不太方便使用注解,可以通过 Provider 使用 Java 代码来构建 SQL。

表 a_teacher 的字段与

首页 上一页 1 2 3 4 5 6 下一页 尾页 3/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java中的运算符 下一篇第2-3-7章 个人网盘服务接口开发-..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目