美文网首页Springboot我爱编程
SpringBoot实战系列(一)-SpringBoot 2.0

SpringBoot实战系列(一)-SpringBoot 2.0

作者: 以微笑开始 | 来源:发表于2018-04-15 11:26 被阅读59次

    SpringBoot实战系列-SpringBoot 2.0 基础环境搭建

    1. SpringBoot 2.0 是什么?

    Spring Boot是由Pivotal团队提供的基于Spring的框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。SpringBoot 提供了一些非功能性特性,如嵌入式服务器、安全、健康检查、外部化配置等等。目前SpringBoot最新版本为2.0

    2. 获取SpringBoot

    使用Maven构建工具获取SpringBoot,在Maven的POM.xml文件中添加以下代码

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    

    编写启动类

    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
    @EnableAutoConfiguration
    public class Application {
        
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    编写Controller

    @RestController
    public class TestController {
    
        @GetMapping(value = "test")
        public String test(){
            return "test";
        }
    }
    

    在浏览器中输入http://localhost:8080/test,在浏览器中可以看到test文本

    3.集成Mybatis

    引入mybatis-spring-boot-starter,以及数据库连接池和MySQL驱动

       <dependency>
           <groupId>org.mybatis.spring.boot</groupId>
           <artifactId>mybatis-spring-boot-starter</artifactId>
           <version>1.3.2</version>
        </dependency>
    
       <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
       </dependency>
    
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.8</version>
      </dependency>     
    

    在resources目录下添加application.properties

    server.port=8080
    
    
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT&amp;useSSL=false&amp;autoReconnect=true&amp;failOverReadOnly=false
    spring.datasource.username=username
    spring.datasource.password=password
    
    spring.datasource.initialSize=5
    spring.datasource.minIdle=5
    spring.datasource.maxActive=20
    
    spring.datasource.maxWait=60000
    
    spring.datasource.timeBetweenEvictionRunsMillis=60000
    spring.datasource.minEvictableIdleTimeMillis=300000
    spring.datasource.validationQuery=SELECT 1 FROM DUAL
    spring.datasource.testWhileIdle=true
    spring.datasource.testOnBorrow=false
    spring.datasource.testOnReturn=false
    spring.datasource.poolPreparedStatements=true
    spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
    spring.datasource.filters=stat,wall,log4j
    spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    
    #扫描Mybatis SQL文件
    mybatis.mapper-locations=classpath:/mapper/*Mapper.xml
    

    编写Mapper接口和SQL文件

    @Mapper
    public interface TestMapper {
        /**
         * 返回数据库版本号
         * @return
         */
        String version();
    }
    
    
    <?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="me.smile.practice.mapper.TestMapper" >
    
        <select id="version" resultType="java.lang.String">
            select version()
        </select>
    
    </mapper>
    

    在test目录下通过编写单元测试检测配置是否正确

    @SpringBootTest(classes = App.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    public class TestMapperTest {
    
        @Resource
        private TestMapper testMapper;
    
        @Test
        public void version() {
    
            String version = testMapper.version();
            System.out.println(version);
            Assert.assertEquals("5.7.21", version);
        }
    }
    

    运行单元测试会在控制台输出当前数据库所使用的版本号。
    至此基于SpringBoot2.0的基本开发环境搭建完毕。

    相关文章

      网友评论

        本文标题:SpringBoot实战系列(一)-SpringBoot 2.0

        本文链接:https://www.haomeiwen.com/subject/skochftx.html