<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
1.建立一個springBoot專案
2.匯入相關的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>
建立相關目錄
說明:
各層名稱 | 說明 | 補充 |
controller | 與前端互動的介面部分 | |
service層 | 業務邏輯主要寫的地方 | |
mapper層 | mapper介面,對應mybatis的sql.xml | |
mapper.xml層 | 在resource中,mybaites自動將資料返回給mapper層 | |
dao | 與資料庫1v1對應的類,駝峰-》對應資料庫的下劃線 |
resource中的目錄
idea下載安裝外掛
lombok
mybaits-tool(任意工具都可以plugin裡很多,主要用於xml與mapper跳轉)
<?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&autoReconnect=true&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; }
附送他的建表語句:
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 '屬於種類,學習,遊戲', `status` int(0) NULL DEFAULT NULL COMMENT '狀態,0未釋出,1未稽核,2不通過,3稽核通過,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 '更新時間', `create_time` timestamp(0) NULL DEFAULT NULL COMMENT '建立時間', 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;
(前端欄位互動部分)
例:入口(查詢條件)
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的開始 private int sqlEnd;//limit的結束 }
返回前端內容(頁碼以及本次總條數,用於分頁)
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; //獲取所有已釋出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 { //分頁查詢所有故事 List<TestStory> selectStorysByPublic(StorySelectParam storySelectParam); int getTotal(StorySelectParam storySelectParam);//總數用於分頁 }
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>
同志們辛苦了,新手都會覺得為什麼要用這個?這麼複雜。一個類不好嗎?
對於老手都能理解(當你做專案後,就知道,一個專案的簡單模組,就有幾十個介面。輕鬆維護,多人協同,第一次搭建稍微麻煩點,後續開發異常舒服)
到此這篇關於springBoot+mybaties後端多層架構的實現範例的文章就介紹到這了,更多相關springBoot mybaties多層架構內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45