分层思想中的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>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用MyBatis等持久框架时,通常需要编MapperSQL代码。为了减少手动编SQL的工作量,可以考虑使用代码生成工具来自动生成MapperSQL代码。 一种常见的做法是使用MyBatis Generator(简称MBG)来生成Mapper的代码。MBG是一个功能强大的代码生成工具,可以根据数据库表结构自动生成Mapper接口、XML文件以及实体类。 下面是一个简单的步骤来实现MapperSQL代码的自动生成: 1. 配置MBG:在项目引入MBG的依赖,并配置MBG的配置文件。配置文件需要指定数据库连接信息、生成的目标包结构、生成规则等。 2. 定义表结构:在数据库创建表,并确保表的命名规范符合MBG的要求。MBG会根据表结构生成对应的实体类和Mapper接口。 3. 编MBG配置文件:在MBG配置文件定义要生成的表、生成的目标路径、生成的文件类型等信息。可以使用XML或者Java代码两种方式来编MBG配置文件。 4. 运行MBG:使用命令行或者IDE插件运行MBG,让其读取配置文件并生成代码。MBG会根据配置文件定义的规则,自动生成Mapper接口、XML文件和实体类。 5. 使用生成的代码:将生成的Mapper接口、XML文件和实体类拷贝到项目的对应位置,并在业务代码使用它们。 通过以上步骤,就可以实现MapperSQL代码的自动生成。这样可以大大减少手动编SQL的工作量,提高开发效率。当数据库表结构有变动时,只需要重新运行MBG即可更新生成的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值