栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

慕课网《SpringBoot+MyBatis搭建迷你小程序》学习总结

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

慕课网《SpringBoot+MyBatis搭建迷你小程序》学习总结

慕课网《SpringBoot+MyBatis搭建迷你小程序》学习总结

  • 时间:2018年04月16日星期一
  • 说明:本文部分内容均来自慕课网。@慕课网:https://www.imooc.com
  • 教学源码:无
  • 学习源码:https://github.com/zccodere/study-imooc
第一章:课程介绍 1-1 课程介绍

学习收获

  • 学会从零搭建后端的SpringBoot + Mybatis框架
  • 微信小程序开发入门
  • 规范的代码编写
  • 实现后端的业务功能
  • 实现本地微信小程序的前端开发
  • 前端与后端的联调

技术要求

  • 基本的Java知识
  • 基本的前端开发知识Html、JS、CSS
  • 基本了解Spring、Mybatis知识
第二章:项目搭建 2-1 功能设计

功能列表

  • 新增区域
  • 删除区域
  • 修改区域
  • 查询区域
2-2 框架搭建

代码编写

1.创建名为small的maven工程pom如下



    4.0.0

    com.myimooc
    small
    0.0.1-SNAPSHOT
    jar

    small
    Demo project for Spring Boot

    
 org.springframework.boot
 spring-boot-starter-parent
 2.0.1.RELEASE
  
    

    
 UTF-8
 UTF-8
 1.8
    

    
 
     org.springframework.boot
     spring-boot-starter-web
 
 
     org.mybatis.spring.boot
     mybatis-spring-boot-starter
     1.3.2
 
 
     mysql
     mysql-connector-java
 
 
     com.mchange
     c3p0
     0.9.5.2
 
 
     org.springframework.boot
     spring-boot-starter-test
     test
 
    

    
 
     
  org.springframework.boot
  spring-boot-maven-plugin
     
     
  org.apache.maven.plugins
  maven-resources-plugin
  3.0.1
  
      UTF-8
  
     
 
 
     
  src/main/java
  
      ***.xml
  
      -->
 
    


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;


@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;


public class Area implements Serializable {

    private static final long serialVersionUID = -3084550177471749048L;
    
    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;


@Repository
public interface AreaMapper {
    
    List listAll();

    
    Area findById(Integer areaId);

    
    Integer create(Area area);

    
    Integer update(Area area);

    
    Integer delete(Integer areaId);
}

3.编写AreaMapper.xml





    
 

 
 
 
 
    

    
 area_id,area_name,priority,create_time,last_edit_time
    

    
 area_name,priority,create_time,last_edit_time
    

    
 insert into tb_area()
 values(#{areaName},#{priority},#{createTime},#{lastEditTime})
    

    
 update tb_area
 
     area_name=#{areaName},
     priority=#{priority},
     last_edit_time=#{lastEditTime}
 
 where area_id = #{areaId}
    

    
 delete from tb_area where area_id = #{areaId}
    

    
 select
 
 from tb_area where area_id = #{areaId}
    

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.*;


public class AreaMapperTest extends SmallApplicationTests{

    @Autowired
    private AreaMapper areaMapper;

    @Test
    @Ignore
    public void listAll() {
 Assert.assertNotNull(areaMapper);
 List 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;


public interface AreaService {

    
    List listAll();

    
    Area findById(Integer areaId);

    
    Boolean create(Area area);

    
    Boolean update(Area area);

    
    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;


@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List 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;


public class baseResponse implements Serializable {

    private static final long serialVersionUID = -9091805407931758357L;
    
    private Boolean success;
    
    private String errMsg;
    
    private Map 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 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;


@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;


@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框架
  • 微信小程序开发入门
  • 实现后端的业务功能
  • 实现本地微信小程序的前端开发
  • 前端与后端的联调
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235766.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号