集成MyBatisPlus
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.2</version> </dependency>
引入依赖。
package com.example.bootmp; import org.junit.jupiter.api.Test; import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScans; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest // 表示扫描mapper包下的所有接口 @MapperScan("com.example.bootmp.mapper") class BootMpApplicationTests { @Test void contextLoads() { } }
在Application启动类中配置注解@MapperScan,不过这不是必须的,所以我只在Test单元测试中添加了。
看看,是不是没有加。
但是这样的话,你需要每个Dao单独加上@Mapper注解。
注:
@Mapper是mybatis自身带的注解,但是只是用一个@Mapper的话在service层调用时会爆红,但是不影响使用。
@Repository是spring提供的注释,能够将该类注册成Bean。被依赖注入。使用该注解后,在启动类上要加
@Mapperscan,来表明Mapper类的位置。可以单独使用
@Mapper,也可以在@Mapper下面加一个@Repository就可以消除爆红,也可以使用@Repository但要在启动类上添加@Mapperscan(路径)。
配置MySQL信息
在SpringBoot的配置文件中配置信息,我这里比较喜欢用yml格式的配置。
server: port: 8088 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/user?useUnicode=true&&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: 123456 mybatis-plus: configuration: //设置日志输出 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
我已经建立好了对应数据库与数据表。
然后我们可以编写实体类,在这说明一下,加入你的MySQL中字段命名格式与实体类格式不一样,但是都是常见格式时,我们可以配置MyBatisPlus,如,b_name可以自动的去对应实体类bName,这里不过多解释。
实体类
package com.example.bootmp.entity; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; import lombok.ToString; /** * @author JanYork * @date 2022/9/7 11:28 * @description */ @Getter @Setter @ToString public class User { private Integer id; private String name; }
我这里使用了lombok一键生成Get与Set。
Dao层
有的喜欢用Dao命名,有的喜欢用Mapper,我是后者。
package com.example.bootmp.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.bootmp.entity.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; /** * @author JanYork * @date 2022/9/7 11:32 * @description */ @Mapper public interface UserMapper extends BaseMapper<User> { }
在MyBatisPlus的环境下,我们只需要继承MyBatisPlus提供的BaseMapper类,并将实体类填入泛型后,他会自动生成许许多多常用的增删改查方法。
IDEA自动反编译class后可以看到这些方法。
比如我们使用单元测试调用一下看看:
不过规范的开发还需要Service层和Controller层。
Service层
package com.example.bootmp.service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.bootmp.entity.User; import com.example.bootmp.mapper.UserMapper; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * @author JanYork * @date 2022/9/7 11:37 * @description */ @Service public class UserService extends ServiceImpl<UserMapper, User> { @Resource private UserMapper userMapper; }
也是非常简单,注入对应的Mapper接口,继承MyBatisPlus提供的ServiceImpl类,泛型中第一个参数是对应Mapper类,第二个是对应实体类。
Controller层
package com.example.bootmp.controller; import com.alibaba.fastjson2.JSON; import com.example.bootmp.entity.User; import com.example.bootmp.service.UserService; import com.example.bootmp.utils.R; import org.apache.ibatis.annotations.Mapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; /** * @author JanYork * @date 2022/9/8 8:17 * @description */ @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/list") public Object list() { List<User> list = userService.list(); return JSON.toJSONString(R.ok(list)); } }
Controller层就不需要多说了,@RestController和@RequestMapping("/user")注解声明Controller和URL路径。
用@Autowired注入对应Service层,就可以开始写方法了。
@RequestMapping("/list") public Object list() { List<User> list = userService.list(); return JSON.toJSONString(R.ok(list)); }
我们来看这个方法,这里我用FastJSON来转换并返回JSON字符串。
我们Service里面,MyBatisPlus也是提供了常用增删改查方法。
userService.list()也就是查询所有的信息了,其他方法就暂不测试了。
返回工具类R
新手可能发现,我这里是返回了一个R类,这是干什么。
在规范开发中,我们通常需要为前端传输点数据外其他信息,比如状态码,状态说明等等。
为了方便,我们之间封装成方法调用并一键返回即可。
package com.example.bootmp.utils; /** * @author JanYork * @date 2022/9/8 8:47 * @description 通用返回类 */ public class R { private Integer code; private String msg; private Object data; public R() { } public R(Integer code, String msg, Object data) { this.code = code; this.msg = msg; this.data = data; } public static R ok() { return new R(200, "success", null); } public static R ok(Object data) { return new R(200, "success", data); } public static R error() { return new R(500, "error", null); } public static R error(String msg) { return new R(500, msg, null); } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
msg就是说明,code就是状态码,data是数据信息。
这样返回的数据就较为规范,我们使用Postman调试看看。
图片有些模糊,我之间贴JSON信息。
{ "code": 200, "data": [ { "id": 1, "name": "JanYork_" }, { "id": 5, "name": "111111" } ], "msg": "success" }
code是200,状态描述是成功(success),数据都在data里面,前端都不需要去一个个找,直接获取data就可以。
整体结构
下篇再见。