- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starterartifactId>
- <version>2.1.3version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <scope>runtimescope>
- dependency>
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/db_brand
- username: root
- password: rootaaa
我们去测试一下DataSource有没有成功导入

- package com.brrbaii.pojo;
- import lombok.*;
- @Data
- @NoArgsConstructor
- @ToString
- public class Brand {
- private int id;
- private String brandName;
- private String companyName;
- private int ordered;
- private String description;
- private int status;
-
- }
-
- package com.brrbaii.dao;
-
- import com.brrbaii.pojo.Brand;
- import org.apache.ibatis.annotations.Delete;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.ResultMap;
- import org.apache.ibatis.annotations.Select;
- import java.util.List;
-
- //@Mapper
- public interface BrandDao {
- @Select("select * from tb_brand")
- @ResultMap("BrandResult")
- List
selectAll(); -
- @Select("select * from tb_brand where id = #{Id}")
- Brand selectById(Integer Id);
-
- @Delete("delete from tb_brand where id = #{Id}")
- boolean deleteById(Integer Id);
-
- boolean save(Brand brand);
-
- boolean updateById(Brand brand);
- }
我们去resource目录下建一个Mapper包,专门放mapper.xml映射文件

BrandDao.xml代码如下:
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
- <mapper namespace="com.brrbaii.dao.BrandDao">
- <resultMap id="BrandResult" type="brand">
- <result column="brand_name" property="brandName" />
- <result column="company_name" property="companyName" />
- resultMap>
-
- <insert id="save">
- insert into
- tb_brand
- values
- (null, #{brandName}, #{companyName}, #{ordered}, #{description}, #{status})
- insert>
-
- <update id="updateById">
- update tb_brand
- <set>
- <if test="brandName!=null and brandName!= ''">
- brand_name = #{brandName},
- if>
- <if test="companyName!=null and companyName!= ''">
- company_name = #{companyName},
- if>
- <if test="description!=null and description!= ''">
- description = #{description},
- if>
- <if test="ordered!=null and ordered!= ''">
- ordered = #{ordered},
- if>
- <if test="status!=null and status!= ''">
- status = #{status},
- if>
- set>
- where id = #{id}
-
- update>
-
- mapper>

- mybatis-plus:
- mapper-locations: classpath:mapper/*.xml
- type-aliases-package: com.brrbaii.pojo
这里的mapper-locations是我配置文件的路径

接口 :
- package com.brrbaii.service;
-
- import com.brrbaii.pojo.Brand;
-
- import java.util.List;
-
- public interface BrandService {
-
- List
selectAll(); -
- Brand selectById(Integer Id);
-
- boolean save(Brand brand);
-
- boolean delete(Integer Id);
-
- boolean updateById(Brand brand);
-
-
- }
实现类:
- package com.brrbaii.service.Imp;
-
- import com.brrbaii.dao.BrandDao;
- import com.brrbaii.dao.MqDao;
- import com.brrbaii.pojo.Brand;
- import com.brrbaii.service.BrandService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- @Service
- public class BrandServiceImp implements BrandService {
-
- @Autowired
- private BrandDao brandDao;
-
-
-
- @Override
- public List
selectAll() { -
-
- List
brands = brandDao.selectAll(); - return brands;
- }
-
- @Override
- public Brand selectById(Integer Id) {
- Brand brand = brandDao.selectById(Id);
- return brand;
- }
-
- @Override
- public boolean save(Brand brand){
- return brandDao.save(brand);
- }
-
- @Override
- public boolean delete(Integer Id) {
- return brandDao.deleteById(Id);
- }
-
- @Override
- public boolean updateById(Brand brand) {
- return brandDao.updateById(brand);
- }
-
-
- }
- package com.brrbaii.controller;
-
- import com.brrbaii.pojo.Brand;
- import com.brrbaii.service.BrandService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.ArrayList;
- import java.util.List;
-
- @RestController
- @RequestMapping("/brands")
- public class BrandController {
- @Autowired
- private BrandService brandService;
- @GetMapping
- public Result getAll(){
- List
brands = brandService.selectAll(); - Integer code = checkMesg(brands) ? Code.SELECT_OK : Code.SELECT_ERR;
- return new Result(code,brands);
- }
-
- @GetMapping("/{Id}")
- public Result getById(@PathVariable Integer Id){
- Brand brand = brandService.selectById(Id);
- Integer code = checkMesg(brand) ? Code.SELECT_OK : Code.SELECT_ERR;
- String msg = checkMesg(brand) ? "查询成功": "查询失败";
- return new Result(code,brand,msg);
- }
-
- @PostMapping
- public Result save(@RequestBody Brand brand){
- boolean flag = brandService.save(brand);
- Integer code = flag ? Code.SAVE_OK : Code.SAVE_ERR;
- return new Result(code,flag);
- }
-
- @DeleteMapping("/{Id}")
- public Result delete(@PathVariable Integer Id){
- boolean flag = brandService.delete(Id);
- Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;
- return new Result(code,flag);
- }
-
- @PutMapping()
- public Result update(@RequestBody Brand brand){
- boolean flag = brandService.updateById(brand);
- Integer code = flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
- return new Result(code,flag);
- }
-
-
- public boolean checkMesg(Object o){
- return o != null ? true : false;
- }
-
- }

-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <scope>runtimescope>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druid-spring-boot-starterartifactId>
- <version>1.2.6version>
- dependency>
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.5.2version>
- dependency>
- mybatis-plus:
- #mapper文件的位置
- mapper-locations: classpath:mapper/*.xml
- #起别名
- type-aliases-package: com.brrbaii.pojo
- #开启日志
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- #为实体类增加前缀
- global-config:
- db-config:
- table-prefix: tb_

mybaits-plus只在mybatis上做了增强,不做改动,如果我们需要编写自定义的SQl语句,还是可以创建一个xml文件去编写具体语句
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
- <mapper namespace="com.brrbaii.dao.MqDao">
-
-
- <select id="selectMapById" resultType="map">
- select * from tb_brand where id = #{id};
- select>
- mapper>
mybatis-plus会自动把实体类的成员变量当做表的字段去查询,驼峰改为下划线_
比如companyName在查询时会被修改为company_name
如果实体类的成员变量和数据库列名不一致,我们可以用@TableFileId注解为它指定名称
