实践出真知,先码一个博客系统先,参考https://juejin.cn/post/6844903823966732302。
1 使用IDEA新建项目->Spring Initializr->下一步,填写组/包名字->下一步选择依赖。(Developer Tools钩上Spring Boot Devtools和 Lombok,
Web钩上Spring Web,SQL钩上MySQL Driver)->下一步填写项目目录即可完成创建。
2 配置MyBatis-Plus用于数据库链接参考https://mp.baomidou.com/guide/,首先添加依赖,更新。
修改src/main/resources/application.properties改名application.yml
# DataSource Config spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/blog?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root password: 123 mybatis-plus: mapper-locations: classpath*:/mapper/**Mapper.xml
3 打开navicat,输入sql语句执行,建表。
CREATE TABLE `m_user`( `id` bigint(20) NOT NULL AUTO_INCREMENT, `username` varchar(64) DEFAULT NULL, `avatar` varchar(255) DEFAULT NULL, `email` varchar(64) DEFAULT NULL, `password` varchar(64) DEFAULT NULL, `status` int(5) NOT NULL, `created` datetime DEFAULT NULL, `last_login` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `UK_USERNAME` (`username`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE `m_blog`( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(20) NOT NULL, `title` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, `content` longtext, `created` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP, `status` tinyint(4) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4; INSERT INTO `blog`.`m_user` (`id`, `username`, `avatar`, `email`, `password`, `status`, `created`, `last_login`) VALUES ('1', 'markerhub', 'https://image-1300566513.cos.ap-guangzhou.myqcloud.com/upload/images/5a9f48118166308daba8b6da7e466aab.jpg', NULL, '96e79218965eb72c92a549dd5a330112', '0', '2020-04-20 10:44:01', NULL);
3 引入spring-boot-starter-freemarker用于给前端发送模版
4 MyBatis-Plus有一个分页插件PaginationInterceptor,开启mapper接口扫描,添加分页插件,通过@mapperScan注解指定要变成
实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类。
项目新增类com.blog.config.MybatisPlusConfig插入配置
package com.blog.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableTransactionManagement @MapperScan("com.blog.mapper") public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }
5 项目创建CodeGenerator类自动生成表对应的controller/entity/mapper/service文件夹下对应表代码。
6 测试一下写个接口。
package com.blog.controller; import com.blog.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * <p> * 前端控制器 * </p> * * @author 关注公众号:MarkerHub * @since 2021-02-15 */ @RestController @RequestMapping("/user") public class UserController { @Autowired UserService userService; @GetMapping("/index") public Object index(){ return userService.getById(1l); } }
7 运行项目打开浏览器输入网址http://localhost:8080/user/index获取到了用户单条信息
8 封装接口返回类。新建类common.lang.Result,代码如下
package com.blog.common.lang; import lombok.Data; import java.io.Serializable; @Data public class Result implements Serializable { private int code; private String msg; private Object data; public static Result succ(int code,String msg,Object data){ Result res=new Result(); res.setCode(code); res.setData(data); res.setMsg(msg); return res; } public static Result succ(Object data){ return succ(200,"success",data); } public static Result fail(int code,String msg,Object data){ Result res=new Result(); res.setCode(code); res.setData(data); res.setMsg(msg); return res; } public static Result fail(String msg){ return succ(400,msg,null); } }
然后修改接口方法直接返回Result类型即可
错误列表备注
1 未找到项目org.springframework.boot:spring-boot-starter-parent2.4.2 ,重新maven下载改为版本2.2.6.RELEASE。