Mapper文件修改为XML

  1. 创建一个与 Mapper 接口同名的 XML 文件(通常位于 resources/mapper 目录下),并添加正确的命名空间(namespace)来指向 Mapper 接口。

  2. 在 XML 文件中添加一个 <insert> 标签,并将 SQL 语句放在其中。

  3. 使用 #{propertyName} 语法来引用 Mapper 接口方法参数中的属性。

原Mapper文件为:

package com.example.admin.mapper;

import com.example.admin.pojo.Admin;
import org.apache.ibatis.annotations.*;


import java.util.List;

@Mapper
public interface AdminMapper {
    @Select("select * from admin")
    public  List<Admin> list();

    @Delete("Delete from admin where id=#{id}")
    public int deleteAd(Integer id);

    //行内表单查询
    @Select("SELECT * FROM admin WHERE username like concat('%',#{username},'%') AND adminType =#{adminType}")
    public List<Admin> listSelect(String username,String adminType);

     @Insert("insert into admin(username,password,adminType) values(#{username},#{password},#{adminType})")
      public int insertAd(Admin admin);

    @Select("select * from admin where id=#{id}")
    public Admin getById(Integer id);

    @Update("update admin set username=#{username},password=#{password},adminType=#{adminType} WHERE id = #{id}")
    public boolean update(Admin admin);


}

修改后的xml文件为

<?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.example.admin.mapper.AdminMapper">

    <!-- 查询所有管理员 -->
        <select id="list" resultType="com.example.admin.pojo.Admin">

            select * from admin

        </select>

        <!-- 根据ID删除管理员 -->

        <delete id="deleteAd" parameterType="java.lang.Integer">

            Delete from admin where id=#{id}

        </delete>


        <!-- 插入管理员 -->
        <insert id="insertAd" parameterType="com.example.admin.pojo.Admin">

            insert into admin(username,password,adminType) values(#{username},#{password},#{adminType})

        </insert>

        <!-- 根据ID获取管理员 -->
        <select id="getById" resultType="com.example.admin.pojo.Admin" parameterType="java.lang.Integer">

            select * from admin where id=#{id}

        </select>

        <!-- 更新管理员信息 -->
        <update id="update" parameterType="com.example.admin.pojo.Admin">

            update admin set username=#{username},password=#{password},adminType=#{adminType} WHERE id = #{id}

        </update>




</mapper>

mapper文件为

package com.example.admin.mapper;

import com.example.admin.pojo.Admin;
import org.apache.ibatis.annotations.*;


import java.util.List;

@Mapper
public interface AdminMapper {
    public  List<Admin> list();
    public int deleteAd(Integer id);
    public List<Admin> listSelect(String username,String adminType);
    public int insertAd(Admin admin);
    public Admin getById(Integer id);
    public boolean update(Admin admin);


}

模糊查询不能直接复制sql需要修改

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值