服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - springboot 多模块将dao(mybatis)项目拆分出去

springboot 多模块将dao(mybatis)项目拆分出去

2021-04-25 13:15Sniper_ZL Java教程

这篇文章主要介绍了springboot 多模块将dao(mybatis)项目拆分出去,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言:

以前我们在建项目的时候, 要么将所有的package建在一个项目里面, 在处理引用的时候, 真的很方便. 不用担心, 有些东西配置不到或者读取不到.

或者, 将package独立出去, 到一个项目中或者子项目中. 这时候, 项目中的引用处理, 还是有些麻烦的. 不过好处更多, 不再表述.

在 idea 里面, 推荐使用 多模块 建项目, 而不再是 eclipse 里面的那种方式. 那这里, 就试着将一个springboot 的项目拆分到子模块中去, 看看效果如何.

项目拆分:

1. 目录变化

springboot 多模块将dao(mybatis)项目拆分出去

2. 父项目

父pom.xml理论上来说, 应该是对子项目中的引用进行一个约束, 主要是版本约束.

所以父 pom.xm 中, 应该使用dependencymanagement 来约束 子项目中 jar 包版本.

然后对于一些子项目都用得着的引用, 可以提到 父项目中去.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?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>
    <dependencies>
      <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>
 
    </dependencies>
  </dependencymanagement>
 
  <!--申明依赖关系-->
  <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>
 
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

3. 子项目 - pojo

springboot 多模块将dao(mybatis)项目拆分出去

3.1 pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

3.2 db实体

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
  }
}

对于pojo和dao中的内容, 可以使用mybatis逆向工程生成, 可以省去很多麻烦.

4. 子项目 - dao

springboot 多模块将dao(mybatis)项目拆分出去

4.1 pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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>
    <!-- 内部引用 -->
    <dependency>
      <groupid>cn.elvinle</groupid>
      <artifactid>pojo</artifactid>
      <version>0.0.1-snapshot</version>
    </dependency>
 
    <!-- 外部引用 -->
    <!-- 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>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

4.2 mapper

usermapper.java:

?
1
2
3
public interface usermapper {
  public list<user> getall();
}

usermapper.xml:

?
1
2
3
4
5
6
7
<?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. 子项目 - service

springboot 多模块将dao(mybatis)项目拆分出去

5.1 pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

5.2 userservice.java

?
1
2
3
public interface userservice {
  public list<user> getall();
}

6. 子项目 - simpl

springboot 多模块将dao(mybatis)项目拆分出去

6.1 pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?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>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

6.2 usersimpl

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@service
public class usersimpl implements userservice {
 
  @autowired
  private usermapper usermapper;
 
  @override
  public list<user> getall() {
    system.out.println("usersimpl getall");
 
    return usermapper.getall();
  }
}

7. 子项目 - web

关键的地方来了.

springboot 多模块将dao(mybatis)项目拆分出去

7.1 pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

7.2 application.yml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
server:
 context-path: /parent
 port: 8080
 
mybatis:
 config-location: /mapper/*.xml
 
spring:
 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 控制器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@restcontroller
@requestmapping("api")
public class apicontroller {
 
  @autowired
  private userservice userservice;
 
  @requestmapping("index")
  public list<user> index(){
 
    list<user> all = userservice.getall();
 
    return all;
  }
}

到目前为止, 没什么特别的, 都是正常修改, 接下来, 会出现与不分模块不同的地方.

7.4 入口处修改

?
1
2
3
4
5
6
7
8
@componentscan({"cn.elvinle"})
@springbootapplication
public class webapplication {
 
  public static void main(string[] args) {
    springapplication.run(webapplication.class, args);
  }
}

这时候, 如果直接运行起程序, 是不会成功访问的. 原因在于, mybatis的自动化配置和自动化创建, 没有支持到多模块中.

这时候, 需要我们手动进行配置和创建.

7.5 mybatis java 配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- mybatis 的工厂 -->
  <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
    <property name="datasource" ref="datasource" />
    <property name="configlocation" value="classpath:mybatis/sqlmapconfig.xml"/>
    <!-- 自动扫描mapping.xml文件 -->
    <!--<property name="mapperlocations" value="classpath:mapper/*.xml" />-->
  </bean>
 
  <!-- dao接口所在包名,spring会自动查找其下的类 -->
  <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
    <property name="basepackage" value="cn.elvinle.bookshop.mapper" />
    <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"/>
  </bean>

可以对照着上面的配置文件, 进行java配置

mybatisconfig:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@configuration
public 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:

?
1
2
3
4
5
6
7
8
9
10
11
@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, 到这里, 就可以把程序跑起来了, 看一下结果:

springboot 多模块将dao(mybatis)项目拆分出去

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/elvinle/p/8177825.html

延伸 · 阅读

精彩推荐