springboot(七):整合mybatis-plus

本文介绍了如何在SpringBoot项目中整合Mybatis-Plus,包括引入依赖、配置文件修改、实体类、Mapper、Service及Controller的调整,利用Mybatis-Plus的便捷功能简化开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 引入依赖

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus</artifactId>
			<version>2.1.1</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatisplus-spring-boot-starter</artifactId>
			<version>1.0.5</version>
		</dependency>

2. 修改配置文件

将mapper的扫描路径配置由mybatis改为mybatis-plus:

mybatis:
  type-aliases-package: com.szl.entity
  mapper-locations: classpath:mapper/*.xml

改为:

mybatis-plus:
  type-aliases-package: com.szl.entity
  mapper-locations: classpath:mapper/*.xml

3. 修改entity

@TableName("customer")
public class Customer implements Serializable{

	/**
	 * 
	 */
	@TableField(exist = false)
	private static final long serialVersionUID = 6983500689524183915L;
	@TableId
	private String uuid;//主键
	private String delFlag;//逻辑删除标识
	private String opeTime;//操作时间
	private String oper;//操作人
	private String createTime;//创建时间
	private String customerName;//用户名
	private String email;//邮箱
	private String frozenState;//冻结状态
	private String frozenType;//冻结类型
	private String lastWrongLoginTime;//最后登录错误时间
	private Integer loginErrorTimes;//登录错误次数
	private String mobile;//手机号
	private String password;//登录密码
	private String salt;//密码哈希

    //getter and setter

在entity上增加注解@TableName("customer"),实体类对应的表名

主键上加注解:@TableId

非表字段,需要加上注解:@TableField(exist = false)

4. 修改Mapper,Service,ServiceImpl,Controller

Mapper:继承BaseMapper<T>

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.szl.entity.Customer;


public interface CustomerMapper extends BaseMapper<Customer>{

}

Service:继承IService<T>

package com.szl.service;

import com.baomidou.mybatisplus.service.IService;
import com.szl.entity.Customer;

public interface CustomerService extends IService<Customer>{


}

ServiceImpl:继承ServiceImpl<BaseMapper<T>, T>

package com.szl.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.szl.entity.Customer;
import com.szl.mapper.CustomerMapper;
import com.szl.service.CustomerService;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>implements CustomerService{

}

修改Controller中的查询方法,不需要在mapper.xml中写sql,mapper,service都不用写方法,调用mybatis-plus封装好的方法即可。

	/**
	 * 根据手机号查询用户信息
	 * @param mobile
	 * @return
	 */
	@RequestMapping(value = "/getCustomer", method = RequestMethod.GET)
	public ResultVo getCustomer(@RequestParam(value = "mobile") String mobile){
		
//		Customer customer = customerService.getCustomerByMobile(mobile);
		EntityWrapper<Customer> entity = new EntityWrapper<Customer>();
		entity.where("mobile", mobile);
		List<Customer> list = customerService.selectList(entity);
		return ResultVo.getData(ResultEnum.SUCCESS, list);
	}

查看结果,和调用customerService.getCustomerByMobile(mobile)一致。

更多mybatis-plus用法,请参考:

http://mp.baomidou.com/guide/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值