mybatis 的 xml 增删改查

时间:2022-5-3    作者:老大夫    分类: JAVA


mapper

package com.tjetc.springboot07.mapper;

import com.tjetc.springboot07.domain.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

/**
 * 查询所有用户
 * 根据id查询用户
 * 插入用户
 * 更新用户
 * 删除用户
 * 根据名字模糊查询
 * 查询所有用户的个数
 * 根据用户名和密码查询
 */
@Mapper
public interface UserInfoMapper {

//    @Select("select * from userinfo")
    //查询所有用户
    List<UserInfo> findAll();
    //根据id查询用户
    UserInfo findById(@Param("id") Integer id);
    //插入用户
    int insert(UserInfo userInfo);
    //更新用户
    int update(UserInfo userInfo);
    //删除用户
    int deleteById(Integer userid);
    //根据名字模糊查询
    List<UserInfo> findByUsername(String username);
    //查询所有用户个数
    int count();
    //通过用户名和密码查询
    UserInfo findByUsernameAndPassword(@Param("username") String username,@Param("password") String password);

//    @Select("select * from userinfo where username like #{username} and password like #{password}")
//    UserInfo findByUsernameAndPassword(String username,String password);
//    UserInfo findByUsernameAndPassword(Map<String,Object> param);

}

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.tjetc.springboot07.mapper.UserInfoMapper">

    <select id="findAll" resultType="com.tjetc.springboot07.domain.UserInfo">
        select *
        from userinfo
    </select>
    <select id="findById" resultType="com.tjetc.springboot07.domain.UserInfo">
        select *
        from userinfo
        where userid =#{id}
    </select>
    <insert id="insert" parameterType="com.tjetc.springboot07.domain.UserInfo">
        insert into userinfo  (username,password,borndate,hiredate,salary)
        values (#{username},#{password},#{borndate},#{hiredate},#{salary})
    </insert>
    <update id="update" parameterType="com.tjetc.springboot07.domain.UserInfo">
        update userinfo set
        username = #{username},
        password = #{password},
        borndate = #{borndate},
        hiredate = #{hiredate},
        salary = #{salary}
        where userid =#{userid}
    </update>
    <delete id="deleteById" parameterType="java.lang.Integer">
        delete from userinfo
        where userid = #{userid}
    </delete>
    <select id="findByUsername" resultType="com.tjetc.springboot07.domain.UserInfo">
        select * from userinfo
        <where>
            username like concat('%',#{username},'%')
        </where>
    </select>
    <select id="count" resultType="java.lang.Integer">
        select count(*)
        from userinfo
    </select>
    <select id="findByUsernameAndPassword" resultType="com.tjetc.springboot07.domain.UserInfo">
        select * from userinfo
        <where>
            username = #{username}
            <if test="#{password} != null">
                and password = #{password}
            </if>
        </where>
    </select>
</mapper>


扫描二维码,在手机上阅读

推荐阅读: