手把手教你springboot整合mybatis+vue+饿了么实现增删改查从0到1适合初学者
创建springboot 工程 添加pom架构springboot + mybatis + mysql + lombok + vue+ 饿了么前期准备工作先创建后台springboot改jdk版本改包名选几个常用我通常用的都是yml 所以我把 properties删除 创建一个yml 文件dev 里面的代码server:port: 7000logging:level:root: infoc
创建springboot 工程 添加pom
架构
springboot + mybatis + mysql + lombok + vue + 饿了么
前期准备工作
先创建后台springboot
改jdk版本
改包名
选几个常用的pom
起个项目名字
上面是名字下面是路径
我通常用的都是yml 所以我把 properties删除 创建一个yml 文件
dev 里面的代码
server:
port: 7000
logging:
level:
root: info
com.joe: debug
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT%2b8&serverTimezone=Asia/Shanghai
username: root
password: root
initialSize: 1
minIdle: 3
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 30000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
hikari:
connection-test-query: select 1
connection-timeout: 60000
idle-timeout: 500000
max-lifetime: 540000
maximum-pool-size: 12
minimum-idle: 10
pool-name: GGLiliKariPool
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
thymeleaf:
check-template: false
check-template-location: false
mybatis:
mapperLocations: classpath:com/joeworld/demo/mapper/*.xml
type-aliases-package: com.joeworld.demo.entity # 实体类所在的位置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
在pom 里面添加 再添加几个必要的包 我吧全部都列出来 直接复制就行
<dependencies>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- 阿里巴巴数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- springboot 测试包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- web包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
创建数据库 —创建表
CREATE TABLE `t_student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_num` int(20) DEFAULT NULL COMMENT '学号',
`student_name` varchar(50) CHARACTER SET utf8 DEFAULT NULL COMMENT '学生姓名',
`nation` varchar(10) DEFAULT NULL COMMENT '民族',
`college` varchar(50) DEFAULT NULL COMMENT '学院',
`major` varchar(50) DEFAULT NULL COMMENT '专业',
`grade` varchar(20) DEFAULT NULL COMMENT '年级',
`student_class` varchar(20) DEFAULT NULL COMMENT '学生班级',
`student_id` varchar(20) DEFAULT NULL COMMENT '学生身份证',
`student_sex` varchar(1) DEFAULT NULL COMMENT '性别 1男 2女',
`create_people` varchar(30) DEFAULT NULL COMMENT '创建人',
`create_time` varchar(30) DEFAULT NULL COMMENT '创建时间',
`update_people` varchar(30) DEFAULT NULL COMMENT '修改人',
`update_time` varchar(30) DEFAULT NULL COMMENT '修改时间',
`is_delete` varchar(2) DEFAULT NULL COMMENT '是否有效 10有效 00 无效',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='学生表';
INSERT INTO `t_student` VALUES (1, 2022020202, '法外狂徒张三', '少数民族', '信息工程学院', '软件工程', '一年级', '三班', '101010101001', '1', NULL, NULL, NULL, NULL, '10');
INSERT INTO `t_student` VALUES (2, 2022020203, '法外狂徒李四', '少数民族', '信息工程学院', '软件工程', '一年级', '三班', '101010101002', '1', NULL, NULL, NULL, NULL, '10');
INSERT INTO `t_student` VALUES (3, 2022020204, '法外狂徒二麻子', '少数民族', '信息工程学院', '软件工程', '一年级', '三班', '101010101003', '1', NULL, NULL, NULL, NULL, '10');
准备工作基本完成了 接下来就开始快乐之旅吧
看一眼目录结构
把实体和工具类搞出来 Student实体类
package com.joeworld.demo.entity;
import lombok.Data;
@Data
public class Student {
private Integer id;
/** 学号 */
private Integer studentNum;
/** 学生姓名 */
private String studentName;
/** 民族 */
private String nation;
/** 学院 */
private String college;
/** 专业 */
private String major;
/** 年级 */
private String grade;
/** 学生班级 */
private String studentClass;
/** 学生身份证 */
private String studentId;
/** 性别 1男 2女 */
private String studentSex;
/** 创建人 */
private String createPeople;
/** 创建时间 */
private String createTime;
/** 修改人 */
private String updatePeople;
/** 修改时间 */
private String updateTime;
/** 是否有效 10有效 00 无效 */
private String isDelete;
}
R(用于统一返回的) 代码都是固定的不要考虑直接复制
package com.joeworld.demo.utils;
import lombok.Data;
@Data
public class R {
private int code = 200;
private String msg ="成功";
private Object data;
public R() {
}
public static R error() {
return error(500, "未知异常,请联系管理员");
}
public static R error(String msg) {
return error(500, msg);
}
public static R error(int code, String msg) {
R r = new R();
r.setCode(code);
r.setMsg(msg);
return r;
}
public static R ok(String msg) {
R r = new R();
r.setMsg(msg);
return r;
}
public static R ok(Object data) {
R r = new R();
r.setData(data);
return r;
}
public static R ok() {
return new R();
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return this.data;
}
public void setData(Object data) {
this.data = data;
}
}
这个PageInfo 和R的作用查不到 都是用于统一返回数据的 区别是PageInfo 是分页
注意啊 PageInfo这个类 pagehelper 包下也有一个一样的 别用错了啊 后面还有会提到
package com.joeworld.demo.config;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageSerializable;
import java.util.Collection;
import java.util.List;
@SuppressWarnings({"rawtypes", "unchecked"})
public class PageInfo<T> extends PageSerializable<T> {
//当前页
private int pageNum;
//是否成功 200 成功 500 失败
private int code;
//是否成功 成功或者失败的消息
private String message;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总页数
private int pages;
//前一页
private int prePage;
//下一页
private int nextPage;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public PageInfo() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
/**
* 包装Page对象
*
* @param list
*/
public PageInfo(List<T> list) {
this(list, 8);
}
/**
* 包装Page对象
*
* @param list page结果
* @param navigatePages 页码数量
*/
public PageInfo(List<T> list, int navigatePages) {
super(list);
if (list instanceof Page) {
Page page = (Page) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.pages = page.getPages();
this.size = page.size();
//由于结果是>startRow的,所以实际的需要+1
if (this.size == 0) {
this.startRow = 0;
this.endRow = 0;
} else {
this.startRow = page.getStartRow() + 1;
//计算实际的endRow(最后一页的时候特殊)
this.endRow = this.startRow - 1 + this.size;
}
} else if (list instanceof Collection) {
this.pageNum = 1;
this.pageSize = list.size();
this.pages = this.pageSize > 0 ? 1 : 0;
this.size = list.size();
this.startRow = 0;
this.endRow = list.size() > 0 ? list.size() - 1 : 0;
}
if (list instanceof Collection) {
this.navigatePages = navigatePages;
//计算导航页
calcNavigatepageNums();
//计算前后页,第一页,最后一页
calcPage();
//判断页面边界
judgePageBoudary();
}
}
public static <T> com.github.pagehelper.PageInfo<T> of(List<T> list){
return new com.github.pagehelper.PageInfo<T>(list);
}
public static <T> com.github.pagehelper.PageInfo<T> of(List<T> list, int navigatePages){
return new com.github.pagehelper.PageInfo<T>(list, navigatePages);
}
/**
* 计算导航页
*/
private void calcNavigatepageNums() {
//当总页数小于或等于导航页码数时
if (pages <= navigatePages) {
navigatepageNums = new int[pages];
for (int i = 0; i < pages; i++) {
navigatepageNums[i] = i + 1;
}
} else { //当总页数大于导航页码数时
navigatepageNums = new int[navigatePages];
int startNum = pageNum - navigatePages / 2;
int endNum = pageNum + navigatePages / 2;
if (startNum < 1) {
startNum = 1;
//(最前navigatePages页
for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++;
}
} else if (endNum > pages) {
endNum = pages;
//最后navigatePages页
for (int i = navigatePages - 1; i >= 0; i--) {
navigatepageNums[i] = endNum--;
}
} else {
//所有中间页
for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++;
}
}
}
}
/**
* 计算前后页,第一页,最后一页
*/
private void calcPage() {
if (navigatepageNums != null && navigatepageNums.length > 0) {
navigateFirstPage = navigatepageNums[0];
navigateLastPage = navigatepageNums[navigatepageNums.length - 1];
if (pageNum > 1) {
prePage = pageNum - 1;
}
if (pageNum < pages) {
nextPage = pageNum + 1;
}
}
}
/**
* 判定页面边界
*/
private void judgePageBoudary() {
isFirstPage = pageNum == 1;
isLastPage = pageNum == pages || pages == 0;;
hasPreviousPage = pageNum > 1;
hasNextPage = pageNum < pages;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public int getEndRow() {
return endRow;
}
public void setEndRow(int endRow) {
this.endRow = endRow;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
@Deprecated
// firstPage就是1, 此函数获取的是导航条上的第一页, 容易产生歧义
public int getFirstPage() {
return navigateFirstPage;
}
@Deprecated
public void setFirstPage(int firstPage) {
this.navigateFirstPage = firstPage;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
@Deprecated
// 请用getPages()来获取最后一页, 此函数获取的是导航条上的最后一页, 容易产生歧义.
public int getLastPage() {
return navigateLastPage;
}
@Deprecated
public void setLastPage(int lastPage) {
this.navigateLastPage = lastPage;
}
public boolean isIsFirstPage() {
return isFirstPage;
}
public void setIsFirstPage(boolean isFirstPage) {
this.isFirstPage = isFirstPage;
}
public boolean isIsLastPage() {
return isLastPage;
}
public void setIsLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public int getNavigatePages() {
return navigatePages;
}
public void setNavigatePages(int navigatePages) {
this.navigatePages = navigatePages;
}
public int[] getNavigatepageNums() {
return navigatepageNums;
}
public void setNavigatepageNums(int[] navigatepageNums) {
this.navigatepageNums = navigatepageNums;
}
public int getNavigateFirstPage() {
return navigateFirstPage;
}
public int getNavigateLastPage() {
return navigateLastPage;
}
public void setNavigateFirstPage(int navigateFirstPage) {
this.navigateFirstPage = navigateFirstPage;
}
public void setNavigateLastPage(int navigateLastPage) {
this.navigateLastPage = navigateLastPage;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("PageInfo{");
sb.append("pageNum=").append(pageNum);
sb.append(", pageSize=").append(pageSize);
sb.append(", size=").append(size);
sb.append(", startRow=").append(startRow);
sb.append(", endRow=").append(endRow);
sb.append(", total=").append(total);
sb.append(", pages=").append(pages);
sb.append(", list=").append(list);
sb.append(", prePage=").append(prePage);
sb.append(", nextPage=").append(nextPage);
sb.append(", isFirstPage=").append(isFirstPage);
sb.append(", isLastPage=").append(isLastPage);
sb.append(", hasPreviousPage=").append(hasPreviousPage);
sb.append(", hasNextPage=").append(hasNextPage);
sb.append(", navigatePages=").append(navigatePages);
sb.append(", navigateFirstPage=").append(navigateFirstPage);
sb.append(", navigateLastPage=").append(navigateLastPage);
sb.append(", navigatepageNums=");
if (navigatepageNums == null) {
sb.append("null");
} else {
sb.append('[');
for (int i = 0; i < navigatepageNums.length; ++i) {
sb.append(i == 0 ? "" : ", ").append(navigatepageNums[i]);
}
sb.append(']');
}
sb.append('}');
return sb.toString();
}
}
好了 实体和工具都搞出来了 开始写业务代码吧
我平时的习惯是 先写controller 有同学习惯是先写mapper
带有分页 和不带有分页我都写一下
controller
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 分页查询
* @param params
* @return
*/
@RequestMapping("/findPage")
public PageInfo<ClassInfo> findPage(@RequestParam Map<String, Object> params){
PageInfo<ClassInfo> pageInfo = studentService.findPage(params);
return pageInfo;
}
/**
*查询所有记录(不带有分页)
* @param params
* @return 返回集合,没有返回空List
*/
@RequestMapping("list")
public R listAll(@RequestParam Map<String, Object> params) {
return R.ok(studentService.listAll(params));
}
}
service
public interface StudentService {
/**
* 分页查询所有记录
* @param map
* @return
*/
PageInfo<ClassInfo> findPage(Map<String,Object> map);
/**
* 查询所有记录
*
* @return 返回集合,没有返回空List
*/
List<Student> listAll(Map<String,Object> map);
}
实现类(serviceImpl)
@Service
public class StudentServiceImpl implements StudentService {
/**
* 分页查询所有记录
* @param map
* @return
*/
@Override
public PageInfo<ClassInfo> findPage(Map<String, Object> map) {
PageHelper.startPage(Integer.valueOf(map.get("pageCode").toString()), Integer.valueOf(map.get("pageSize").toString()));
List<ClassInfo> page = studentMapper.findPage(map);
return new PageInfo<>(page);
}
/**
* 查询所有记录
*
* @return 返回集合,没有返回空List
*/
@Override
public List<Student> listAll(Map<String,Object> map) {
return studentMapper.listAll(map);
}
}
在这里提醒一下 PageInfo 用我们自己写的那个 不要用pagehelper 包下的
(我不会告诉你 这两个PageInfo 是一样的没有啥大的区别 自己写的就多了一个code和一个message)
dao
@Mapper
public interface StudentMapper {
/**
* 分页查询所有记录
*
* @return 返回集合,没有返回空List
*/
List<ClassInfo> findPage(Map<String,Object> map);
/**
* 查询所有记录
*
* @return 返回集合,没有返回空List
*/
List<Student> listAll(Map<String,Object> map);
}
mappper
注意namespace路径和parameterType 的路径
<mapper namespace="com.joeworld.demo.dao.StudentMapper">
<!-- 在这里做一下映射 column是数据库的字段 property 是实体的字段-->
<resultMap id="BaseResultMap" type="com.joeworld.demo.entity.Student">
<result column="id" property="id" />
<result column="student_num" property="studentNum" />
<result column="student_name" property="studentName" />
<result column="nation" property="nation" />
<result column="college" property="college" />
<result column="major" property="major" />
<result column="grade" property="grade" />
<result column="student_class" property="studentClass" />
<result column="student_id" property="studentId" />
<result column="student_sex" property="studentSex" />
<result column="create_people" property="createPeople" />
<result column="create_time" property="createTime" />
<result column="update_people" property="updatePeople" />
<result column="update_time" property="updateTime" />
<result column="is_delete" property="isDelete" />
</resultMap>
<!-- 表字段 单独拿出来为了统一 不然太乱了 如果哪天你要加个字段 这么多方法都要一个一个加 太麻烦 为了后期好维护 -->
<sql id="baseColumns">
id
, student_num
, student_name
, nation
, college
, major
, grade
, student_class
, student_id
, student_sex
, create_people
, create_time
, update_people
, update_time
, is_delete
</sql>
<!-- 分页查询全部 下面这些if 是判断是否传值 如果传值的话就拼接上一个and-->
<select id="findPage" resultMap="BaseResultMap">
SELECT
<include refid="baseColumns" />
FROM t_student
<where>
is_delete=10
<if test="null != studentNum and null != studentNum.value and '' != studentNum.value">
and student_num=#{studentNum}
</if>
<if test="null != studentName and null != studentName.value and '' != studentName.value">
and student_name=#{studentName}
</if>
<if test="null != nation and null != nation.value and '' != nation.value">
and nation=#{nation}
</if>
<if test="null != college and null != college.value and '' != college.value">
and college=#{college}
</if>
<if test="null != major and null != major.value and '' != major.value">
and major=#{major}
</if>
<if test="null != grade and null != grade.value and '' != grade.value">
and grade=#{grade}
</if>
<if test="null != studentClass and null != studentClass.value and '' != studentClass.value">
and student_class=#{studentClass}
</if>
<if test="null != studentId and null != studentId.value and '' != studentId.value">
and student_id=#{studentId}
</if>
<if test="null != studentSex and null != studentSex.value and '' != studentSex.value">
and student_sex=#{studentSex}
</if>
<if test="null != createPeople and null != createPeople.value and '' != createPeople.value">
and create_people=#{createPeople}
</if>
<if test="null != createTime and null != createTime.value and '' != createTime.value">
and create_time=#{createTime}
</if>
<if test="null != updatePeople and null != updatePeople.value and '' != updatePeople.value">
and update_people=#{updatePeople}
</if>
<if test="null != updateTime and null != updateTime.value and '' != updateTime.value">
and update_time=#{updateTime}
</if>
<if test="null != isDelete and null != isDelete.value and '' != isDelete.value">
and is_delete=#{isDelete}
</if>
</where>
order by id desc
<if test="offset != null and limit != null">
limit #{offset}, #{limit}
</if>
</select>
<!-- 查询所有记录(不带有分页) -->
<select id="listAll" resultMap="BaseResultMap">
SELECT
<include refid="baseColumns" />
FROM t_student t
<where>
is_delete=10
<if test="null != studentNum and null != studentNum.value and '' != studentNum.value">
and student_num=#{studentNum}
</if>
<if test="null != studentName and null != studentName.value and '' != studentName.value">
and student_name=#{studentName}
</if>
<if test="null != nation and null != nation.value and '' != nation.value">
and nation=#{nation}
</if>
<if test="null != college and null != college.value and '' != college.value">
and college=#{college}
</if>
<if test="null != major and null != major.value and '' != major.value">
and major=#{major}
</if>
<if test="null != grade and null != grade.value and '' != grade.value">
and grade=#{grade}
</if>
<if test="null != studentClass and null != studentClass.value and '' != studentClass.value">
and student_class=#{studentClass}
</if>
<if test="null != studentId and null != studentId.value and '' != studentId.value">
and student_id=#{studentId}
</if>
<if test="null != studentSex and null != studentSex.value and '' != studentSex.value">
and student_sex=#{studentSex}
</if>
<if test="null != createPeople and null != createPeople.value and '' != createPeople.value">
and create_people=#{createPeople}
</if>
<if test="null != createTime and null != createTime.value and '' != createTime.value">
and create_time=#{createTime}
</if>
<if test="null != updatePeople and null != updatePeople.value and '' != updatePeople.value">
and update_people=#{updatePeople}
</if>
<if test="null != updateTime and null != updateTime.value and '' != updateTime.value">
and update_time=#{updateTime}
</if>
<if test="null != isDelete and null != isDelete.value and '' != isDelete.value">
and is_delete=#{isDelete}
</if>
</where>
order by id desc
</select>
</mapper>
这样查询就搞完了 用springboot 自带的测试类测一下 有没有啥问题
@Autowired
private StudentService studentService;
@Test
void listAll() {
HashMap<String , Object> map = new HashMap<>();
map.put("studentName","法外狂徒张三"); //带有条件 查询studentName 是法外狂徒张三的学生
List<Student> students = studentService.listAll(map);
students.forEach(System.out::println);
}
不出意外的话 会报错 看一眼是啥错 它说不合法的状态异常 没有找到上下文 想一想为啥 …
我们来看一眼启动类
把这些注解加进去 启动
@EnableAutoConfiguration(exclude = {
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
@EnableTransactionManagement
@MapperScan("com.joeworld.**.dao") // dao 的路径 ** 代表全部
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
改完启动 这次更严重 项目都跑不起来了 一声卧槽 骂骂咧咧的去百度一波 发现
springboot 在2.6.0之后默认禁止循环依赖了
所以我们把版本改成 2.6.0以下 我改成2.4.5
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
👇
再来跑一下
此时心中 是不是有一万只草泥马在奔腾
再来回想一下 我们是不是没有把xml文件放在 resources 文件夹下 这时候有两个解决方案
1 把xml移动到resources 文件夹下 同时目录结构要和src 下的目录一样 不建议太麻烦
2 在pom中的build标签中添加一个配置
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
👇
第二种比较方面 加上这个resources 标签就行了
再跑一下 带有条件
可以看到 左边有绿色的√ 右边有返回的数据 就是成功了 并且有数据返回
在来查询student 中的全部数据 整挺好 都出来了
再来测一下带有分页的
@Test
public void listPage(){
HashMap<String , Object> map = new HashMap<>();
map.put("pageCode",2); //从几条开始
map.put("pageSize",2); //查询几条
PageInfo<ClassInfo> page = studentService.findPage(map);
System.out.println(page);
}
再来 搞个新增
先从测试类开始写
@Test
void insertStudent() {
Student student = new Student(); //new 一个学生对象
student.setStudentNum(123312); //学号
student.setStudentName("幼儿园老大张三"); //学生姓名
student.setNation("大汉"); //民族
student.setCollege("幼儿园学院"); //学院
student.setMajor("幼儿专业"); //专业
student.setStudentClass("小二班"); //班级
student.setStudentId("1111222");// 学生身份证
student.setStudentSex("1"); //性别
student.setIsDelete("10"); //有效
int insert = studentService.insert(student);
if (insert > 0){
System.out.println("新增成功");
}else {
System.out.println("新增失败");
}
}
controller (这个时候还没有用到controller)
/**
* 新增,忽略null字段
*
* @param student 新增的记录
* @return 返回影响行数
*/
@RequestMapping("insert")
public R insert(@RequestBody Student student) {
return R.ok(studentService.insertIgnoreNull(student));
}
service
/**
* 新增,插入所有字段
*
* @param student 新增的记录
* @return 返回影响行数
*/
int insert(Student student);
serviceImpl
/**
* 新增,插入所有字段
*
* @param student 新增的记录
* @return 返回影响行数
*/
@Override
public int insert(Student student) {
return studentMapper.insert(student);
}
dao
/**
* 新增,忽略null字段
*
* @param student 新增的记录
* @return 返回影响行数
*/
int insertIgnoreNull(Student student);
mapper
<!-- 插入不为NULL的字段 -->
<insert id="insertIgnoreNull" parameterType="com.joeworld.demo.entity.Student"
keyProperty="id" keyColumn="id" useGeneratedKeys="true">
INSERT INTO t_student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="null != studentNum and null != studentNum.value and '' != studentNum.value">
student_num,
</if>
<if test="null != studentName and null != studentName.value and '' != studentName.value">
student_name,
</if>
<if test="null != nation and null != nation.value and '' != nation.value">
nation,
</if>
<if test="null != college and null != college.value and '' != college.value">
college,
</if>
<if test="null != major and null != major.value and '' != major.value">
major,
</if>
<if test="null != grade and null != grade.value and '' != grade.value">
grade,
</if>
<if test="null != studentClass and null != studentClass.value and '' != studentClass.value">
student_class,
</if>
<if test="null != studentId and null != studentId.value and '' != studentId.value">
student_id,
</if>
<if test="null != studentSex and null != studentSex.value and '' != studentSex.value">
student_sex,
</if>
<if test="null != createPeople and null != createPeople.value and '' != createPeople.value">
create_people,
</if>
<if test="null != createTime and null != createTime.value and '' != createTime.value">
create_time,
</if>
<if test="null != updatePeople and null != updatePeople.value and '' != updatePeople.value">
update_people,
</if>
<if test="null != updateTime and null != updateTime.value and '' != updateTime.value">
update_time,
</if>
<if test="null != isDelete and null != isDelete.value and '' != isDelete.value">
is_delete,
</if>
</trim>
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="null != studentNum and null != studentNum.value and '' != studentNum.value">
#{studentNum},
</if>
<if test="null != studentName and null != studentName.value and '' != studentName.value">
#{studentName},
</if>
<if test="null != nation and null != nation.value and '' != nation.value">
#{nation},
</if>
<if test="null != college and null != college.value and '' != college.value">
#{college},
</if>
<if test="null != major and null != major.value and '' != major.value">
#{major},
</if>
<if test="null != grade and null != grade.value and '' != grade.value">
#{grade},
</if>
<if test="null != studentClass and null != studentClass.value and '' != studentClass.value">
#{studentClass},
</if>
<if test="null != studentId and null != studentId.value and '' != studentId.value">
#{studentId},
</if>
<if test="null != studentSex and null != studentSex.value and '' != studentSex.value">
#{studentSex},
</if>
<if test="null != createPeople and null != createPeople.value and '' != createPeople.value">
#{createPeople},
</if>
<if test="null != createTime and null != createTime.value and '' != createTime.value">
#{createTime},
</if>
<if test="null != updatePeople and null != updatePeople.value and '' != updatePeople.value">
#{updatePeople},
</if>
<if test="null != updateTime and null != updateTime.value and '' != updateTime.value">
#{updateTime},
</if>
<if test="null != isDelete and null != isDelete.value and '' != isDelete.value">
#{isDelete},
</if>
</trim>
</insert>
瞄一眼数据库 多了一个刚新增的数据
新增搞完了 得搞个修改呀 没有修改可不行
测试类
注意要添加id 啊 根据id 进行修改的 不加id 要么报错要么全表修改了 注意
@Test
void updateStudent() {
Student student = new Student(); //new 一个学生对象
student.setId(4);
student.setStudentNum(123312); //学号
student.setStudentName("幼儿园老大三麻子"); //学生姓名
student.setNation("汉"); //民族
student.setCollege("计算器学院"); //学院
student.setMajor("计算机专业"); //专业
student.setStudentClass("大班"); //班级
student.setStudentId("12312322");// 学生身份证
student.setStudentSex("2"); //性别
int update = studentService.update(student);
if (update > 0) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
}
controller
/**
* 修改,忽略null字段
*
* @param student 修改的记录
* @return 返回影响行数
*/
@RequestMapping("update")
public R update(@RequestBody Student student) {
return R.ok(studentService.updateIgnoreNull(student));
}
service
/**
* 修改,修改所有字段
*
* @param student 修改的记录
* @return 返回影响行数
*/
int update(Student student);
serviceImpl
/**
* 修改,忽略null字段
*
* @param student 修改的记录
* @return 返回影响行数
*/
@Override
public int updateIgnoreNull(Student student) {
return studentMapper.updateIgnoreNull(student);
}
dao
/**
* 修改,忽略null字段
*
* @param student 修改的记录
* @return 返回影响行数
*/
int updateIgnoreNull(Student student);
mapper
<!-- 更新不为NULL的字段 -->
<update id="updateIgnoreNull" parameterType="com.joeworld.demo.entity.Student">
UPDATE t_student
<set>
<if test="null != studentNum and null != studentNum.value and '' != studentNum.value">
student_num=#{studentNum},
</if>
<if test="null != studentName and null != studentName.value and '' != studentName.value">
student_name=#{studentName},
</if>
<if test="null != nation and null != nation.value and '' != nation.value">
nation=#{nation},
</if>
<if test="null != college and null != college.value and '' != college.value">
college=#{college},
</if>
<if test="null != major and null != major.value and '' != major.value">
major=#{major},
</if>
<if test="null != grade and null != grade.value and '' != grade.value">
grade=#{grade},
</if>
<if test="null != studentClass and null != studentClass.value and '' != studentClass.value">
student_class=#{studentClass},
</if>
<if test="null != studentId and null != studentId.value and '' != studentId.value">
student_id=#{studentId},
</if>
<if test="null != studentSex and null != studentSex.value and '' != studentSex.value">
student_sex=#{studentSex},
</if>
<if test="null != createPeople and null != createPeople.value and '' != createPeople.value">
create_people=#{createPeople},
</if>
<if test="null != createTime and null != createTime.value and '' != createTime.value">
create_time=#{createTime},
</if>
<if test="null != updatePeople and null != updatePeople.value and '' != updatePeople.value">
update_people=#{updatePeople},
</if>
<if test="null != updateTime and null != updateTime.value and '' != updateTime.value">
update_time=#{updateTime},
</if>
<if test="null != isDelete and null != isDelete.value and '' != isDelete.value">
is_delete=#{isDelete},
</if>
</set>
WHERE id = #{id}
</update>
修改都写完了 这不得来个根据id查询啊 整合vue的时候这里要改 留意一下
测试类
@Test
void findById(){
Student byId = studentService.getById(4);
System.out.println(byId);
}
service
/**
* 根据主键查询
*
* @param id 主键
* @return 返回记录,没有返回null
*/
Student getById(Integer id);
serviceImpl
@Override
public Student getById(Integer id) {
return studentMapper.getById(id);
}
dao
Student getById(Integer id);
Mapper
<!-- 根据主键获取单条记录 -->
<select id="getById" resultMap="BaseResultMap" parameterType="Integer">
SELECT id
, student_num
, student_name
, nation
, college
, major
, grade
, student_class
, student_id
, student_sex
, create_people
, create_time
, update_people
, update_time
, is_delete FROM t_student t WHERE is_delete=10 and id = #{id}
</select>
测试
好 到此为止 后台的增删改查全部完事了
开始整合前端
咱去GitHub拉一下 vue-admin-template 的代码
地址 :https://github.com/PanJiaChen/vue-admin-template
有整合过git 的同学 可以直接用git 拉 没有整合过 就下载这压缩包
如果连GitHub 都打不开的同学可以去下载 dev-sidecar 这个东西加速很猛 刹不住车的那种
地址 https://gitee.com/docmirror/dev-sidecar/releases
介绍一下vue-admin-template吧 (官网解释)
这是一个极简的 vue admin 管理后台。它只包含了 Element UI & axios & iconfont & permission control & lint,这些搭建后台必要的东西。
说白了就是前端的架子它把搭好了 直接用就行了
在线演示地址
https://panjiachen.gitee.io/vue-element-admin/#/dashboard
下载vue开发环境
引用阿梅的文章 https://www.cnblogs.com/zhaomeizi/p/8483597.html
安装 完成后 可以选择使用vscode 也可以使用idea作为开发工具 用idea的话要安装一下vue插件 就这个东西
我是用过idea 打开的 我平时比较喜欢用idea 虽然vscode很强大但是我不用
写在最后 未完成 待补充
更多推荐
所有评论(0)