亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2019年08月28日 10:21:28   作者:macrozheng  
這篇文章主要介紹了詳解mall整合SpringBoot+MyBatis搭建基本骨架,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
SpringBoot實(shí)戰(zhàn)電商項(xiàng)目mall(20k+star)地址:https://github.com/macrozheng/mall

摘要

本文主要講解mall整合SpringBoot+MyBatis搭建基本骨架,以商品品牌為例實(shí)現(xiàn)基本的CRUD操作及通過(guò)PageHelper實(shí)現(xiàn)分頁(yè)查詢。

mysql數(shù)據(jù)庫(kù)環(huán)境搭建

下載并安裝mysql5.7版本,下載地址:https://dev.mysql.com/downloads/installer/

設(shè)置數(shù)據(jù)庫(kù)帳號(hào)密碼:root root

下載并安裝客戶端連接工具Navicat,下載地址:http://www.formysql.com/xiazai.html

創(chuàng)建數(shù)據(jù)庫(kù)mall導(dǎo)入

mall的數(shù)據(jù)庫(kù)腳本,腳本地址:https://github.com/macrozheng/mall-learning/blob/master/document/sql/mall.sql

項(xiàng)目使用框架介紹

SpringBoot

SpringBoot可以讓你快速構(gòu)建基于Spring的Web應(yīng)用程序,內(nèi)置多種Web容器(如Tomcat),通過(guò)啟動(dòng)入口程序的main函數(shù)即可運(yùn)行。

PagerHelper

MyBatis分頁(yè)插件,簡(jiǎn)單的幾行代碼就能實(shí)現(xiàn)分頁(yè),在與SpringBoot整合時(shí),只要整合了PagerHelper就自動(dòng)整合了MyBatis。
PageHelper.startPage(pageNum, pageSize);
//之后進(jìn)行查詢操作將自動(dòng)進(jìn)行分頁(yè)
List<PmsBrand> brandList = brandMapper.selectByExample(new PmsBrandExample());
//通過(guò)構(gòu)造PageInfo對(duì)象獲取分頁(yè)信息,如當(dāng)前頁(yè)碼,總頁(yè)數(shù),總條數(shù)
PageInfo<PmsBrand> pageInfo = new PageInfo<PmsBrand>(list);

Druid

alibaba開(kāi)源的數(shù)據(jù)庫(kù)連接池,號(hào)稱Java語(yǔ)言中最好的數(shù)據(jù)庫(kù)連接池。

Mybatis generator

MyBatis的代碼生成器,可以根據(jù)數(shù)據(jù)庫(kù)生成model、mapper.xml、mapper接口和Example,通常情況下的單表查詢不用再手寫(xiě)mapper。

項(xiàng)目搭建

使用IDEA初始化一個(gè)SpringBoot項(xiàng)目

添加項(xiàng)目依賴

在pom.xml中添加相關(guān)依賴。
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <dependencies>
    <!--SpringBoot通用依賴模塊-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!--MyBatis分頁(yè)插件-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.2.10</version>
    </dependency>
    <!--集成druid連接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.10</version>
    </dependency>
    <!-- MyBatis 生成器 -->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.3</version>
    </dependency>
    <!--Mysql數(shù)據(jù)庫(kù)驅(qū)動(dòng)-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.15</version>
    </dependency>
  </dependencies>

修改SpringBoot配置文件

在application.yml中添加數(shù)據(jù)源配置和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

項(xiàng)目結(jié)構(gòu)說(shuō)明

Mybatis generator 配置文件

配置數(shù)據(jù)庫(kù)連接,Mybatis generator生成model、mapper接口及mapper.xml的路徑。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <properties resource="generator.properties"/>
  <context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
    <property name="javaFileEncoding" value="UTF-8"/>
    <!-- 為模型生成序列化方法-->
    <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
    <!-- 為生成的Java模型創(chuàng)建一個(gè)toString方法 -->
    <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
    <!--可以自定義生成model的代碼注釋-->
    <commentGenerator type="com.macro.mall.tiny.mbg.CommentGenerator">
      <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
      <property name="suppressAllComments" value="true"/>
      <property name="suppressDate" value="true"/>
      <property name="addRemarkComments" value="true"/>
    </commentGenerator>
    <!--配置數(shù)據(jù)庫(kù)連接-->
    <jdbcConnection driverClass="${jdbc.driverClass}"
            connectionURL="${jdbc.connectionURL}"
            userId="${jdbc.userId}"
            password="${jdbc.password}">
      <!--解決mysql驅(qū)動(dòng)升級(jí)到8.0后不生成指定數(shù)據(jù)庫(kù)代碼的問(wèn)題-->
      <property name="nullCatalogMeansCurrent" value="true" />
    </jdbcConnection>
    <!--指定生成model的路徑-->
    <javaModelGenerator targetPackage="com.macro.mall.tiny.mbg.model" targetProject="mall-tiny-01\src\main\java"/>
    <!--指定生成mapper.xml的路徑-->
    <sqlMapGenerator targetPackage="com.macro.mall.tiny.mbg.mapper" targetProject="mall-tiny-01\src\main\resources"/>
    <!--指定生成mapper接口的的路徑-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.macro.mall.tiny.mbg.mapper"
               targetProject="mall-tiny-01\src\main\java"/>
    <!--生成全部表tableName設(shè)為%-->
    <table tableName="pms_brand">
      <generatedKey column="id" sqlStatement="MySql" identity="true"/>
    </table>
  </context>
</generatorConfiguration>

運(yùn)行Generator的main函數(shù)生成代碼

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;

/**
 * 用于生產(chǎn)MBG的代碼
 * Created by macro on 2018/4/26.
 */
public class Generator {
  public static void main(String[] args) throws Exception {
    //MBG 執(zhí)行過(guò)程中的警告信息
    List<String> warnings = new ArrayList<String>();
    //當(dāng)生成的代碼重復(fù)時(shí),覆蓋原代碼
    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);
    //創(chuàng)建 MBG
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    //執(zhí)行生成代碼
    myBatisGenerator.generate(null);
    //輸出警告信息
    for (String warning : warnings) {
      System.out.println(warning);
    }
  }
}

添加MyBatis的Java配置

用于配置需要?jiǎng)討B(tài)生成的mapper接口的路徑
package com.macro.mall.tiny.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatis配置類(lèi)
 * Created by macro on 2019/4/8.
 */
@Configuration
@MapperScan("com.macro.mall.tiny.mbg.mapper")
public class MyBatisConfig {
}

實(shí)現(xiàn)Controller中的接口

實(shí)現(xiàn)PmsBrand表中的添加、修改、刪除及分頁(yè)查詢接口。
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<List<PmsBrand>> 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<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
    List<PmsBrand> brandList = demoService.listBrand(pageNum, pageSize);
    return CommonResult.success(CommonPage.restPage(brandList));
  }

  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  @ResponseBody
  public CommonResult<PmsBrand> 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<PmsBrand> listAllBrand();

  int createBrand(PmsBrand brand);

  int updateBrand(Long id, PmsBrand brand);

  int deleteBrand(Long id);

  List<PmsBrand> listBrand(int pageNum, int pageSize);

  PmsBrand getBrand(Long id);
}

實(shí)現(xiàn)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實(shí)現(xiàn)類(lèi)
 * Created by macro on 2019/4/19.
 */
@Service
public class PmsBrandServiceImpl implements PmsBrandService {
  @Autowired
  private PmsBrandMapper brandMapper;

  @Override
  public List<PmsBrand> 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<PmsBrand> listBrand(int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    return brandMapper.selectByExample(new PmsBrandExample());
  }

  @Override
  public PmsBrand getBrand(Long id) {
    return brandMapper.selectByPrimaryKey(id);
  }
}

項(xiàng)目源碼地址

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Eclipse 字體、字號(hào)的設(shè)置、最佳字體推薦

    詳解Eclipse 字體、字號(hào)的設(shè)置、最佳字體推薦

    這篇文章主要介紹了Eclipse 字體、字號(hào)的設(shè)置、最佳字體推薦,需要的朋友可以參考下
    2020-09-09
  • JavaWeb Refresh響應(yīng)頭代碼實(shí)例詳解

    JavaWeb Refresh響應(yīng)頭代碼實(shí)例詳解

    這篇文章主要介紹了JavaWeb Refresh響應(yīng)頭代碼實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • RedisTemplate中opsForValue和opsForList方法的使用詳解

    RedisTemplate中opsForValue和opsForList方法的使用詳解

    這篇文章主要介紹了RedisTemplate中opsForValue和opsForList方法的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • IntelliJ IDEA之高效代碼插件RainBow Brackets詳解

    IntelliJ IDEA之高效代碼插件RainBow Brackets詳解

    這篇文章主要介紹了IntelliJ IDEA之高效代碼插件RainBow Brackets詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 淺談java Collection中的排序問(wèn)題

    淺談java Collection中的排序問(wèn)題

    下面小編就為大家?guī)?lái)一篇淺談java Collection中的排序問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • java中URLencode、URLdecode及Base64加解密轉(zhuǎn)換

    java中URLencode、URLdecode及Base64加解密轉(zhuǎn)換

    本文主要介紹了java中URLencode、URLdecode及Base64加解密轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • Idea2020 無(wú)法share項(xiàng)目到svn的解決方法

    Idea2020 無(wú)法share項(xiàng)目到svn的解決方法

    這篇文章主要介紹了Idea2020 無(wú)法share項(xiàng)目到svn的解決方法,需要的朋友可以參考下
    2020-09-09
  • 詳解Maven POM(項(xiàng)目對(duì)象模型)

    詳解Maven POM(項(xiàng)目對(duì)象模型)

    這篇文章主要介紹了Maven POM(項(xiàng)目對(duì)象模型)的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Java IO之字節(jié)輸入輸出流詳解

    Java IO之字節(jié)輸入輸出流詳解

    這篇文章主要為大家介紹了Java IO之字節(jié)輸入輸出流,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • Java中Cookie和Session詳解及區(qū)別總結(jié)

    Java中Cookie和Session詳解及區(qū)別總結(jié)

    這篇文章主要介紹了Java中Cookie和Session詳解,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-06-06

最新評(píng)論