慕课网《SpringBoot+MyBatis搭建迷你小程序》学习总结
慕课网《SpringBoot+MyBatis搭建迷你小程序》学习总结
- 时间:2018年04月16日星期一
- 说明:本文部分内容均来自慕课网。@慕课网:https://www.imooc.com
- 教学源码:无
- 学习源码:https://github.com/zccodere/study-imooc
学习收获
- 学会从零搭建后端的SpringBoot + Mybatis框架
- 微信小程序开发入门
- 规范的代码编写
- 实现后端的业务功能
- 实现本地微信小程序的前端开发
- 前端与后端的联调
技术要求
- 基本的Java知识
- 基本的前端开发知识Html、JS、CSS
- 基本了解Spring、Mybatis知识
功能列表
- 新增区域
- 删除区域
- 修改区域
- 查询区域
代码编写
1.创建名为small的maven工程pom如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myimooc</groupId>
<artifactId>small</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>small</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!-- 如果配置了拷贝java classpath下面的xml,项目中其他的配置文件应该手动配置复制过去 -->
<!-- <resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource> -->
</resources>
</build>
</project>
2.创建名为small的数据库初始化建表SQL如下
CREATE TABLE `tb_area` (
`area_id` int(2) NOT NULL AUTO_INCREMENT ,
`area_name` varchar(255) NOT NULL ,
`priority` int(2) NOT NULL DEFAULT 0 ,
`create_time` datetime NULL DEFAULT NULL,
`last_edit_time` datetime NULL DEFAULT NULL,
PRIMARY KEY (`area_id`),
UNIQUE KEY `UK_AREA`(`area_name`)
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
3.编写application.properties
server.port=8080
server.servlet.context-path=/small
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/small?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
4.编写SmallStart类
package com.myimooc.small;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
* <br>
* 标题: 启动应用<br>
* 描述: 慕课网《SpringBoot+MyBatis搭建迷你小程序》<br>
* SpringBoot 整合 Mybatis 时,必须加 @MapperScan
*
* @author zc
* @date 2018/04/16
*/
@SpringBootApplication
@MapperScan("com.myimooc.small.mapper")
public class SmallStart {
public static void main(String[] args) {
SpringApplication.run(SmallStart.class, args);
}
}
第三章:项目开发
3-1 数据访问
代码编写
1.编写Area类
package com.myimooc.small.po;
import java.io.Serializable;
import java.util.Date;
/**
* <br>
* 标题: 区域表PO<br>
* 描述: tb_area 表PO<br>
*
* @author zc
* @date 2018/04/16
*/
public class Area implements Serializable {
private static final long serialVersionUID = -3084550177471749048L;
/**
* 主键 区域 ID
*/
private Integer areaId;
/**
* 区域名称
*/
private String areaName;
/**
* 权重,越大越排前显示
*/
private Integer priority;
/**
* 创建时间
*/
private Date createTime;
/**
* 最后一次修改时间
*/
private Date lastEditTime;
@Override
public String toString() {
return "Area{" +
"areaId=" + areaId +
", areaName='" + areaName + '\'' +
", priority=" + priority +
", createTime=" + createTime +
", lastEditTime=" + lastEditTime +
'}';
}
public Integer getAreaId() {
return areaId;
}
public void setAreaId(Integer areaId) {
this.areaId = areaId;
}
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastEditTime() {
return lastEditTime;
}
public void setLastEditTime(Date lastEditTime) {
this.lastEditTime = lastEditTime;
}
}
2.编写AreaMapper类
package com.myimooc.small.mapper;
import com.myimooc.small.po.Area;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* <br>
* 标题: 区域表 DAO<br>
* 描述: tb_area 表 DAO<br>
*
* @author zc
* @date 2018/04/17
*/
@Repository
public interface AreaMapper {
/**
* 批量查询
*
* @return 区域集合
*/
List<Area> listAll();
/**
* 详情查询
*
* @param areaId 区域ID
* @return 区域信息
*/
Area findById(Integer areaId);
/**
* 创建区域
*
* @param area 区域 PO
* @return 影响记录数
*/
Integer create(Area area);
/**
* 修改区域
*
* @param area 区域 PO
* @return 影响记录数
*/
Integer update(Area area);
/**
* 删除区域
*
* @param areaId 区域ID
* @return 影响记录数
*/
Integer delete(Integer areaId);
}
3.编写AreaMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.myimooc.small.mapper.AreaMapper">
<resultMap id="baseResultMap" type="com.myimooc.small.po.Area">
<id column="area_id" property="areaId" jdbcType="INTEGER" javaType="Integer"/>
<result column="area_name" property="areaName" jdbcType="VARCHAR" javaType="String"/>
<result column="priority" property="priority" jdbcType="VARCHAR" javaType="Integer"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" javaType="Date"/>
<result column="last_edit_time" property="lastEditTime" jdbcType="TIMESTAMP" javaType="Date"/>
</resultMap>
<sql id="baseColumnAll">
area_id,area_name,priority,create_time,last_edit_time
</sql>
<sql id="baseColumnInsert">
area_name,priority,create_time,last_edit_time
</sql>
<insert id="create" useGeneratedKeys="true" keyProperty="areaId" keyColumn="area_id"
parameterType="com.myimooc.small.po.Area">
insert into tb_area(<include refid="baseColumnInsert"/>)
values(#{areaName},#{priority},#{createTime},#{lastEditTime})
</insert>
<update id="update" parameterType="com.myimooc.small.po.Area">
update tb_area
<set>
<if test="areaName != null">area_name=#{areaName},</if>
<if test="priority != null">priority=#{priority},</if>
<if test="lastEditTime != null">last_edit_time=#{lastEditTime}</if>
</set>
where area_id = #{areaId}
</update>
<delete id="delete">
delete from tb_area where area_id = #{areaId}
</delete>
<select id="listAll" resultMap="baseResultMap">
select
<include refid="baseColumnAll"/>
from tb_area order by priority desc
</select>
<select id="findById" resultMap="baseResultMap">
select
<include refid="baseColumnAll"/>
from tb_area where area_id = #{areaId}
</select>
</mapper>
4.编写AreaMapperTest类
package com.myimooc.small.mapper;
import com.myimooc.small.SmallApplicationTests;
import com.myimooc.small.po.Area;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.*;
/**
* <br>
* 标题: 区域表 DAO 单元测试类<br>
* 描述: tb_area 表 DAO 单元测试类<br>
*
* @author zc
* @date 2018/04/17
*/
public class AreaMapperTest extends SmallApplicationTests{
@Autowired
private AreaMapper areaMapper;
@Test
@Ignore
public void listAll() {
Assert.assertNotNull(areaMapper);
List<Area> areas = areaMapper.listAll();
assertEquals(2,areas.size());
}
@Test
public void findById() {
Area area = areaMapper.findById(1);
assertEquals("东苑",area.getAreaName());
}
@Test
@Ignore
public void create() {
Area area = new Area();
area.setAreaName("南苑");
area.setPriority(1);
Integer num = areaMapper.create(area);
assertEquals(new Integer(1),num);
}
@Test
@Ignore
public void update() {
Area area = new Area();
area.setAreaName("西苑");
area.setAreaId(3);
area.setLastEditTime(new Date());
Integer num = areaMapper.update(area);
assertEquals(new Integer(1),num);
}
@Test
@Ignore
public void delete() {
Integer num = areaMapper.delete(3);
assertEquals(new Integer(1),num);
}
}
3-2 业务服务
代码编写
1.编写AreaService类
package com.myimooc.small.service;
import com.myimooc.small.po.Area;
import java.util.List;
/**
* <br>
* 标题: 区域业务服务<br>
* 描述: 区域业务服务<br>
*
* @author zc
* @date 2018/04/17
*/
public interface AreaService {
/**
* 批量查询
*
* @return 区域集合
*/
List<Area> listAll();
/**
* 详情查询
*
* @param areaId 区域ID
* @return 区域信息
*/
Area findById(Integer areaId);
/**
* 创建区域
*
* @param area 区域 PO
* @return 影响记录数
*/
Boolean create(Area area);
/**
* 修改区域
*
* @param area 区域 PO
* @return 影响记录数
*/
Boolean update(Area area);
/**
* 删除区域
*
* @param areaId 区域ID
* @return 影响记录数
*/
Boolean delete(Integer areaId);
}
2.编写AreaServiceImpl类
package com.myimooc.small.service.impl;
import com.myimooc.small.mapper.AreaMapper;
import com.myimooc.small.po.Area;
import com.myimooc.small.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
/**
* <br>
* 标题: 区域业务服务实现<br>
* 描述: 区域业务服务<br>
*
* @author zc
* @date 2018/04/17
*/
@Service
public class AreaServiceImpl implements AreaService {
@Autowired
private AreaMapper areaMapper;
@Override
public List<Area> listAll() {
return areaMapper.listAll();
}
@Override
public Area findById(Integer areaId) {
return areaMapper.findById(areaId);
}
@Transactional(rollbackFor = {Exception.class})
@Override
public Boolean create(Area area) {
if (area != null) {
return areaMapper.create(area) == 1;
}
throw new RuntimeException("区域信息不能为空!");
}
@Transactional(rollbackFor = {Exception.class})
@Override
public Boolean update(Area area) {
if (area != null) {
area.setLastEditTime(new Date());
return areaMapper.update(area) == 1;
}
throw new RuntimeException("区域信息不能为空!");
}
@Transactional(rollbackFor = {Exception.class})
@Override
public Boolean delete(Integer areaId) {
if (areaId > 0) {
return areaMapper.delete(areaId) == 1;
}
throw new RuntimeException("区域ID不能为空!");
}
}
3.编写BaseResponse类
package com.myimooc.small.vo;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* <br>
* 标题: Http请求响应基类<br>
* 描述: 响应请求是否处理成功<br>
*
* @author zc
* @date 2018/04/17
*/
public class BaseResponse implements Serializable {
private static final long serialVersionUID = -9091805407931758357L;
/**
* 状态:true 成功;false 失败;
*/
private Boolean success;
/**
* 错误消息
*/
private String errMsg;
/**
* 响应数据
*/
private Map<String,Object> data;
public void putData(String key,Object value){
this.initData();
this.data.put(key,value);
}
public Object getData(String key){
this.initData();
return this.data.get(key);
}
private void initData(){
if (data == null){
data = new HashMap<>(16);
}
}
public Map<String, Object> getData() {
return data;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
}
4.编写AreaController类
package com.myimooc.small.controller;
import com.myimooc.small.po.Area;
import com.myimooc.small.service.AreaService;
import com.myimooc.small.vo.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* <br>
* 标题: 区域控制器<br>
* 描述: 控制器类<br>
*
* @author zc
* @date 2018/04/17
*/
@RestController
@RequestMapping("/superadmin/area")
public class AreaController {
@Autowired
private AreaService areaService;
@GetMapping("/list")
public BaseResponse list(){
BaseResponse response = new BaseResponse();
response.setSuccess(true);
response.putData("areaList",this.areaService.listAll());
return response;
}
@GetMapping("/find")
public BaseResponse findById(Integer areaId){
BaseResponse response = new BaseResponse();
response.setSuccess(true);
response.putData("area",this.areaService.findById(areaId));
return response;
}
@PostMapping("/create")
public BaseResponse create(@RequestBody Area area){
BaseResponse response = new BaseResponse();
response.setSuccess(this.areaService.create(area));
return response;
}
@PostMapping("/update")
public BaseResponse update(@RequestBody Area area){
BaseResponse response = new BaseResponse();
response.setSuccess(this.areaService.update(area));
return response;
}
@GetMapping("/remove")
public BaseResponse remove(Integer areaId){
BaseResponse response = new BaseResponse();
response.setSuccess(this.areaService.delete(areaId));
return response;
}
}
5.编写GlobalExceptionHandler类
package com.myimooc.small.handler;
import com.myimooc.small.vo.BaseResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
/**
* <br>
* 标题: 全局异常处理类<br>
* 描述: 统一处理异常<br>
*
* @author zc
* @date 2018/04/17
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public BaseResponse exceptionHandler(HttpServletRequest request, Exception e){
e.printStackTrace();
BaseResponse response = new BaseResponse();
response.setSuccess(false);
response.setErrMsg(e.getMessage());
return response;
}
}
第四章:项目联调
4-1 小程序端
小程序开发中文档地址:https://developers.weixin.qq.com/miniprogram/dev/
由篇幅限制,小程序端源码请到github地址查看,完成后的结构如下
第五章:课程总结 5-1 课程总结课程总结
- 学会从零搭建后端的SpringBoot + Mybatis框架
- 微信小程序开发入门
- 实现后端的业务功能
- 实现本地微信小程序的前端开发
- 前端与后端的联调
点击查看更多内容
7人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦