• Java微服务(Spring-boot+MyBatis+Maven)入门教程


    1,项目创建
        新建maven项目,如下图:

    选择路径,下一步

    输入1和2的内容,点完成

    项目创建完毕,结构如下图所示:

    填写pom.xml里内容,为了用于打包,3必须选择jar,4和5按图上填写

    2,项目配置
    2-1,application.properties
    server.address=localhost
    server.context-path=/
    server.port=8090

    #------ isosurface ---------
    spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=gisdemo
    spring.datasource.username=sa
    spring.datasource.password=sapwd1234
    spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
    2-2,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>

    <groupId>com</groupId>
    <artifactId>gis</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>gis.services</name>

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath />
    </parent>

    <repositories>
    <repository>
    <id>in-project</id>
    <name>In Project Repo</name>
    <url>file:///${project.basedir}/lib</url>
    </repository>
    </repositories>

    <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>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
    <exclusion>
    <artifactId>spring-boot-starter-logging</artifactId>
    <groupId>org.springframework.boot</groupId>
    </exclusion>
    </exclusions>
    </dependency>
    <!-- Log4j2 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
    <exclusion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
    </dependency>

    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
    </dependency>

    <dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>4.0</version>
    </dependency>
    </dependencies>

    <build>
    <finalName>gisServices</finalName>

    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    <description>gis.services</description>
    </project>
    3,部分页面
    StartServer.java - 服务启动页

    /**
    * 服务启动
    *
    * @date 2017-11-28 13:16:28
    */
    @SpringBootApplication //必须!
    @MapperScan(basePackages = "com.gis.mapper") //必须!
    public class StartServer extends SpringBootServletInitializer {

    private static Class<StartServer> applicationClass = StartServer.class;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(applicationClass);
    }

    public static void main(String[] args) {
    SpringApplication.run(applicationClass, args);
    }
    }
    Mapper.java - 数据访问

    /**
    * <p>
    * Mapper 接口
    * </p>
    *
    * @author ${author}
    * @since 2017-07-25
    */
    @Mapper
    public interface StbprpAndDataMapper{

    //List<StbprpAndData> selectDataAndStbprp(Map<String, Object> params);

    @Select("SELECT A.STCD,A.STNM,A.LGTD,A.LTTD,A.STTP,B.DRP AS COMMENT FROM ST_STBPRP_B A LEFT JOIN " +
    " (SELECT STCD,SUM(DRP) AS DRP FROM ST_PPTN_R WHERE TM <= #{endTime} and TM >= #{beginTime} GROUP BY STCD)B " +
    " ON A.STCD=B.STCD ORDER BY B.DRP DESC")
    List<StbprpAndData> selectDataAndStbprp(@Param("beginTime") String bt,@Param("endTime") String et);
    }
    controller 调用 

    List<StbprpAndData> datalist = stbprpAndDataMapper.selectDataAndStbprp(bt, et);
    4,运行
        直接运行 StartServer.java ,出现下图代表运行成功,

    通过浏览器访问 http://localhost:8090/isosurface/getIso?bt=111&et=222 即可

    localhost、8090 在application.properties中配置 

    isosurface为controller的@RequestMapping

    getIso为conrtoller里方法的@RequestMapping

    bt和et为方法参数

    @RestController
    @RequestMapping("isosurface")
    public class IsosurfaceController {

    @Autowired
    private StbprpAndDataMapper stbprpAndDataMapper;

    @RequestMapping("getIso")
    public String getIso(String bt, String et) {
    List<StbprpAndData> datalist = stbprpAndDataMapper.selectDataAndStbprp(bt, et);
    return "";
    }
    }
    5,打包发布
    运行—调试配置,

    已经创建过的配置会如图所示,2为配置名,3和4默认不变,然后 调试,出现了BUITL SUCCESS代表打包成功,jar文件会在项目的target目录下

    注意:打包会自动从maven服务器上下载jar包然后压缩,无法通过maven下载的第三方jar需要手动添加进本地maven库,如下图所示,先在lib文件夹内根据如下层级结构添加jar包

    1、2、3为groupId,4为artifactId,5为版本号,然后在pom.xml中添加如下节点

    <dependency>
    <groupId>org.freehep.emf</groupId>
    <artifactId>freehep-graphicsio-emf</artifactId>
    <version>2.1.1</version>
    </dependency>
    并且指定本地maven库,也就是项目的lib文件夹

    <repositories>
    <repository>
    <id>in-project</id>
    <name>In Project Repo</name>
    <url>file:///${project.basedir}/lib</url>
    </repository>
    </repositories>
    更新maven 保证不报错即可,如果报错,说明层级结构建的不对,按照maven库生成的目录结构来修改项目lib里的目录结构


    6,批处理运行
    可直接通过cmd运行
    java -jar xxxx.jar
    或者创建bat批处理程序运行

    @echo off

    rem 判断默认jar文件是否存在,存在则启动,不存在则遍历当前目录下的jar文件并启动(只取一个,后者覆盖前者)

    set jarName=gisServices.jar
    set folderName=%cd%

    if not exist "gisServices.jar" (
    for /f "delims=" %%f in ('dir /b /a-d /o-d "%folderName%*.jar"') do (
    set jarName=%%f
    )
    )

    ::echo %jarName%
    java -jar %jarName%
    pause
    7,其他
    配置文件 application.properties 可放到jar同级目录内,外部的配置文件优先级要高于jar包内部的,jar包运行时会自动去查找然后调用配置信息,配置修改后重启服务即可。


    参考文档:https://blog.csdn.net/t123y456m/article/details/79726399 

  • 相关阅读:
    在 XD 和 Axure 中使用 iconfont
    chartjs 曲线图 纪要
    js ajax 等的的一些纪要
    程序员的方向
    sqlserver 常用的练习50例子
    (function(){})()原理
    layer弹出层详解
    sqlserver 表值函数 保存使用
    关于批量下载线程池与信号机制的使用
    tp5.1 phpstudy composer 配置等
  • 原文地址:https://www.cnblogs.com/william-dai/p/10899798.html
Copyright © 2020-2023  润新知