IDEA 搭建多模块SpringBoot 项目:集成MyBatisDruidDubbo

Posted 在奋斗的大道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IDEA 搭建多模块SpringBoot 项目:集成MyBatisDruidDubbo相关的知识,希望对你有一定的参考价值。

智慧城管项目结构划分:

SmartUrban 智慧城管父类

核心pom.xml文件:定义功能模块和依赖SpringBoot parent 版本信息。

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>SmartUrban</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>SmartUrban-provider</module>
        <module>SmartUrban-Consumer</module>
        <module>SmartUrban-Generator</module>
        <module>SmartUrban-Common</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

SmartUrban-Common 通用模块

主要用于定于Service 和Model 实体对象。

package com.zzg.model;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable 
    private String id;

    private String account;

    private String name;

    private String passwd;

    private Date createDt;

    private String createBy;

    private Date updateDt;

    private String updateBy;

    private Byte state;

    private static final long serialVersionUID = 1L;

    public String getId() 
        return id;
    

    public void setId(String id) 
        this.id = id == null ? null : id.trim();
    

    public String getAccount() 
        return account;
    

    public void setAccount(String account) 
        this.account = account == null ? null : account.trim();
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name == null ? null : name.trim();
    

    public String getPasswd() 
        return passwd;
    

    public void setPasswd(String passwd) 
        this.passwd = passwd == null ? null : passwd.trim();
    

    public Date getCreateDt() 
        return createDt;
    

    public void setCreateDt(Date createDt) 
        this.createDt = createDt;
    

    public String getCreateBy() 
        return createBy;
    

    public void setCreateBy(String createBy) 
        this.createBy = createBy == null ? null : createBy.trim();
    

    public Date getUpdateDt() 
        return updateDt;
    

    public void setUpdateDt(Date updateDt) 
        this.updateDt = updateDt;
    

    public String getUpdateBy() 
        return updateBy;
    

    public void setUpdateBy(String updateBy) 
        this.updateBy = updateBy == null ? null : updateBy.trim();
    

    public Byte getState() 
        return state;
    

    public void setState(Byte state) 
        this.state = state;
    

    @Override
    public String toString() 
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", account=").append(account);
        sb.append(", name=").append(name);
        sb.append(", passwd=").append(passwd);
        sb.append(", createDt=").append(createDt);
        sb.append(", createBy=").append(createBy);
        sb.append(", updateDt=").append(updateDt);
        sb.append(", updateBy=").append(updateBy);
        sb.append(", state=").append(state);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    

package com.zzg.service;

import com.zzg.model.User;
import java.util.List;

public interface UserService 
    List<User> selectAll();

SmartUrban-Generator 代码生成器

基于MyBatis-Generator 生成工具,创建的代码生成器。

核心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">
    <parent>
        <artifactId>SmartUrban</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SmartUrban-Generator</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.6</version>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator/mybatis-generator-cfg.xml
                    </configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                    <!--mysql 驱动程序 -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.12</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>


</project>

执行完,MyBatis-Generator 代码生成器后,如下截图:

会依据配置文件在指定文件生成对于实体的model\\mapper\\xml 文件。

SmartUrban-provider 服务提供者

核心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">
    <parent>
        <artifactId>SmartUrban</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SmartUrban-provider</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--集成common 模块-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>SmartUrban-Common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--springboot 与 mybatis 集成 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- 数据库连接池druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--mysql 驱动程序 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>
        <!--集成dubbo 服务 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

    </dependencies>
</project>

Service 接口实现类、Mapper 接口定义,Xml文件

package com.zzg.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.zzg.mapper.UserMapper;
import com.zzg.model.User;
import com.zzg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Service
@Component
public class UserServiceImpl implements UserService 
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> selectAll() 
        return userMapper.selectAll();
    

package com.zzg.mapper;

import com.zzg.model.User;
import java.util.List;

public interface UserMapper 
    int deleteByPrimaryKey(String id);

    int insert(User record);

    User selectByPrimaryKey(String id);

    List<User> selectAll();

    int updateByPrimaryKey(User record);
<?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="com.zzg.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.zzg.model.User">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="account" jdbcType="VARCHAR" property="account" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="passwd" jdbcType="VARCHAR" property="passwd" />
    <result column="create_dt" jdbcType="TIMESTAMP" property="createDt" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="update_dt" jdbcType="TIMESTAMP" property="updateDt" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="state" jdbcType="TINYINT" property="state" />
  </resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from house_user
    where id = #id,jdbcType=VARCHAR
  </delete>
  <insert id="insert" parameterType="com.zzg.model.User">
    insert into house_user (id, account, `name`, 
      passwd, create_dt, create_by, 
      update_dt, update_by, `state`
      )
    values (#id,jdbcType=VARCHAR, #account,jdbcType=VARCHAR, #name,jdbcType=VARCHAR, 
      #passwd,jdbcType=VARCHAR, #createDt,jdbcType=TIMESTAMP, #createBy,jdbcType=VARCHAR, 
      #updateDt,jdbcType=TIMESTAMP, #updateBy,jdbcType=VARCHAR, #state,jdbcType=TINYINT
      )
  </insert>
  <update id="updateByPrimaryKey" parameterType="com.zzg.model.User">
    update house_user
    set account = #account,jdbcType=VARCHAR,
      `name` = #name,jdbcType=VARCHAR,
      passwd = #passwd,jdbcType=VARCHAR,
      create_dt = #createDt,jdbcType=TIMESTAMP,
      create_by = #createBy,jdbcType=VARCHAR,
      update_dt = #updateDt,jdbcType=TIMESTAMP,
      update_by = #updateBy,jdbcType=VARCHAR,
      `state` = #state,jdbcType=TINYINT
    where id = #id,jdbcType=VARCHAR
  </update>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select id, account, `name`, passwd, create_dt, create_by, update_dt, update_by, `state`
    from house_user
    where id = #id,jdbcType=VARCHAR
  </select>
  <select id="selectAll" resultMap="BaseResultMap">
    select id, account, `name`, passwd, create_dt, create_by, update_dt, update_by, `state`
    from house_user
  </select>
</mapper>

application.properties 配置文件和程序入口application

server.prot=8081
server.servlet.context-path=/provider
# ???????
spring.datasource.url=jdbc:mysql://localhost:3306/house?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# druid ??
# ?????????????
spring.datasource.druid.initial-size=5
# ???????
spring.datasource.druid.max-active=30
# ???????
spring.datasource.druid.min-idle=5
# ????????????????
spring.datasource.druid.max-wait=60000
# ???????????????????????????????
spring.datasource.druid.time-between-eviction-runs-millis=60000
# ????????????????
spring.datasource.druid.min-evictable-idle-time-millis=300000
# ???????????sql??????????
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
# ?????true?????????????????????????????????timeBetweenEvictionRunsMillis???validationQuery?????????
spring.datasource.druid.test-while-idle=true
# ???????validationQuery?????????????????????
spring.datasource.druid.test-on-borrow=false
# ???????validationQuery?????????????????????
spring.datasource.druid.test-on-return=false
# ????preparedStatement????PSCache?PSCache???????????????????oracle??mysql??????
spring.datasource.druid.pool-prepared-statements=true
# ???PSCache???????0????0??poolPreparedStatements???????true?
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50
# ?????????filters????????sql????
spring.datasource.druid.filters=stat,wall,slf4j
# ??connectProperties?????mergeSql????SQL??
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# ????DruidDataSource?????
spring.datasource.druid.use-global-data-source-stat=true
##### WebStatFilter?? #######
#??StatFilter
spring.datasource.druid.web-stat-filter.enabled=true
#??????
spring.datasource.druid.web-stat-filter.url-pattern=/*
#????????url
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
#??session????
spring.datasource.druid.web-stat-filter.session-stat-enable=true
#??sessionStatMaxCount?1000?
spring.datasource.druid.web-stat-filter.session-stat-max-count=1000
#spring.datasource.druid.web-stat-filter.principal-session-name=
#spring.datasource.druid.web-stat-filter.principal-cookie-name=
#spring.datasource.druid.web-stat-filter.profile-enable=
##### StatViewServlet?? #######
#?????????
spring.datasource.druid.stat-view-servlet.enabled=true
#?????????
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
#?? Reset All ??
spring.datasource.druid.stat-view-servlet.reset-enable=false
#???????
spring.datasource.druid.stat-view-servlet.login-username=admin
#??????
spring.datasource.druid.stat-view-servlet.login-password=123
#??????allow?????????????????
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
#????deny???allow????deny???????allow??????????
spring.datasource.druid.stat-view-servlet.deny=
# mybatis ??
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.zzg.model
# dubbo ????
dubbo.application.name=provider
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
dubbo.consumer.check=false
dubbo.comsumer.timeout=6000


# ????????
logging.level.com.zzg.mapper=debug

 

package com.zzg;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@MapperScan("com.zzg.mapper")
@EnableDubbo
public class ProviderApplication 
    public static void main(String[] args) 
        SpringApplication.run(ProviderApplication.class, args);
    

SmartUrban-Consumer 服务消费者

核心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">
    <parent>
        <artifactId>SmartUrban</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SmartUrban-Consumer</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <dependencies>
        <!--集成common 模块-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>SmartUrban-Common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <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>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

</project>

 Controller 定义和程序入口Application.

package com.zzg.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zzg.model.User;
import com.zzg.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController 
    @Reference
    private UserService userService;

    @RequestMapping("/hello")
    public String get() 
        List<User> list = userService.selectAll();
        return "Hello Spring Boot!";
    

package com.zzg;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// 开启dubbo的自动配置
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication 
    public static void main(String[] args) 
        SpringApplication.run(ConsumerApplication.class, args);
    

application.properties

server.prot=8082
server.servlet.context-path=/consumer
# dubbo ??
dubbo.application.name=consumer
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
dubbo.registry.check=false
dubbo.consumer.check=false
dubbo.comsumer.timeout=6000

以上是关于IDEA 搭建多模块SpringBoot 项目:集成MyBatisDruidDubbo的主要内容,如果未能解决你的问题,请参考以下文章

多模块项目搭建(IDEA+Maven+SpringBoot)

IDEA SpringBoot多模块项目搭建详细过程(转)

SpringBoot+Mybatis多模块(module)项目搭建教程

记Spring搭建功能完整的个人博客「Oyster」全过程[其二] Idea中Maven+SpringBoot多模块项目开发的设计和各种坑(模块间依赖和打包问题)

Maven 搭建spring boot多模块项目

SpringBoot + React 前后端分离多模块项目框架搭建流程