分层思想中的mapper层:

mapper下可以写接口和.xml:

两种方法如下:

1.只写接口。在接口中写方法和sql语句: 适合简短的查询

 

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.在接口中实现方法,在.xml中写sql语句:适合sql语句较复杂的情况,如:进行联合查询

 

 

<?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>

猜你喜欢

转载自blog.csdn.net/weixin_53748496/article/details/127705614
今日推荐