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
5、这样就可以了,只需要mapper层里接口继承了通用mapper就行了,不用再写方法和mybatis的xml文件了!!!
最终代码如下:
@RequestMapping("getReceiveAddressByMemberId")
@ResponseBody
public List<UmsMemberReceiveAddress> getReceiveAddressByMemberId(String memberId){
List<UmsMemberReceiveAddress> umsMemberReceiveAddresses = userService.getReceiveAddressByMemberId(memberId);
return umsMemberReceiveAddresses;
}
@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;
public interface UmsMemberReceiveAddressMapper extends Mapper<UmsMemberReceiveAddress> {
}
List<UmsMemberReceiveAddress> getReceiveAddressByMemberId(String memberId);
@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;
}