springboot多模块项目架构,

  springboot多模块项目架构,

  

目录

前期准备配置mybais-config.xml数据库建表案例Paramr案例

 

  

前期准备

1.建立一个跳羚项目

 

  2.导入相关的砰的一声依赖

  ?可扩展标记语言版本=1.0 编码=UTF八号?项目xmlns= http://maven。阿帕奇。org/POM/4。0 .0 xmlns : xsi= http://www。w3。org/2001/XML schema-instance xsi :架构位置= http://maven。阿帕奇。org/POM/4。0 .0 https://maven.apache.org/xsd/maven-4.0.0.xsd模型版本4 .0 .0/模型版本父groupIdorg.springframework.boot/groupId artifactId spring-boot-starter-parent/artifactId版本2.7-从存储库查找父级-/父级groupIdcom.example/groupId artifactIdetf/artifactId版本0 .0 .1-快照/版本名称ETF/名称描述ETF/描述属性Java。版本1.8/Java。mybatis-plus版本。版本3。4 .2/mybatis-plus。版本/属性依赖项依赖项groupIdcom.alibaba/groupId artifactIdfastjson/artifactId版本1 .2 .75/版本/依赖项依赖项groupIdcom.baomidou/groupId artifactId my batis-plus-boot-starter/artifactId版本${mybatis-plus.version

  t;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.xmlmapper.xml层在resource中,mybaites自动将数据返回给mapper层dao与数据库1v1对应的类,驼峰-》对应数据库的下划线resource中的目录

  

 

  idea下载安装插件

  lombok

  mybaits-tool(任意工具都可以plugin里很多,主要用于xml与mapper跳转)

  

 

  

配置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; @Datapublic 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;

 

  

Paramr案例

(前端字段交互部分)

 

  例:入口(查询条件)

  

package com.example.etf.story.paramer; import com.fasterxml.jackson.annotation.JsonFormat;import lombok.Data; import java.util.ArrayList;import java.util.List; @Datapublic 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; @Datapublic 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@RestControllerpublic 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; @Servicepublic 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@Componentpublic 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多层架构内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: