Preface:
In the past, when we were building a project, we either built all the packages in one project, which was really convenient when processing references. Don’t worry, some things cannot be configured or read.
Or, separate the package into a project or sub-project. At this time, the reference processing in the project is still a bit troublesome. However, the benefits are more, so I will not express it anymore.
In idea, it is recommended to use multi-module construction projects instead of the way in eclipse. Then, try to split a springboot project into a submodule to see how it works.
Project split:
1. Directory changes
2. Parent project
In theory, parent pom.xml should be a constraint on references in child projects, mainly version constraints.
Therefore, in the parent pom.xm, dependencyManagement should be used to constrain the jar package version in the subproject.
Then, for references that are useful for some subprojects, you can mention them in the parent project.
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>parent</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <modules> <module>pojo</module> <module>web</module> <module>dao</module> <module>service</module> <module>simpl</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <druid.version>1.1.3</druid.version> <mybatis.boot.starter.version>1.3.1</mybatis.boot.starter.version> <mysql.connector.java.version>5.1.44</mysql.connector.java.version> </properties> <dependencyManagement> <dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.boot.starter.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.java.version}</version> </dependency> </dependency> </dependencyManagement> <!--Declare dependencies--> <dependencies> <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> </dependency> </dependency> </dependency> </build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>3. Subproject - pojo
3.1 pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>pojo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>pojo</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
3.2 db entity
package cn.elvinle.pojo;/** * @author: elvin */public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}For content in pojo and dao, you can use mybatis reverse engineering to generate it, which can save a lot of trouble.
4. Subproject- dao
4.1 pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dao</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- Internal reference--> <dependency> <groupId>cn.elvinle</groupId> <artifactId>pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- External reference--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> </dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
4.2 mapper
UserMapper.java:
public interface UserMapper { public List<User> getAll();}UserMapper.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="cn.elvinle.dao.mapper.UserMapper" > <select id="getAll" resultType="cn.elvinle.pojo.User"> select * from user </select></mapper>
5. Subproject-service
5.1 pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>cn.elvinle</groupId> <artifactId>pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
5.2 UserService.java
public interface UserService { public List<User> getAll();}6. Subproject - simpl
6.1 pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>simpl</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>simpl</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>cn.elvinle</groupId> <artifactId>pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>cn.elvinle</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>cn.elvinle</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependency> </dependency> </build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
6.2 UserSImpl
@Servicepublic class UserSImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> getAll() { System.out.println("UserSImpl getAll"); return userMapper.getAll(); }}7. Subproject - web
The key place is here.
7.1 pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>web</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>cn.elvinle</groupId> <artifactId>simpl</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
7.2 application.yml
server: context-path: /parent port: 8080mybatis: config-location: /mapper/*.xmlspring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver
7.3 Controller
@RestController@RequestMapping("api")public class ApiController { @Autowired private UserService userService; @RequestMapping("index") public List<User> index(){ List<User> all = userService.getAll(); return all; }}So far, nothing special, it has been modified normally. Next, there will be differences from the ones without modules.
7.4 Modification at the entrance
@ComponentScan({"cn.elvinle"})@SpringBootApplicationpublic class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); }}At this time, if the program is run directly, it will not be successfully accessed. The reason is that mybatis' automated configuration and automation creation are not supported in multiple modules.
At this time, we need to manually configure and create.
7.5 Mybatis java configuration
<!-- mybatis factory --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/> <!-- Automatically scan mapping.xml file --> <!--<property name="mapperLocations" value="classpath:mapper/*.xml" />--> </bean> <!-- The package name where the DAO interface is located, Spring will automatically find the class under it --> <bean id="mapperScannerConfigurer"> <property name="basePackage" value="cn.elvinle.bookshop.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
You can use Java configuration to compare the above configuration file
MybatisConfig:
@Configurationpublic class MybatisConfig { @Value("${mybatis.config-location}") private String mapperLocationPattern; @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource(){return new com.alibaba.druid.pool.DruidDataSource(); } @Bean(name="sqlSessionFactory") public SqlSessionFactory sqlSessionFactory() throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocationPattern)); return sqlSessionFactoryBean.getObject(); }}MybatisMapperScannerConfig:
@Configuration@AutoConfigureAfter(MybatisConfig.class)@MapperScan("cn.elvinle.dao.mapper")public class MybatisMapperScannerConfig { public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); mapperScannerConfigurer.setBasePackage("cn.elvinle.dao.mapper"); return mapperScannerConfigurer; }}OK, here you can run the program and see the results:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.