Springboot中Mybatis-Plus的使用

Springboot引入Mybatis-Plus

  1. maven引入配置
	<dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.1</version>
    </dependency>```
  1. 数据库配置
datasource:
    dynamic:
      primary: lark_platform
      datasource:
        lark_platform:
          url: jdbc:mysql://localhost:3306/test?useSSL=true&requireSSL=false&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
          username: root
          password: root
          druid:
            initial-size: 1
            min-idle: 1
            max-active: 2
  1. 编写实体类和Mapper类
@TableName("user")
@Getter
@Setter
@Accessors(chain = true)
public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * id
	 */
	@TableId(type = IdType.ASSIGN_ID)
	private Long id;

	/**
	 * 所属公司ID
	 */
	private Long companyId;
	/**
	 * 账号
	 */
	private String account;
	/**
	 * 密码
	 */
	private String password;
	/**
	 * 用户姓名
	 */
	private String name;
	/**
	 * 联系电话
	 */
	private String phone;
	/**
	 * 邮箱
	 */
	private String email;
	/**
	 * 头像
	 */
	private String avatar;
	/**
	 * 用户类型。0-免费用户
	 */
	private Integer userType;
	/**
	 * 最后登录时间
	 */
	private Date lastLoginTime;
	/**
	 * 删除状态。0-正常,1-删除
	 */
	private Integer isDeleted;
	/**
	 * 创建时间
	 */
	private Date createTime;
	/**
	 * 更新时间
	 */
	private Date updateTime;
}
  1. 编写mapper类
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
  1. 编写service类
@Service
@Slf4j
public class UserService {
`
	/**
	 * 通过用户id获取用户信息(有缓存)
	 *
	 * @param id 用户id
	 * @return {@code User}
	 */
	public User getById(Long id) {
		return userMapper.selectById(id);
	}
}
  1. 编写Controller
@RestController
public class UserController{
	String API_PREFIX = "/user";
	@GetMapping(API_PREFIX + "/info")
	public Response<UserVo> getUser(long userId) {
		return Response.success(BeanUtil.copyProperties(userService.getById(userId), UserVo.class));
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值