最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springBoot+mybaties后端多層架構(gòu)的實(shí)現(xiàn)示例

 更新時(shí)間:2022年07月06日 08:52:22   作者:我要用代碼向我喜歡的女孩表白  
本文主要介紹了springBoot+mybaties后端多層架構(gòu)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前期準(zhǔn)備

1.建立一個(gè)springBoot項(xiàng)目

2.導(dǎo)入相關(guān)的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>etf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>etf</name>
    <description>etf</description>
 
        <properties>
            <java.version>1.8</java.version>
            <mybatis-plus.version>3.4.2</mybatis-plus.version>
        </properties>
 
 
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

建立相關(guān)目錄

說(shuō)明:

各層名稱說(shuō)明補(bǔ)充
controller與前端交互的接口部分 
service層業(yè)務(wù)邏輯主要寫的地方 
mapper層mapper接口,對(duì)應(yīng)mybatis的sql.xml 
mapper.xml層在resource中,mybaites自動(dòng)將數(shù)據(jù)返回給mapper層 
dao與數(shù)據(jù)庫(kù)1v1對(duì)應(yīng)的類,駝峰-》對(duì)應(yīng)數(shù)據(jù)庫(kù)的下劃線 

resource中的目錄

idea下載安裝插件

lombok

mybaits-tool(任意工具都可以plugin里很多,主要用于xml與mapper跳轉(zhuǎn))

配置mybais-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
 
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&amp;autoReconnect=true&amp;allowMultiQueries=true"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/EtfHotMapper.xml"/>
        <mapper resource="mapper/EtfTeachMapper.xml"/>
        <mapper resource="mapper/TestEditorMapper.xml" />
        <mapper resource="mapper/TestClientMapper.xml" />
    </mappers>
 
</configuration>

dao層案例:

package com.example.etf.story.dao;
 
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
 
import java.util.Date;
 
@Data
public class TestStory {
 
    private int storyId;
    private String initInfo;
    private String storyName;
    private String storyType;
    private int status;
    private String creater;
    private String auditor;
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private Date updateTime;
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private Date createTime;
}

數(shù)據(jù)庫(kù)建表案例

附送他的建表語(yǔ)句:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
 
-- ----------------------------
-- Table structure for test_story
-- ----------------------------
DROP TABLE IF EXISTS `test_story`;
CREATE TABLE `test_story`  (
  `story_id` int(0) NOT NULL AUTO_INCREMENT COMMENT '第一層id',
  `init_info` json NOT NULL COMMENT '初始信息',
  `story_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '名稱',
  `story_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '屬于種類,學(xué)習(xí),游戲',
  `status` int(0) NULL DEFAULT NULL COMMENT '狀態(tài),0未發(fā)布,1未審核,2不通過(guò),3審核通過(guò),4失效',
  `creator` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '作者',
  `auditor` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '審核人',
  `update_time` timestamp(0) NULL DEFAULT NULL COMMENT '更新時(shí)間',
  `create_time` timestamp(0) NULL DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',
  PRIMARY KEY (`story_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Dynamic;
 
SET FOREIGN_KEY_CHECKS = 1;

Paramr案例

(前端字段交互部分)

例:入口(查詢條件)

package com.example.etf.story.paramer;
 
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
 
import java.util.ArrayList;
import java.util.List;
 
@Data
public class StorySelectParam {
        private int pageNum=1;
        private int pageSize=8;
        @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
        private List<String> updateTime;
        @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
        private List<String> createTime;
        private String creator;
        private String storyName;
        private String storyType;
 
        private int sqlStart;//limit的開(kāi)始
        private int sqlEnd;//limit的結(jié)束
 
 
}

返回前端內(nèi)容(頁(yè)碼以及本次總條數(shù),用于分頁(yè))

package com.example.etf.story.paramer;
 
import com.example.etf.story.dao.TestStory;
import lombok.Data;
 
import java.util.List;
 
@Data
public class StoriesParam {
    private List<TestStory> testStories;
    private int total;
    private int pageNum;
    private int  pageSize;
}

Controller層案例

package com.example.etf.story.controller;
 
 
import com.example.etf.story.dao.TestStory;
import com.example.etf.story.paramer.StoriesParam;
import com.example.etf.story.paramer.StorySelectParam;
import com.example.etf.story.service.TestClientService;
import com.example.etf.story.service.TestEditorService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.List;
 
@Controller
@RestController
public class TestClientController {
 
    @Resource
    private TestClientService clientService;
 
    //獲取所有已發(fā)布story
    @PostMapping("/selectStorysByPublic")
    @ResponseBody
    public StoriesParam selectStorysByPublic(@RequestBody StorySelectParam storySelectParam){
        return clientService.selectStorysByPublic(storySelectParam);
    }
 
}

Service層案例

package com.example.etf.story.service;
 
import com.example.etf.controller.SqlSessionF;
import com.example.etf.story.dao.TestStory;
import com.example.etf.story.mapper.TestClientMapper;
 
import com.example.etf.story.paramer.StoriesParam;
import com.example.etf.story.paramer.StorySelectParam;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service
public class TestClientService {
    public StoriesParam selectStorysByPublic(StorySelectParam storySelectParam){
        TestClientMapper mapper = new SqlSessionF().getSqlSession().getMapper(TestClientMapper.class);
        int pageNum = storySelectParam.getPageNum();
        int pageSize = storySelectParam.getPageSize();
        if(pageNum==0 || pageSize==0){
            storySelectParam.setSqlStart(0);
            storySelectParam.setSqlEnd(8);
        }else {
            storySelectParam.setSqlStart((pageNum - 1) * pageSize);
            storySelectParam.setSqlEnd(pageNum * pageSize);
        }
        System.out.println(storySelectParam);
 
        List<TestStory> testStories = mapper.selectStorysByPublic(storySelectParam);
        StoriesParam storiesParam = new StoriesParam();
        storiesParam.setTestStories(testStories);
        storiesParam.setPageNum(pageNum);
        storiesParam.setPageSize(pageSize);
 
        storiesParam.setTotal(mapper.getTotal(storySelectParam));
 
        return storiesParam;
 
    }
}

Mapper層案例

package com.example.etf.story.mapper;
 
import com.example.etf.story.dao.TestStory;
import com.example.etf.story.dao.TestStoryStage;
import com.example.etf.story.paramer.StoriesParam;
import com.example.etf.story.paramer.StorySelectParam;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
@Mapper
@Component
public interface TestClientMapper {
    //分頁(yè)查詢所有故事
 
    List<TestStory> selectStorysByPublic(StorySelectParam storySelectParam);
    int getTotal(StorySelectParam storySelectParam);//總數(shù)用于分頁(yè)
}

SQL.xml案例

(resource下的mapper)

TestClientMapper.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.example.etf.story.mapper.TestClientMapper">
 
<select id="selectStorysByPublic" resultType="com.example.etf.story.dao.TestStory">
    select * from
 
    test_story
 
    where
    <if test=" storyName==null or storyName=='' ">
        story_name !=""
    </if>
 
 
    <if test="storyName !='' and storyName !=null ">
        story_name like concat('%',#{storyName},'%')
    </if>
 
    <if test="creator !='' and creator !=null ">
        and creator like concat('%',#{creator},'%')
    </if>
 
    <if test="storyType !='' and storyType !=null ">
        and story_type like concat('%',#{storyType},'%')
    </if>
 
    <if test="updateTime.size()>0 and updateTime !=null">
        <![CDATA[ and DATE_FORMAT(update_time, '%Y-%m-%d') >= #{updateTime[0]} ]]>
        <![CDATA[ and DATE_FORMAT(update_time, '%Y-%m-%d') <= #{updateTime[1]} ]]>
    </if>
    <if test="createTime.size()>0 and createTime !=null">
        <![CDATA[ and DATE_FORMAT(create_time, '%Y-%m-%d') >= #{createTime[0]} ]]>
        <![CDATA[ and DATE_FORMAT(create_time, '%Y-%m-%d') <= #{createTime[1]} ]]>
    </if>
    limit ${sqlStart},${sqlEnd}
</select>
 
<select id="getTotal" resultType="int">
    select count(1) from
 
    test_story
 
    where
    <if test=" storyName==null or storyName=='' ">
        story_name !=""
    </if>
 
 
    <if test="storyName !='' and storyName !=null ">
        story_name like concat('%',#{storyName},'%')
    </if>
 
    <if test="creator !='' and creator !=null ">
        and creator like concat('%',#{creator},'%')
    </if>
 
    <if test="storyType !='' and storyType !=null ">
        and story_type like concat('%',#{storyType},'%')
    </if>
 
    <if test="updateTime.size()>0 and updateTime !=null">
        <![CDATA[ and DATE_FORMAT(update_time, '%Y-%m-%d') >= #{updateTime[0]} ]]>
        <![CDATA[ and DATE_FORMAT(update_time, '%Y-%m-%d') <= #{updateTime[1]} ]]>
    </if>
    <if test="createTime.size()>0 and createTime !=null">
        <![CDATA[ and DATE_FORMAT(create_time, '%Y-%m-%d') >= #{createTime[0]} ]]>
        <![CDATA[ and DATE_FORMAT(create_time, '%Y-%m-%d') <= #{createTime[1]} ]]>
    </if>
 
</select>
</mapper>

同志們辛苦了,新手都會(huì)覺(jué)得為什么要用這個(gè)?這么復(fù)雜。一個(gè)類不好嗎?

對(duì)于老手都能理解(當(dāng)你做項(xiàng)目后,就知道,一個(gè)項(xiàng)目的簡(jiǎn)單模塊,就有幾十個(gè)接口。輕松維護(hù),多人協(xié)同,第一次搭建稍微麻煩點(diǎn),后續(xù)開(kāi)發(fā)異常舒服)

 到此這篇關(guān)于springBoot+mybaties后端多層架構(gòu)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)springBoot mybaties多層架構(gòu)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 正則表達(dá)式詳細(xì)使用

    Java 正則表達(dá)式詳細(xì)使用

    這篇文章主要介紹了Java 正則表達(dá)式詳細(xì)使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-10-10
  • java實(shí)現(xiàn)通訊錄管理系統(tǒng)

    java實(shí)現(xiàn)通訊錄管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • springboot集成swagger3與knife4j的詳細(xì)代碼

    springboot集成swagger3與knife4j的詳細(xì)代碼

    這篇文章主要介紹了springboot集成swagger3與knife4j,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • 利用Postman和Chrome的開(kāi)發(fā)者功能探究項(xiàng)目(畢業(yè)設(shè)計(jì)項(xiàng)目)

    利用Postman和Chrome的開(kāi)發(fā)者功能探究項(xiàng)目(畢業(yè)設(shè)計(jì)項(xiàng)目)

    這篇文章主要介紹了利用Postman和Chrome的開(kāi)發(fā)者功能探究項(xiàng)目(畢業(yè)設(shè)計(jì)項(xiàng)目),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java基于Javafaker生成測(cè)試數(shù)據(jù)

    Java基于Javafaker生成測(cè)試數(shù)據(jù)

    這篇文章主要介紹了Java基于Javafaker生成測(cè)試數(shù)據(jù)的方法,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • SpringBoot?如何使用sharding?jdbc進(jìn)行分庫(kù)分表

    SpringBoot?如何使用sharding?jdbc進(jìn)行分庫(kù)分表

    這篇文章主要介紹了SpringBoot?如何使用sharding?jdbc進(jìn)行分庫(kù)分表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Mybatis多線程下如何使用Example詳解

    Mybatis多線程下如何使用Example詳解

    這篇文章主要給大家介紹了關(guān)于Mybatis多線程下如何使用Example的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 如何開(kāi)發(fā)基于Netty的HTTP/HTTPS應(yīng)用程序

    如何開(kāi)發(fā)基于Netty的HTTP/HTTPS應(yīng)用程序

    HTTP/HTTPS是最常見(jiàn)的協(xié)議套件之一,并且隨著智能手機(jī)的成功,它的應(yīng)用也日益廣泛,因?yàn)閷?duì)于任何公司來(lái)說(shuō),擁有一個(gè)可以被移動(dòng)設(shè)備訪問(wèn)的網(wǎng)站幾乎是必須的。下面就來(lái)看看如何開(kāi)發(fā)基于Netty的HTTP/HTTPS應(yīng)用程序
    2021-06-06
  • Java中的Spring Security配置過(guò)濾器

    Java中的Spring Security配置過(guò)濾器

    這篇文章主要介紹了Java中的Spring Security配置過(guò)濾器,文章通過(guò)圍繞文章主題的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • Java通過(guò)HttpClient進(jìn)行HTTP請(qǐng)求的代碼詳解

    Java通過(guò)HttpClient進(jìn)行HTTP請(qǐng)求的代碼詳解

    Apache?HttpClient是一個(gè)功能強(qiáng)大且廣泛使用的Java庫(kù),它提供了方便的方法來(lái)執(zhí)行HTTP請(qǐng)求并處理響應(yīng)。本文將介紹如何使用HttpClient庫(kù)進(jìn)行HTTP請(qǐng)求,包括GET請(qǐng)求、POST請(qǐng)求、添加參數(shù)和請(qǐng)求體、設(shè)置請(qǐng)求頭等操作,需要的朋友可以參考下
    2023-05-05

最新評(píng)論

绥芬河市| 库尔勒市| 石阡县| 门头沟区| 廉江市| 岢岚县| 临城县| 瓮安县| 固安县| 简阳市| 文水县| 彭阳县| 平陆县| 临西县| 丘北县| 玛纳斯县| 肇东市| 自治县| 大英县| 青海省| 施秉县| 金乡县| 克山县| 绍兴县| 湛江市| 客服| 阳谷县| 金寨县| 桐柏县| 辽源市| 宁化县| 焦作市| 许昌市| 称多县| 拜城县| 太仆寺旗| 安庆市| 辽宁省| 新和县| 忻州市| 林周县|