The mapper layer in layered thinking:

Interface and .xml can be written under mapper:

The two methods are as follows:

1. Write-only interface. Write methods and sql statements in the interface: suitable for short queries

 

 

package com.hsd.mapper;

import com.hsd.entity.Employee;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface EmployeeMapper {
    @Select("select * from employee")
    List<Employee> selectAll();

    @Insert("insert into employee value (null,#{name},#{age},#{sex},#{phone},#{hobby},#{state})")
    void addServlet(Employee employee);
@Select("select * from employee where id=#{id}")
    Employee selectById(int id);
@Update("update employee set name=#{name},age=#{age},sex=#{sex},phone=#{phone},hobby=#{hobby},state=#{state} where id=#{id}")
    void update(Employee employee);
@Delete("delete from employee where id=#{id}")
    void deleteById(int id);
@Delete("delete from employee where id = #{id}")
    void xDelete(int parseInt);
@Select("select * from employee where name like concat('%',#{name},'%') or phone=#{phone}")
    List<Employee> selectNameAndPhone(Employee employee);
@Update("update employee set state=\"离职\" where id=#{id};")
    void leave(Employee employee);
    @Select("select * from employee where id=#{id}")
    List<Employee> information(Employee employee);
}

 2. Implement the method in the interface, and write the sql statement in .xml: it is suitable for the situation where the sql statement is more complicated, such as: performing joint query

 

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hsd.mapper.StudentMapper">
    <select id="selectAll" resultType="com.hsd.entity.Student">
select * from student;

    </select>
<insert id="addServlet">
    insert into student value (null,#{name},#{age},#{sex},#{hobby},#{time});
</insert>
    <delete id="deleteById">
        delete from student where id=#{id};
    </delete>
    <select id="selectById" resultType="com.hsd.entity.Student">
select * from student where id=#{id};
    </select>
    <update id="update">
        update student set name=#{name},age=#{age},sex=#{sex},hobby=#{hobby},time=#{time} where id=#{id};
    </update>
    <delete id="xDelete">
        delete from student where id = #{id}
    </delete>
    <select id="selectByName" resultType="com.hsd.entity.Student" parameterType="com.hsd.entity.vo.StudentVo">
select * from student where name like concat('%',#{name},'%')
    </select>
    <select id="selectByAge" resultType="com.hsd.entity.Student" parameterType="com.hsd.entity.vo.StudentVo">
select * from student
/*循环判断,where标签在MyBatis中是循环*/
/*if标签在MyBatis中是判断*/
        <where>
        /*在MyBatis中,在where循环中使用and的前提,必须是还有一次条件,如果只有一次条件就不允许使用and,可以写一个模拟条件*/
        sex in('0','1')/*模拟条件*/
        /*判断输入的不是空值*/
            <if test="age1 != null">
            /*在MyBatis中,大于号和小于号是由(&gt; &lt;)代替的*/
               and age &gt;#{age1}
            </if>
            <if test="age2!=null">
                /*在MyBatis中,大于号和小于号是由(gt; lt;)代替的*/
                and age &lt;#{age2}
            </if>
        </where>
    </select>
    <select id="selectByNameAndAge" parameterType="com.hsd.entity.vo.StudentVo" resultType="com.hsd.entity.Student">
        select * from student
        <where>
            <if test="name!=null and name!=''">
                name like concat('%',#{name},'%')
            </if>
            <if test="age1!=null and age1!=0">
                and age &gt;#{age1}
            </if>
            <if test="age2!=null and age2!=0">
                /*在MyBatis中,大于号和小于号是由(gt; lt;)代替的*/
                and age &lt;#{age2}
            </if>
        </where>
    </select>
</mapper>

 

Guess you like

Origin blog.csdn.net/weixin_53748496/article/details/127705614