通用Mapper使用介绍
通用Mapper是简化mybatis操作的一个框架,使用它就不需要再建立xml映射文件了,也不用在dao接口(注解开发)写sql语句,只需要将pojp用注解跟数据库的表和字段建立映射关系,然后在dao接口继承Mapp类并指定泛型(你要对哪个pojo操作就指定哪个pojo),在service实现类中,直接调用相关方法,就可以执行简单的CRUD,这样dao接口中也不需要再写方法了。但是这个也只能在简单的CRUD中可以使用。
通用Mapper其实也可以当作一种编程思维,不仅仅是dao接口可以用它抽取,service、controller也一样可以用这种思想抽取出来,直接调用抽取出来的代码就可以了,而不需要重复的写一些代码,这篇随笔里面只说使用方式,这种思想会另起随笔解释。
1、添加起步依赖:
tk.mybatis
mapper-spring-boot-starter
2.0.4
mysql
mysql-connector-java
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
2、将pojo类跟数据库用注解建立映射关系
/****
* @Author:admin
* @Description:Brand构建
*
*****/@Table(name="tb_brand")//指定要映射的数据库中的哪个表
public class Brand implementsSerializable{@Id//表示该成员变量是主键id
@GeneratedValue(strategy = GenerationType.IDENTITY)//标识该主键的策略,自增
@Column(name = "id")private Integer id;//品牌id
@Column(name= "name")//标识该成员变量跟数据库中的name字段映射
private String name;//品牌名称
@Column(name= "image")private String image;//品牌图片地址
@Column(name= "letter")private String letter;//品牌的首字母
@Column(name= "seq")private Integer seq;//排序//get方法
publicInteger getId() {returnid;
}//set方法
public voidsetId(Integer id) {this.id =id;
}//get方法
publicString getName() {returnname;
}//set方法
public voidsetName(String name) {this.name =name;
}//get方法
publicString getImage() {returnimage;
}//set方法
public voidsetImage(String image) {this.image =image;
}//get方法
publicString getLetter() {returnletter;
}//set方法
public voidsetLetter(String letter) {this.letter =letter;
}//get方法
publicInteger getSeq() {returnseq;
}//set方法
public voidsetSeq(Integer seq) {this.seq =seq;
}
}
3、但是想要使用上面这个pojo中的注解,还需要加该系列注解的依赖才能使用:
javax.persistence
persistence-api
1.0
compile
4、创建controller、service、serviceImpl、dao,我按顺序挂一遍:
controller:
@RestController
@RequestMapping("/brand")public classBrandController {
@AutowiredprivateBrandService brandService;
@GetMapping("findAll")publicResult findBrandAll(){
List brandAll =brandService.findBrandAll();return new Result(true, StatusCode.OK,"操作成功!",brandAll);
}
}
service:
public interfaceBrandService {
ListfindBrandAll();
}
serviceImpl:
@Servicepublic class BrandServiceImpl implementsBrandService {
@AutowiredprivateBrandDao brandDao;
@Overridepublic ListfindBrandAll() {returnbrandDao.selectAll();
}
}
dao:关键的地方到了,这个dao需要继承Mapper类,并指定泛型(泛型指定你要操作的pojo):继承它就行了,不需要写方法,当它继承了Mapper之后,调用这个dao接口的serviceImpl类中就可以调用mapper中定义好的方法,这些方法内部会拼接sql语句
public interface BrandDao extends Mapper{
}
还有最关键的一点,就是跟mybatis使用一样,它需要扫描dao接口:这里要要注意,使用扫描的组件必须是tk,而不能用mybatis原本的:
importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;//tk!!tk!!tk!! 重要的事情说三遍,这里必须使用这个包里面的MapperScan!!
importtk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication//spring boot自动配置注解
@EnableEurekaClient//这是spring cloud中的eureka客户端注解
@MapperScan(basePackages = "com.changgou.goods.dao")//指定要扫描的dao接口的包
public classGoodsServiceApplication {public static voidmain(String[] args) {
SpringApplication.run(GoodsServiceApplication.class,args);
}
}
再附加一些Mapper中的常用api介绍:
查询所有:里面代码就有体现,用继承了Mapper的dao接口(以下都是这样,不重复写了).select()
根据主键id查询:selectByPrimaryKey(id);
添加数据:有两个api
.insertSelective(brand); 添加数据,如果添加的数据中有为null的字段,则不进行更改 一般都用这种方式比较好,如果有特