Spring Boot环境配置:Spring Boot+Mybatis+Gradle

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。简言之就是,约定大于配置。
突出特点有
1. 创建独立的Spring应用程序
2. 嵌入的Tomcat,无需部署WAR文件
3. 通过各种starter,简化项目依赖配置
3. 自动配置Spring
4. 提供一系列运行监控手段,如指标,健康检查和外部配置
6. 无需代码生成,也不需要XML配置

下面看看如何快速配置springboot脚手架工程。以主要配置文件为说明线索。

build.gradle

首先是依赖文件,spring boot 支持gradle和maven。这里采用gradle举例,maven的只有格式的差异,引用的包都是一样的。
如下:

buildscript {
    ext {
        springBootVersion = '1.5.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile('org.apache.cxf:cxf-spring-boot-starter-jaxrs:3.1.11')
    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0')
    compile('org.springframework.boot:spring-boot-starter-validation')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('com.h2database:h2')
    runtime('mysql:mysql-connector-java')
    /*providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')*/
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

其中,springBootVersion 指定spring boot 的版本。
最主要的是dependencies,里面列出工程依赖的包,和maven的dependencies标签功能是一直的,可是在配置写法上简洁了很多。

application.yml

这个文件是spring boot工程的主要配置文件,支持application.yml和application.properties两种形式。两种方式各有优势,yml层级明确,properties写法传统,简单。
数据库连接的配置信息需要定义在这里,在build.gradle文件里,若引用了org.mybatis.spring.boot:mybatis-spring-boot-starter,则数据库连接信息就必须配置,因为spring boot采用约定的方式,配置org.mybatis.spring.boot:mybatis-spring-boot-starter之后,系统就认为需要数据库,如果不配置的话,则在启动会数据库连接异常,导致无法启动。
当然,application.yml的可配置信息有很多,如下面配置的日志输出信息。或者spring boot默认端口是8080,可以通过server:port: 9000改为9000,等等。
这里示例如下,配置了数据库连接信息,mybatis配置和日志输出。

spring:
  # 数据库配置
  datasource:
    url: jdbc:mysql://localhost/world
    username: root
    password: root
    driver-class-name: org.gjt.mm.mysql.Driver

  # mybatis配置
mybatis:
    mapper-locations: classpath*:mapper/*.xml

logging:
  level: debug
  file: logs/mdesk.log

数据库创建

这不多说,建个测试数据库,并和上面配置的数据库连接保持一致。

mybatis

因持久化层采用mybatis,所以生成mybatis的mapping文件和实体类,这些可以借助mybatis提供的工具自动生成,不是侧重点,不多介绍。
如下是生成的示例,从中也可以看出我的测试库的表结构。

CountryMapper.xml

<?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.mungo.mdesk.dao.mapper.CountryMapper">

    <!--<cache type="com.wooyoo.learning.util.RedisCache"/>-->

    <select id="select" resultType="com.mungo.mdesk.domain.Country">
        SELECT * FROM Country WHERE code = #{code} LIMIT 1
    </select>

</mapper>

CountryMapper.java

package com.mungo.mdesk.dao.mapper;

import com.mungo.mdesk.domain.Country;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * Created by wangxx on 2017/6/1.
 */
@Mapper
public interface CountryMapper {
    Country select(@Param("code") String code);

}

Country.java

package com.mungo.mdesk.domain;

/**
 * Created by wangxx on 2017/6/27.
 */
public class Country {
    public String code;
    public String name;
    public String continent;
    public String region;
    public String surfaceArea;
    public String indepYear;
    public String population;
    public String lifeExpectancy;
    public String gNP;
    public String gNPOld;
    public String localName;
    public String governmentForm;
    public String headOfState;
    public String capital;
    public String code2;

    ....getter/setter
  }

Spring Boot启动代码

SpringBoot的启动方式,不用多说,main()方法直接启动,工程创建后会有一个**Application.java的文件,里面就一个main方法,类一般有@SpringBootApplication。
下面定义一个简单的Controller测试请求。

CountryController.java

package com.mungo.mdesk.controller;

import com.mungo.mdesk.dao.mapper.CountryMapper;
import com.mungo.mdesk.dao.mapper.ProductMapper;
import com.mungo.mdesk.domain.Country;
import com.mungo.mdesk.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by wangxx on 2017/6/1.
 */
@RestController
@RequestMapping("/country")
public class CountryController {
    @Autowired
    private CountryMapper countryMapper;

    @GetMapping("/{code}")
    public Country getProductInfo(@PathVariable("code") String code) {
        // TODO
        Country country = countryMapper.select(code);
        return country;
    }
}

很简单,定义一个/country路由,后面通过url传递参数,通过解析传过来的参数,查询数据里相关国家信息并直接以json返回。因为使用了@RestController注解。若要返回页面使用@Controller注解即可。

至此,Spring Boot+Mybatis+Gradle环境搭建完成。整个项目结构如下图:
这里写图片描述

访问结果:
http://localhost:8080/country/CHN
这里写图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值