本文主要讲解mall整合SpringBoot+MyBatis搭建基本骨架,以商品品牌为例实现基本的CRUD操作及通过PageHelper实现分页查询。
mysql数据库环境搭建
下载并安装mysql5.7版本,下载地址:https://dev.mysql.com/downloads/installer/
设置数据库帐号密码:root root
下载并安装客户端连接工具Navicat,下载地址:http://www.formysql.com/xiazai.html
创建数据库mall
导入mall的数据库脚本,脚本地址:https://github.com/macrozheng/mall-learning/blob/master/document/sql/mall.sql
项目使用框架介绍
SpringBoot
SpringBoot可以让你快速构建基于Spring的Web应用程序,内置多种Web容器(如Tomcat),通过启动入口程序的main函数即可运行。
PagerHelper
MyBatis分页插件,简单的几行代码就能实现分页,在与SpringBoot整合时,只要整合了PagerHelper就自动整合了MyBatis。
PageHelper.startPage(pageNum,pageSize);//之后进行查询操作将自动进行分页List
Druid
alibaba开源的数据库连接池,号称Java语言中最好的数据库连接池。
Mybatis generator
MyBatis的代码生成器,可以根据数据库生成model、mapper.xml、mapper接口和Example,通常情况下的单表查询不用再手写mapper。
项目搭建
使用IDEA初始化一个SpringBoot项目
添加项目依赖
在pom.xml中添加相关依赖。
修改SpringBoot配置文件
在application.yml中添加数据源配置和MyBatis的mapper.xml的路径配置。
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
mybatis:
mapper-locations:
- classpath:mapper/*.xml
- classpath*:com/**/mapper/*.xmlCopy to clipboardErrorCopied
项目结构说明
Mybatis generator 配置文件
配置数据库连接,Mybatis generator生成model、mapper接口及mapper.xml的路径。
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
运行Generator的main函数生成代码
packagecom.macro.mall.tiny.mbg;importorg.mybatis.generator.api.MyBatisGenerator;importorg.mybatis.generator.config.Configuration;importorg.mybatis.generator.config.xml.ConfigurationParser;importorg.mybatis.generator.internal.DefaultShellCallback;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.List;/**
* 用于生产MBG的代码
* Created by macro on 2018/4/26.
*/publicclassGenerator{publicstaticvoidmain(String[]args)throwsException{//MBG 执行过程中的警告信息List
添加MyBatis的Java配置
用于配置需要动态生成的mapper接口的路径
packagecom.macro.mall.tiny.config;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.context.annotation.Configuration;/**
* MyBatis配置类
* Created by macro on 2019/4/8.
*/@Configuration@MapperScan("com.macro.mall.tiny.mbg.mapper")publicclassMyBatisConfig{}Copy to clipboardErrorCopied
实现Controller中的接口
实现PmsBrand表中的添加、修改、删除及分页查询接口。
packagecom.macro.mall.tiny.controller;importcom.macro.mall.tiny.common.api.CommonPage;importcom.macro.mall.tiny.common.api.CommonResult;importcom.macro.mall.tiny.mbg.model.PmsBrand;importcom.macro.mall.tiny.service.PmsBrandService;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.validation.BindingResult;importorg.springframework.web.bind.annotation.*;importjava.util.List;/**
* 品牌管理Controller
* Created by macro on 2019/4/19.
*/@Controller@RequestMapping("/brand")publicclassPmsBrandController{@AutowiredprivatePmsBrandServicedemoService;privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(PmsBrandController.class);@RequestMapping(value="listAll",method=RequestMethod.GET)@ResponseBodypublicCommonResult>getBrandList(){returnCommonResult.success(demoService.listAllBrand());}@RequestMapping(value="/create",method=RequestMethod.POST)@ResponseBodypublicCommonResultcreateBrand(@RequestBodyPmsBrandpmsBrand){CommonResultcommonResult;intcount=demoService.createBrand(pmsBrand);if(count==1){commonResult=CommonResult.success(pmsBrand);LOGGER.debug("createBrand success:{}",pmsBrand);}else{commonResult=CommonResult.failed("操作失败");LOGGER.debug("createBrand failed:{}",pmsBrand);}returncommonResult;}@RequestMapping(value="/update/{id}",method=RequestMethod.POST)@ResponseBodypublicCommonResultupdateBrand(@PathVariable("id")Longid,@RequestBodyPmsBrandpmsBrandDto,BindingResultresult){CommonResultcommonResult;intcount=demoService.updateBrand(id,pmsBrandDto);if(count==1){commonResult=CommonResult.success(pmsBrandDto);LOGGER.debug("updateBrand success:{}",pmsBrandDto);}else{commonResult=CommonResult.failed("操作失败");LOGGER.debug("updateBrand failed:{}",pmsBrandDto);}returncommonResult;}@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)@ResponseBodypublicCommonResultdeleteBrand(@PathVariable("id")Longid){intcount=demoService.deleteBrand(id);if(count==1){LOGGER.debug("deleteBrand success :id={}",id);returnCommonResult.success(null);}else{LOGGER.debug("deleteBrand failed :id={}",id);returnCommonResult.failed("操作失败");}}@RequestMapping(value="/list",method=RequestMethod.GET)@ResponseBodypublicCommonResult
添加Service接口
packagecom.macro.mall.tiny.service;importcom.macro.mall.tiny.mbg.model.PmsBrand;importjava.util.List;/**
* PmsBrandService
* Created by macro on 2019/4/19.
*/publicinterfacePmsBrandService{List
实现Service接口
packagecom.macro.mall.tiny.service.impl;importcom.github.pagehelper.PageHelper;importcom.macro.mall.tiny.mbg.mapper.PmsBrandMapper;importcom.macro.mall.tiny.mbg.model.PmsBrand;importcom.macro.mall.tiny.mbg.model.PmsBrandExample;importcom.macro.mall.tiny.service.PmsBrandService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;/**
* PmsBrandService实现类
* Created by macro on 2019/4/19.
*/@ServicepublicclassPmsBrandServiceImplimplementsPmsBrandService{@AutowiredprivatePmsBrandMapperbrandMapper;@OverridepublicList