SpringBoot+Mybatis+thymeleaf+gradle+idea 项目 搭建 打包 运行

一、 项目构建

1. 项目总览

2. 构建gradle项目:

   

 3. 创建子模块

  

 4. 配置子模块下build.gradle文件:

apply plugin: 'application'

group 'com.hsm.ls'
version '1.0-SNAPSHOT'

mainClassName = "com.hsm.ls.collection.web.CollectionWebStartApplication"


sourceCompatibility = 1.8

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

repositories {
//    mavenCentral()
    maven {
        url "http://maven.iflytek.com:8081/nexus/content/groups/public/"
    }
    maven {
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile ("org.springframework.boot:spring-boot-starter:2.0.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-web:2.0.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf:2.0.0.RELEASE")
    compile ('org.springframework:spring-jdbc:4.3.12.RELEASE')

    compile ('com.iflytek.dubbo:common-dubbo-framework:1.2.0-SNAPSHOT')
            {
                exclude group: 'org.apache.solr'
            }
    compile ('com.iflytek.aisp:aisp-service-biz-api:1.0.0-SNAPSHOT')

    compile ("org.apache.poi:poi:3.15")
    compile ("org.apache.poi:poi-ooxml:3.15")

    compile ('c3p0:c3p0:0.9.1.2')
    compile ('org.mybatis:mybatis:3.2.3')
    compile ('org.mybatis:mybatis-spring:1.2.3')
    compile ('mysql:mysql-connector-java:6.0.3')

}

distributions() {
    main {
        baseName = "collection-web"
        contents {
            from("src/main/resources") {
                into "conf"
            }
        }
    }
}

5. 编写启动类:

package com.hsm.ls.collection.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

/**
 * Created by lfx on 2018/8/30.
 */
@SpringBootApplication
//@ImportResource(locations = "classpath:server-web.xml")
public class CollectionWebStartApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(CollectionWebStartApplication.class);
        application.run(args);
        System.out.println("started collection-tool-web server success.");
    }
}

6. 测试启动是否成功

  若启动成功则继续进行,若有问题则排查问题直到启动成功

7. 配置application.yml

server:
  port: 8080

#thymeleaf
spring:
  thymeleaf:
    mode: HTML5
    encoding: UTF-8
#    content-type: text/html
    #开发时关闭缓存,不然没法看到实时页面
    cache: false

# db config
jdbc:
  engine:
    driverClass: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://127.0.0.1:3306/db1?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true&useSSL=false
    user: root
    password: 123456
  biz:
    driverClass: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://127.0.0.1:3306/db2?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true&useSSL=false
    user: root
    password: 123456
  # 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3
  initialPoolSize: 5
  # 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0
  maxIdleTime: 60
  # 连接池中保留的最大连接数。默认值: 15
  maxPoolSize: 15
  # 连接池中保留的最小连接数,默认为:3
  minPoolSize: 5
  # 当连接池连接耗尽时,客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。默认: 0
  checkoutTimeout: 5000
  # 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3
  acquireIncrement: 5
  # 定义在从数据库获取新连接失败后重复尝试的次数。默认值: 30 ;小于等于0表示无限次
  acquireRetryAttempts: 10
  # 重新尝试的时间间隔,默认为:1000毫秒
  acquireRetryDelay: 1000
  # 每120秒检查所有连接池中的空闲连接。默认值: 0,不检查
  idleConnectionTestPeriod: 120
logging:
  level:
    com: debug

   该项目用到了双库,且必须设置时区,不需要的小伙伴可去掉时区设置或者去掉双库,按照需求来定。

       时区:jdbcUrl中的serverTimezone=GMT%2B8可去掉

8. 数据库连接配置类:

 

由于该项目需要使用到双库,故有两个配置类,配置类如下:

package com.hsm.ls.collection.web.service;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;

import java.beans.PropertyVetoException;

/**
 * Created by lfx on 2018/8/10.
 */
@Component
@MapperScan(
        basePackages = {"com.hsm.ls.collection.web.mapper.biz"},
        sqlSessionFactoryRef = "flowBizSqlSessionFactory"
)
public class FlowBizDbConfig {

    @Value("${jdbc.biz.jdbcUrl}")
    private String jdbcUrl;
    @Value("${jdbc.biz.user}")
    private String user;
    @Value("${jdbc.biz.password}")
    private String password;
    @Value("${jdbc.initialPoolSize}")
    private int initialPoolSize;
    @Value("${jdbc.maxIdleTime}")
    private int maxIdleTime;
    @Value("${jdbc.maxPoolSize}")
    private int maxPoolSize;
    @Value("${jdbc.minPoolSize}")
    private int minPoolSize;
    @Value("${jdbc.checkoutTimeout}")
    private int checkoutTimeout;
    @Value("${jdbc.acquireIncrement}")
    private int acquireIncrement;
    @Value("${jdbc.acquireRetryAttempts}")
    private int acquireRetryAttempts;
    @Value("${jdbc.acquireRetryDelay}")
    private int acquireRetryDelay;
    @Value("${jdbc.idleConnectionTestPeriod}")
    private int idleConnectionTestPeriod;

    @Bean(name = "flowBizDataSource")
    public ComboPooledDataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setInitialPoolSize(initialPoolSize);
        dataSource.setMaxIdleTime(maxIdleTime);
        dataSource.setMaxPoolSize(maxPoolSize);
        dataSource.setMinPoolSize(minPoolSize);
        dataSource.setCheckoutTimeout(checkoutTimeout);
        dataSource.setAcquireIncrement(acquireIncrement);
        dataSource.setAcquireRetryAttempts(acquireRetryAttempts);
        dataSource.setAcquireRetryDelay(acquireRetryDelay);
        dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
        return dataSource;
    }

    @Bean(name = "flowBizSqlSessionFactory")
    public SqlSessionFactory getSessionFactory(@Qualifier("flowBizDataSource") ComboPooledDataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        //开启驼峰命名转换
        factoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return factoryBean.getObject();
    }

    @Bean(name = "flowBizTrans")
    public DataSourceTransactionManager getTransactionManager(@Qualifier("flowBizDataSource") ComboPooledDataSource dataSource){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
}

 

package com.hsm.ls.collection.web.service;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;

import java.beans.PropertyVetoException;

/**
 * Created by weiluo on 2017/11/27.
 */
@Component
@MapperScan(
        basePackages = {"com.hsm.ls.collection.web.mapper.engine"},
        sqlSessionFactoryRef = "flowEngineSqlSessionFactory"
)
public class FlowEngineDbConfig {

    @Value("${jdbc.engine.jdbcUrl}")
    private String jdbcUrl;
    @Value("${jdbc.engine.user}")
    private String user;
    @Value("${jdbc.engine.password}")
    private String password;
    @Value("${jdbc.initialPoolSize}")
    private int initialPoolSize;
    @Value("${jdbc.maxIdleTime}")
    private int maxIdleTime;
    @Value("${jdbc.maxPoolSize}")
    private int maxPoolSize;
    @Value("${jdbc.minPoolSize}")
    private int minPoolSize;
    @Value("${jdbc.checkoutTimeout}")
    private int checkoutTimeout;
    @Value("${jdbc.acquireIncrement}")
    private int acquireIncrement;
    @Value("${jdbc.acquireRetryAttempts}")
    private int acquireRetryAttempts;
    @Value("${jdbc.acquireRetryDelay}")
    private int acquireRetryDelay;
    @Value("${jdbc.idleConnectionTestPeriod}")
    private int idleConnectionTestPeriod;

    @Bean(name = "flowEngineDataSource")
    public ComboPooledDataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setInitialPoolSize(initialPoolSize);
        dataSource.setMaxIdleTime(maxIdleTime);
        dataSource.setMaxPoolSize(maxPoolSize);
        dataSource.setMinPoolSize(minPoolSize);
        dataSource.setCheckoutTimeout(checkoutTimeout);
        dataSource.setAcquireIncrement(acquireIncrement);
        dataSource.setAcquireRetryAttempts(acquireRetryAttempts);
        dataSource.setAcquireRetryDelay(acquireRetryDelay);
        dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
        return dataSource;
    }

    @Bean(name = "flowEngineSqlSessionFactory")
    public SqlSessionFactory getSessionFactory(@Qualifier("flowEngineDataSource") ComboPooledDataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        //开启驼峰命名转换
        factoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return factoryBean.getObject();
    }

    @Bean(name = "flowEngineTrans")
    public DataSourceTransactionManager getTransactionManager(@Qualifier("flowEngineDataSource") ComboPooledDataSource dataSource){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

}

分别对应mapper目录下的两个文件夹

 9. 编写dao层:

package com.hsm.ls.collection.web.mapper.biz;

import com.hsm.ls.collection.web.domain.MarketBatchInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

/**
 * Created by lfx on 2018/9/3.
 */
public interface MarketMapper {

    @SelectProvider(type = MarketProvider.class, method = "getBatchInfoList")
    public List<MarketBatchInfo> getBatchInfoByIdOrName(@Param("batchId") String batchId, @Param("batchName") String batchName);
}
package com.hsm.ls.collection.web.mapper.biz;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Created by lfx on 2018/9/3.
 */
public class MarketProvider {

    /**
     * 获取满足条件的批次
     * @param params
     * @return
     */
    public String getBatchInfoList(Map<String,Object> params){
        String batchId = (String)params.get("batchId");
        String batchName = (String) params.get("batchName");
        StringBuilder sb = new StringBuilder();
        sb.append("select info.Id,info.BatchName,info.BusinessId,info.PlatTaskId from MarketBatchInfo info where 1=1 ");

        if(StringUtils.isNotBlank(batchId)){
            sb.append(" and info.id = " + batchId);
        }
        if(StringUtils.isNotBlank(batchName)){
            sb.append(" and info.BatchName LIKE '%"+batchName+"%' ");
        }
        return sb.toString();
    }

}

10. controller层:

 11. 配置thymeleaf

   见第七步,已配置。

 12. 静态页

  随便你写点啥  反正能正常访问即可。

 

 若到此步皆正常,则可以放心开始填充业务和功能了~~~~  

 

二、 项目打包运行

1. dist文件夹:

下次再写。。。。。 来活了md

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值