欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

创建多模块的springboot项目

程序员文章站 2024-01-08 13:27:16
...

项目目录结构:

创建多模块的springboot项目

 

父工程:pom工程,管理整个项目依赖的版本

主工程:war工程, 管理整个项目的配置(数据库配置,springmvc配置等等)

公共模块:jar工程,管理整个项目的util

子模块:jar工程,项目会有多个这样的子模块,比如用户管理子模块、角色管理子模块

 

下面我们从0开始搭建一个简单的多模块项目,工程名 chem

STEP ONE:搭建父模块

新建maven项目:file  >> new >> project >> maven project

创建多模块的springboot项目

创建多模块的springboot项目

创建好父工程后,导入maven依赖

<properties>
	<spring.boot.version>2.1.4.RELEASE</spring.boot.version>
	<mybatis.version>2.0.1</mybatis.version>
	<druid.version>1.1.5</druid.version>
	<lang.version>3.8</lang.version>
</properties>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>${spring.boot.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>

		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis.version}</version>
		</dependency>

		<!-- 连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid.version}</version>
		</dependency>

		<!-- lang3工具包 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>${lang.version}</version>
		</dependency>
	</dependencies>
</dependencyManagement>

<build>
	<plugins>
		<!-- jdk版本号 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
				<encoding>UTF-8</encoding>
			</configuration>
		</plugin>
	</plugins>
</build>

 

STEP TWO:创建公共模块 chem-common

创建多模块的springboot项目

编写pom文件

<dependencies>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-lang3</artifactId>
	</dependency>
</dependencies>

 

STEP THREE:创建用户管理子模块

创建多模块的springboot项目

1:编写pom文件:

<dependencies>
	<!-- web -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<!-- mybatis -->
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
	</dependency>

	<!-- lang3 -->
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-lang3</artifactId>
	</dependency>

</dependencies>

2:添加公共模块依赖

鼠标右击chem-module-user项目 maven >> ADD Dependency(添加依赖)

创建多模块的springboot项目

3:执行完添加依赖操作后在pom文件中会多出一个依赖,代表OK

创建多模块的springboot项目

 

STEP FOUR:创建角色管理子模块

不在赘述,同上操作,名称是 chem-module-role

 

STEP FIVE:创建主工程

创建多模块的springboot项目

 

1:创建完项目,提示有错误

创建多模块的springboot项目

 pom文件错误信息:web.xml is missing and <failOnMissingWebXml> is set to true

不要着急,添加好依赖,在设置<failOnMissingWebXml>false</failOnMissingWebXml>,错误就会消失

<dependencies>
	<!-- thymeleaf模板引擎 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
	<dependency>
		<groupId>com.github.theborakompanioni</groupId>
		<artifactId>thymeleaf-extras-shiro</artifactId>
		<version>2.0.0</version>
	</dependency>

	<!-- jdbc -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>

	<!-- web -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<!-- mybatis -->
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
	</dependency>

	<!-- mysql -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>

	<!-- test -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

	<!-- 连接池 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
	</dependency>
</dependencies>

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<version>2.6</version>
			<configuration>
				<failOnMissingWebXml>false</failOnMissingWebXml>
			</configuration>
		</plugin>
	</plugins>
	<finalName>chem</finalName>
</build>

2:OK,配置好pom,再引入公共模块、用户管理子模块和角色管理子模块

<dependency>
	<groupId>com.mote</groupId>
	<artifactId>chem-common</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>
		
<dependency>
	<groupId>com.mote</groupId>
	<artifactId>chem-module-user</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>
	
<dependency>
	<groupId>com.mote</groupId>
	<artifactId>chem-module-role</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

3:配置主项目

创建多模块的springboot项目

启动类:

@SpringBootApplication
public class ChemApplication extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(ChemApplication.class, args);
	}
	
	//重写configure方法
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ChemApplication.class);
    }

}

mysql配置

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration
@MapperScan(basePackages = "com.mote.dao", sqlSessionFactoryRef = "sqlSessionFactory")
public class DatasourceConfig {

	// classpath* 这个*很重要,支持扫描依赖项目的配置文件
	static final String MAPPER_LOCATION = "classpath*:mybatis/mappers/*.xml";

	@Primary
	// 表示这个数据源是默认数据源
	@Bean(name = "dataSource")
	@ConfigurationProperties(prefix = "spring.datasource.mysql")
	public DataSource myDataSource() {
		return DataSourceBuilder.create().build();
	}

	@Primary
	@Bean(name = "sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory(
			@Qualifier("dataSource") DataSource datasource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(datasource);
		bean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));
		bean.setMapperLocations(new PathMatchingResourcePatternResolver()
				.getResources(DatasourceConfig.MAPPER_LOCATION));
		return bean.getObject();
	}

	@Primary
	@Bean("sqlSessionTemplate")
	public SqlSessionTemplate mySqlsessiontemplate(
			@Qualifier("sqlSessionFactory") SqlSessionFactory sessionfactory) {
		return new SqlSessionTemplate(sessionfactory);
	}
}

springmvc相关配置
 

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * springmvc 功能拓展
 * 
 * @author Administrator
 *
 */
@Configuration
public class SpringMvcConfig extends WebMvcConfigurationSupport {

	/**
	 * 静态资源映射:配置配置资源路径
	 */
	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/css/**").addResourceLocations(
				"classpath:/static/css/");
		registry.addResourceHandler("/js/**").addResourceLocations(
				"classpath:/static/js/");
		registry.addResourceHandler("/images/**").addResourceLocations(
				"classpath:/static/images/");
		super.addResourceHandlers(registry);
	}

	/**
	 * 拓展视图映射:跳过后台直接跳转
	 */
	@Override
	protected void addViewControllers(ViewControllerRegistry registry) {
        
		
		super.addViewControllers(registry);
	}

}

TestController

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

	/**
	 * 跳转test页面
	 * @return
	 */
	@GetMapping("test")
	public ModelAndView toTest() {
		ModelAndView mv = new ModelAndView("test");
		return mv;
	}

}

mybatis全局配置文件

<?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" />
		<!-- 开启打印sql -->
		<setting name="logImpl" value="STDOUT_LOGGING" />
		<!-- 开启二级缓存 -->
		<setting name="cacheEnabled" value="true" />
	</settings>
</configuration>

test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>THIS IS A MORE MODULE DEMO</h1>
</body>
</html>

springboot的配置文件(基础配置,都可以用上)

#mysql
spring.datasource.mysql.jdbc-url=jdbc:mysql://114.215.83.3:3306/chemLocation?serverTimezone=UTC&allowMultiQueries=true
spring.datasource.mysql.username=root
spring.datasource.mysql.password=123456
spring.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.mysql.type: com.alibaba.druid.pool.DruidDataSource

# port
server.port=8081

# contextPath
server.servlet.context-path=/chem

#log
logging.path=../logs

#thymeleaf
spring.thymeleaf.cache=false


#utf-8
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8

# tomcat & more springboot
spring.jmx.enabled=false

 

OK,最后启动springboot启动类,访问测试页面

创建多模块的springboot项目

 

附录:eclipse搭建单个springboot项目

创建一个maven项目,继承springboot父工程

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>

创建多模块的springboot项目


创建好会有报错、不要着急,编写pom文件,设置<failOnMissingWebXml>false</failOnMissingWebXml>,错误消失

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>
<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<version>2.6</version>
			<configuration>
				<failOnMissingWebXml>false</failOnMissingWebXml>
			</configuration>
		</plugin>
	</plugins>
	<finalName>bigFile-operaton</finalName>
</build>

 

相关标签: java杂记

上一篇:

下一篇: