tk通用mapper的使用,但扫描不到mapper包的解决方法(含测试代码,根据id查询收货地址)

tk通用mapper的使用

1、先在pom.xml文件添加通用mapper的依赖

<!-- 通用mapper -->
<dependency>
   <groupId>tk.mybatis</groupId>
   <artifactId>mapper-spring-boot-starter</artifactId>
   <version>1.2.3</version>
   <exclusions>
      <exclusion>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
      </exclusion>
   </exclusions>
</dependency>

2、在mapper包里面的接口,继承tk的mapper工具类,并且加上UmsMember的映射类,对这个实体类下封装的对象进行对数据库的单表增删改查
在这里插入图片描述

public interface UserMapper extends Mapper<UmsMember>

3、配置mapper的主键和主键返回策略,在bean表类下加以下@Id注解,标注改实体类对应的数据库表中的主键。并把实体类进行序列化,例如这这个实体类中:public class PmsBaseAttrInfo implements Serializable,类后加implements Serializable

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;

4、注意:要在application里添加tk.mybatis包下的注解@MapperScan扫描tk的mapper包(核心内容)!!!
import tk.mybatis.spring.annotation.MapperScan; //注意这里要要tk…mapper的扫描器,要不然扫描不了mapper
tk通用mapper的使用,但扫描不到mapper包的解决方法(含测试代码,根据id查询收货地址)_第1张图片
5、这样就可以了,只需要mapper层里接口继承了通用mapper就行了,不用再写方法和mybatis的xml文件了!!!

例如:根据member_id查询用户的收货地址。

最终代码如下:

  1. controller控制层
@RequestMapping("getReceiveAddressByMemberId")
@ResponseBody
public List<UmsMemberReceiveAddress> getReceiveAddressByMemberId(String memberId){
    List<UmsMemberReceiveAddress> umsMemberReceiveAddresses = userService.getReceiveAddressByMemberId(memberId);
    return umsMemberReceiveAddresses;
}
  1. bean映射类(实体类)
@Id
private String id;
private String memberId;
private String  name;
private String  phoneNumber;
private int defaultStatus;
private String postCode;
private String province;
private String city;
private String region;
private String detailAddress;
  1. Mapper层,只需要继承通用mapper,并且加上映射实体类就行了。
public interface UmsMemberReceiveAddressMapper extends Mapper<UmsMemberReceiveAddress> {
}
  1. Service接口下的方法
List<UmsMemberReceiveAddress> getReceiveAddressByMemberId(String memberId);
  1. 接口的实现方法
@Autowired
UmsMemberReceiveAddressMapper umsMemberReceiveAddressMapper;
@Override
public List<UmsMemberReceiveAddress> getReceiveAddressByMemberId(String memberId) {
    UmsMemberReceiveAddress umsMemberReceiveAddress = new UmsMemberReceiveAddress();
    umsMemberReceiveAddress.setMemberId(memberId);
    List<UmsMemberReceiveAddress> umsMemberReceiveAddresses = umsMemberReceiveAddressMapper.select(umsMemberReceiveAddress);
    return umsMemberReceiveAddresses;
}
  1. 之后在网页输入http://user.gmall.com/getReceiveAddressByMemberId?memberId=1
    就可以查询并返回memberId为1的所有收获地址了。

你可能感兴趣的:(tk通用mapper的使用,但扫描不到mapper包的解决方法(含测试代码,根据id查询收货地址))