详解mall整合SpringBoot+MyBatis搭建基本骨架

网友投稿 674 2023-07-25

详解mall整合SpringBoot+MyBatis搭建基本骨架

详解mall整合SpringBoot+MyBatis搭建基本骨架

SpringBoot实战电商项目mall(20k+star)地址:https://github.com/macrozheng/mall

摘要

本文主要讲解mall整合SpringBoot+MyBatis搭建基本骨架,以商品品牌为例实现基本的CRUD操作及通过PageHelper实现分页查询。

mysql数据库环境搭建

-并安装mysql5.7版本,-:https://dev.mysql.com/downloads/installer/

设置数据库帐号密码:root root

-并安装客户端连接工具Navicat,-:http://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 brandList = brandMapper.selectByExample(new PmsBrandExample());

//通过构造PageInfo对象获取分页信息,如当前页码,总页数,总条数

PageInfo pageInfo = new PageInfo(list);

Druid

alibaba开源的数据库连接池,号称java语言中最好的数据库连接池。

Mybatis generator

MyBatis的代码生成器,可以根据数据库生成model、mapper.xml、mapper接口和Example,通常情况下的单表查询不用再手写mapper。

项目搭建

使用IDEA初始化一个SpringBoot项目

添加项目依赖

在pom.xml中添加相关依赖。

org.springframework.boot

spring-boot-starter-parent

2.1.3.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-actuator

org.springframework.boot

spring-boot-starter-aop

org.springframework.boot

spring-boot-starter-test

test

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.10

com.alibaba

druid-spring-boot-starter

1.1.10

org.mybatis.generator

mybatis-generator-core

1.3.3

mysql

mysql-connector-java

8.0.15

修改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/*.xml

项目结构说明

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">

connectionURL="${jdbc.connectionURL}"

userId="${jdbc.userId}"

password="${jdbc.password}">

connectionURL="${jdbc.connectionURL}"

userId="${jdbc.userId}"

password="${jdbc.password}">

targetProject="mall-tiny-01\src\main\java"/>

targetProject="mall-tiny-01\src\main\java"/>

运行Generator的main函数生成代码

package com.macro.mall.tiny.mbg;

import org.mybatis.generator.api.MyBatisGenerator;

import org.mybatis.generator.config.Configuration;

import org.mybatis.generator.config.xml.ConfigurationParser;

import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

/**

* 用于生产MBG的代码

* Created by macro on 2018/4/26.

*/

public class Generator {

public static void main(String[] args) throws Exception {

//MBG 执行过程中的警告信息

List warnings = new ArrayList();

//当生成的代码重复时,覆盖原代码

boolean overwrite = true;

//读取我们的 MBG 配置文件

InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml");

ConfigurationParser cp = new ConfigurationParser(warnings);

Configuration config = cp.parseConfiguration(is);

is.close();

DefaultShellCallback callback = new DefaultShellCallback(overwrite);

//创建 MBG

MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

//执行生成代码

myBatisGenerator.generate(null);

//输出警告信息

for (String warning : warnings) {

System.out.println(warning);

}

}

}

添加MyBatis的Java配置

用于配置需要动态生成的mapper接口的路径

package com.macro.mall.tiny.config;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.context.annotation.Configuration;

/**

* MyBatis配置类

* Created by macro on 2019/4/8.

*/

@Configuration

@MapperScan("com.macro.mall.tiny.mbg.mapper")

public class MyBatisConfig {

}

实现Controller中的接口

实现PmsBrand表中的添加、修改、删除及分页查询接口。

package com.macro.mall.tiny.controller;

import com.macro.mall.tiny.common.api.CommonPage;

import com.macro.mall.tiny.common.api.CommonResult;

import com.macro.mall.tiny.mbg.model.PmsBrand;

import com.macro.mall.tiny.service.PmsBrandService;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

* 品牌管理Controller

* Created by macro on 2019/4/19.

*/

@Controller

@RequestMapping("/brand")

public class PmsBrandController {

@Autowired

private PmsBrandService demoService;

private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);

@RequestMapping(value = "listAll", method = RequestMethod.GET)

@ResponseBody

public CommonResult> getBrandList() {

return CommonResult.success(demoService.listAllBrand());

}

@RequestMapping(value = "/create", method = RequestMethod.POST)

@ResponseBody

public CommonResult createBrand(@RequestBody PmsBrand pmsBrand) {

CommonResult commonResult;

int count = demoService.createBrand(pmsBrand);

if (count == 1) {

commonResult = CommonResult.success(pmsBrand);

LOGGER.debug("createBrand success:{}", pmsBrand);

} else {

commonResult = CommonResult.failed("操作失败");

LOGGER.debug("createBrand failed:{}", pmsBrand);

}

return commonResult;

}

@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)

@ResponseBody

public CommonResult updateBrand(@PathVariable("id") Long id, @RequestBody PmsBrand pmsBrandDto, BindingResult result) {

CommonResult commonResult;

int count = 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);

}

return commonResult;

}

@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)

@ResponseBody

public CommonResult deleteBrand(@PathVariable("id") Long id) {

int count = demoService.deleteBrand(id);

if (count == 1) {

LOGGER.debug("deleteBrand success :id={}", id);

return CommonResult.success(null);

} else {

LOGGER.debug("deleteBrand failed :id={}", id);

return CommonResult.failed("操作失败");

}

}

@RequestMapping(value = "/list", method = RequestMethod.GET)

@ResponseBody

public CommonResult> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,

@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {

List brandList = demoService.listBrand(pageNum, pageSize);

return CommonResult.success(CommonPage.restPage(brandList));

}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)

@ResponseBody

public CommonResult brand(@PathVariable("id") Long id) {

return CommonResult.success(demoService.getBrand(id));

}

}

添加Service接口

package com.macro.mall.tiny.service;

import com.macro.mall.tiny.mbg.model.PmsBrand;

import java.util.List;

/**

* PmsBrandService

* Created by macro on 2019/4/19.

*/

public interface PmsBrandService {

List listAlhttp://lBrand();

int createBrand(PmsBrand brand);

int updateBrand(Long id, PmsBrand brand);

int deleteBrand(Long id);

List listBrand(int pageNum, int pageSize);

PmsBrand getBrand(Long id);

}

实现Service接口

package com.macro.mall.tiny.service.impl;

import com.github.pagehelper.PageHelper;

import com.macro.mall.tiny.mbg.mapper.PmsBrandMapper;

import com.macro.mall.tiny.mbg.model.PmsBrand;

import com.macro.mall.tiny.mbg.model.PmsBrandExample;

import com.macro.mall.tiny.service.PmsBrandService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

/**

* PmsBrandService实现类

* Created by macro on 2019/4/19.

*/

@Service

public class PmsBrandServiceImpl implements PmsBrandService {

@Autowired

private PmsBrandMapper brandMapper;

@Override

public List listAllBrand() {

return brandMapper.selectByExample(new PmsBrandExample());

}

@Override

public int createBrand(PmsBrand brand) {

return brandMapper.insertSelective(brand);

}

@Override

public int updateBrand(Long id, PmsBrand brand) {

brand.setId(id);

return brandMapper.updateByPrimaryKeySelective(brand);

}

@Override

public int deleteBrand(Long id) {

return brandMapper.deleteByPrimaryKey(id);

}

@Override

public List listBrand(int pageNum, int pageSize) {

PageHelper.startPage(pageNum, pageSize);

return brandMapper.selectByExample(new PmsBrandExample());

}

@Override

public PmsBrand getBrand(Long id) {

return brandMapper.selectByPrimaryKey(id);

}

}

项目源码地址

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-01

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:项目敏捷开发工具:提高效率和团队协作的利器
下一篇:SpringMVC上传图片代码实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~