1、创建模块
1、创建SpringBoot模块
2、勾选Mysql Driver,Mybatis Framework
2、坐标导入
因为刚刚以及勾选了mybatis,mysql,所以他们已经添加到了pom.xml中,下面导入其他坐标。
- lombok 减少get、set方法的编写
- druid 数据库连接池
<!--数据库连接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.4</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
3、项目目录构建
4、编写代码
1、定义实体类(与数据库一一对应)
packagecom.example.mybatis.pojo; importlombok.AllArgsConstructor; importlombok.Data; importlombok.NoArgsConstructor; #lombok的注解减少getset方法publicclassUser { privateIntegerid; privateStringname; privateStringpassword; }
2、mapper
packagecom.example.mybatis.mapper; importcom.example.mybatis.pojo.User; importorg.apache.ibatis.annotations.Mapper; importorg.springframework.stereotype.Repository; importjava.util.List; // 告诉springboot这是一个mybatis的mapepr类// 将userdao交由spring容齐管理publicinterfaceUserMapper { // 查询所有用户publicList<User>listUser(); } 3、service接口packagecom.example.mybatis.service; importcom.example.mybatis.pojo.User; importjava.util.List; publicinterfaceUserService { // 查询所有用户publicList<User>listUser(); }
4、service接口的实现类serviceimpl
package com.example.mybatis.service.impl;
import com.example.mybatis.mapper.UserMapper;
import com.example.mybatis.pojo.User;
import com.example.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service // 交由spring容齐管理
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> listUser() {
return userMapper.listUser();
}
}
5、Controller
packagecom.example.mybatis.controller; importcom.example.mybatis.pojo.User; importcom.example.mybatis.service.UserService; importcom.example.mybatis.untils.JSONResult; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.GetMapping; importorg.springframework.web.bind.annotation.RestController; importjava.util.List; publicclassUserController { privateUserServiceuserService; "/users") (publicJSONResulttest(){ returnJSONResult.ok(userService.listUser()); } }
6、工具类JSONResult
packagecom.example.mybatis.untils; /*** * @Title: JSONResult.java* @Description: 自定义响应数据结构* 本类可提供给 H5/ios/安卓/公众号/小程序 使用* 前端接受此类数据(json object)后,可自行根据业务去实现相关功能* 200:表示成功* 500:表示错误,错误信息在msg字段中* 501:bean验证错误,不管多少个错误都以map形式返回* 502:拦截器拦截到用户token出错* 555:异常抛出信息* 556: 用户qq校验异常* 557: 校验用户是否在CAS登录,用户门票的校验* @Copyright: Copyright (c) 2020*/publicclassJSONResult { // 响应业务状态privateIntegerstatus; // 响应消息privateStringmsg; // 响应中的数据privateObjectdata; privateStringok; // 不使用publicstaticJSONResultbuild(Integerstatus, Stringmsg, Objectdata) { returnnewJSONResult(status, msg, data); } publicstaticJSONResultbuild(Integerstatus, Stringmsg, Objectdata, Stringok) { returnnewJSONResult(status, msg, data, ok); } publicstaticJSONResultok(Objectdata) { returnnewJSONResult(data); } publicstaticJSONResultok() { returnnewJSONResult(null); } publicstaticJSONResulterrorMsg(Stringmsg) { returnnewJSONResult(500, msg, null); } publicstaticJSONResulterrorUserTicket(Stringmsg) { returnnewJSONResult(557, msg, null); } publicstaticJSONResulterrorMap(Objectdata) { returnnewJSONResult(501, "error", data); } publicstaticJSONResulterrorTokenMsg(Stringmsg) { returnnewJSONResult(502, msg, null); } publicstaticJSONResulterrorException(Stringmsg) { returnnewJSONResult(555, msg, null); } publicstaticJSONResulterrorUserQQ(Stringmsg) { returnnewJSONResult(556, msg, null); } publicJSONResult() { } publicJSONResult(Integerstatus, Stringmsg, Objectdata) { this.status=status; this.msg=msg; this.data=data; } publicJSONResult(Integerstatus, Stringmsg, Objectdata, Stringok) { this.status=status; this.msg=msg; this.data=data; this.ok=ok; } publicJSONResult(Objectdata) { this.status=200; this.msg="OK"; this.data=data; } publicBooleanisOK() { returnthis.status==200; } publicIntegergetStatus() { returnstatus; } publicvoidsetStatus(Integerstatus) { this.status=status; } publicStringgetMsg() { returnmsg; } publicvoidsetMsg(Stringmsg) { this.msg=msg; } publicObjectgetData() { returndata; } publicvoidsetData(Objectdata) { this.data=data; } publicStringgetOk() { returnok; } publicvoidsetOk(Stringok) { this.ok=ok; } }
7、配置文件
server: port: 8080#数据库数据源spring: datasource: type: com.alibaba.druid.pool.DruidDataSource#数据源类型username: rootpassword: xmpkjurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMTdriver-class-name: com.mysql.cj.jdbc.Drivermybatis: mapper-locations: classpath:/mapper/*.xml # xml文件
8、mapper.xml
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.example.mybatis.mapper.UserMapper"><selectid="listUser"resultType="com.example.mybatis.pojo.User">select*fromuser</select></mapper>
5、测试
运行项目,打开Postman或者Apifox访问:localhost:8080/users
成功从数据库查到信息