1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > idea 快速搭建spring boot 多模块项目(底部附源码)

idea 快速搭建spring boot 多模块项目(底部附源码)

时间:2019-08-13 02:06:04

相关推荐

idea 快速搭建spring boot 多模块项目(底部附源码)

独角兽企业重金招聘Python工程师标准>>>

第一步 :创建父maven 模块,新建一个spring boot项目

父类pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!-- 聚合子模块 --><modules><module>web</module><module>dao</module><module>generate</module><module>service</module><module>dao</module><module>service</module><module>web</module><module>generate</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><!-- 父模块信息--><groupId>com.hht</groupId><artifactId>multimodule</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><name>spring-boot-multi-module-demo</name><description>Demo project for Multi Module Spring Boot</description><!-- 公共信息--><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>provided</scope></dependency></dependencies></project>

第二步:只保留pom.xml文件,删除src文件,然后依次新增dao、service、web模块,我这里多加了一个 generate方便生成mybatis对象

这个是最顶级的目录结构

第三步:generate模块配置,这一步可以忽略从自己的现有的功能模块copy就好了

只需要两个文件,然后运行generateplugin就ok了

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><parent><artifactId>multimodule</artifactId><groupId>com.hht</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>generate</artifactId><name>mybatis generate module</name><description> generate mybatis vo ,mapper and map.xml by database</description><build><plugins><!-- mybatis generator 自动生成代码插件 --><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.2</version><configuration><configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile><overwrite>true</overwrite><verbose>true</verbose></configuration></plugin></plugins></build></project>

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfigurationPUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN""/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包--><classPathEntry location="D:\myeclipse_workspace\ucenter\WebRoot\WEB-INF\lib\mysql-connector-java-5.1.21.jar"/><context id="DB2Tables" targetRuntime="MyBatis3"><commentGenerator><property name="suppressDate" value="true"/><!-- 是否去除自动生成的注释 true:是 : false:否 --><property name="suppressAllComments" value="true"/></commentGenerator><!--数据库链接URL,用户名、密码 --><jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/datacenter" userId="root" password="root"></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false"/></javaTypeResolver><!-- 生成模型的包名和位置--><javaModelGenerator targetPackage="com.hht.model" targetProject="src/main/java"><property name="enableSubPackages" value="true"/><property name="trimStrings" value="true"/></javaModelGenerator><!-- 生成映射文件的包名和位置--><sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"><property name="enableSubPackages" value="true"/></sqlMapGenerator><!-- 生成DAO的包名和位置--><javaClientGenerator type="XMLMAPPER" targetPackage="com.hht.mapper" targetProject="src/main/java"><property name="enableSubPackages" value="true"/></javaClientGenerator><!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--><table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table></context></generatorConfiguration>

最后生成三个文件

如果遇到问题,可以参考一下这一篇博客/haitaohu/blog/2966616

第四步:配置dao模块

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><parent><artifactId>multimodule</artifactId><groupId>com.hht</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>dao</artifactId><packaging>jar</packaging><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.46</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency></dependencies></project>

mybatis 相关的文件,从 generate生成的文件copy过来

DaoApplication文件

package com.hht;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author hht* @ClassName DaoApplication* @Description TODO* @Date /4/18 14:24* @VERSION 1.0*/@SpringBootApplication@MapperScan("com.hht.mapper")public class DaoApplication {public static void main(String[] args) {SpringApplication application = new SpringApplication(DaoApplication.class);application.run(args);}}

第五步:配置service

这个比较简单,依赖 dao就可以了

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><parent><artifactId>multimodule</artifactId><groupId>com.hht</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>service</artifactId><dependencies><dependency><groupId>com.hht</groupId><artifactId>dao</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>

Service实现

package com.hht.service;import com.hht.mapper.UserMapper;import com.hht.model.User;import org.springframework.beans.factory.annotation.Autowired;import org.ponent;import org.springframework.stereotype.Service;/*** @author hht* @ClassName UserService* @Description TODO* @Date /4/18 17:53* @VERSION 1.0*/@Servicepublic class UserService {@Autowiredprivate UserMapper userMapper;public void saveUser(User user){userMapper.insert(user);}}

最后一步:配置web模块

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><parent><artifactId>multimodule</artifactId><groupId>com.hht</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>web</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><parent><artifactId>multimodule</artifactId><groupId>com.hht</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>web</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.hht</groupId><artifactId>service</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>

application.yml

server:port: 8081spring:datasource:name: datacenterurl: jdbc:mysql://127.0.0.1:3306/datacenter?characterEncoding=utf8&useSSL=true&serverTimezone=UTCusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driverfilters: statmaxActive: 20initialSize: 1maxWait: 60000minIdle: 1timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: select 'x'testWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truemaxOpenPreparedStatements: 20mybatis:mapper-locations: classpath:mapping/*.xml #注意:一定要对应mapper映射xml文件的所在路径type-aliases-package: com.hht.datacenter.model # 注意:对应实体类的路径

最后运行web项目成功

注意:这里只是记录多模块开发,并不是一个完善的生产模式,dao做了junit单模块测试,其它的没有加 可以自己加一下,参考代码

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。