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用法,请参考: