mybatis 一个基本增删改查的mapper示例

本示例基于SpringBoot:2.1.6.RELEASE

基本增删改查mapper示例

<?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="cn.lenchu.learn.springboot.learnspringbootmybatis.mapper.UserMapper">
    <insert id="saveUser" >
        insert into m_user (id, username, password, createTime) values (
        #{id}, #{username}, #{password}, #{createTime}
        )
    </insert>

    <select id="selectUserById" resultType="UserEntity">
        select * from m_user where id = #{id}
    </select>

    <select id="selectUserByUsername" resultType="UserEntity">
        select * from m_user where username = #{username}
    </select>

    <update id="updateUser">
        update m_user
        <set>
            <if test="username != null">username = #{username},</if>
            <if test="password != null">password = #{password}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteUserById">
        delete from m_user
        where id = #{id}
    </delete>

    <select id="selectUserWithRole" resultMap="UserWithRoles">
        select u.*,
               r.id r_id, r.name r_name
        from m_user u, m_role r, m_user_role ur
        where u.id = ur.uid and ur.rid = r.id and u.id = #{uid}
    </select>

    <resultMap id="UserWithRoles" type="UserWithRolesEntity" autoMapping="true">
            <id column="id" property="id"/>
<!--            <result column="u_username" property="username" />-->
<!--            <result column="u_password" property="password" />-->
<!--            <result column="u_createTime" property="createTime" />-->
        <collection property="roles" ofType="RoleEntity">
            <id column="r_id" property="id" />
            <result column="r_name" property="name" />
        </collection>
    </resultMap>
</mapper>

mapper接口示例

package cn.lenchu.learn.springboot.learnspringbootmybatis.mapper;

import cn.lenchu.learn.springboot.learnspringbootmybatis.entity.UserEntity;
import cn.lenchu.learn.springboot.learnspringbootmybatis.entity.UserWithRolesEntity;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserMapper {

    void saveUser(UserEntity user);

    Optional<UserEntity> selectUserById(String id);

    Optional<UserEntity> selectUserByUsername(String username);

    void updateUser(UserEntity user);

    void deleteUserById(String id);

    Optional<UserWithRolesEntity> selectUserWithRole(String uid);
}

application.yml中简单配置

mybatis:
  type-aliases-package: cn.lenchu.learn.springboot.learnspringbootmybatis.entity
  mapper-locations: classpath:mapper/*.xml

启动类上添加mapperScan注解

@MapperScan(basePackages = {"cn.lenchu.learn.springboot.learnspringbootmybatis.mapper"})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值