一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - 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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 近亲乱中文字幕 | 91精品啪在线观看国产老湿机 | 日日草视频 | 男男浴室吸乳play | 天堂成人在线观看 | 女性全身裸露无遮挡 | 91av爱爱 | 国产原创一区二区 | 91李宗精品72集在线观看 | 性关系视频免费网站在线观看 | 国产思妍小仙女一二区 | 国产午夜精品福利 | 国产日韩精品一区二区在线观看 | 国产成人综合一区人人 | 欧亚专线欧洲m码可遇不可求 | 国产偷啪视频一区 | 24adc年龄18岁欢迎大驾光临 | 欧美亚洲国产一区二区三区 | 国产精品suv一区二区 | 波多野结衣 在线播放 | 欧美日韩专区国产精品 | 欧美在线观看网址 | 糖心vlog麻豆精东影业传媒 | 欧美乱子伦xxxx12在线 | 爽新片xxxxxxx | 高清日韩在线 | 日韩欧美不卡视频 | 日本xxxxx高清免费观看 | 亚洲热在线观看 | 范冰冰a级一级特级毛片 | 国内精品国语自产拍在线观看55 | 双性np肉文 | 高清在线观看mv的网址免费 | 亚洲成人国产精品 | 亚洲精品国产乱码AV在线观看 | 国产成年人在线观看 | 久久精品无码一区二区日韩av | 久草在线草a免费线看 | 亚洲qvod图片区电影 | 国产小视频免费看 | 丰满岳乱妇在线观看视频国产 |