springboot2 - 快速入门

前往码云下载源码:

https://gitee.com/seaboot/demo-admin.git

工程目录

main
-- java:java代码存放目录
-- resources:文件资源存放目录
    |-- static:静态资源存放目录(js、css、html,不做任何配置即可访问)
    |-- templates:页面模版,freemaker、beetl、thymeleaf、velocity 等(没有强制要求,按需调整)
    |-- mapping:mybatis 配置文件存放目录(没有强制要求,可按需调整)
    |-- application.yml:系统参数配置文件
-- webapp:正常是不需要这个目录的,如果需要仍然可以启用

pom.xml:maven 配置
test:代码单元测试

SpringBoot 程序入口

@SpringBootApplication: 标注程序入口;
@MapperScan:声明 mybatis 接口的位置,方便程序扫描 DAO。

mybatis 扫描包的机制是个谜,永远不确定 @MapperScan、@Mapper 和 @Repository 这三个注解该如何组合。
当前项目只需要 @MapperScan 和 @Repository,有些项目只需要 @Repository 注解,根据实际情况调整。

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * MapperScan mybatis 扫描包配置
 */
@SpringBootApplication
@MapperScan("cn.demo.admin")
public class AdminApplication {

    /**
     * 程序总入口
     * @param args -
     */
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}

模版引擎

在前后端开发分离的今天,模版引擎存在感确实不高。

templates 目录主要用于存放动态页面,功能类似于JSP,SpringBoot 默认使用的是 thymeleaf,
我用的是 freeMarker,maven 和 yml 在后续给出。

静态界面

在 resources 目录下,新建一个 static 目录,就可以放置 html 脚本了。
springboot 环境下,不需要做任何配置就能访问。

虽说现在是前后端分离的,有时候项目部署有端口限制,不允许前后端分开部署,
这时候可以将编译之后的前端界面,放到 static 目录下,效果就跟传统项目一样了。

如果你的配置和我一致,点开链接即可:http://localhost:8088/bao/index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  <p> demo </p>
</body>
</html>

单元测试

import cn.demo.admin.example.dao.DebugDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * Created by 12614 on 2018/4/27.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdminApplication.class)
public class TestAPI {

    @Resource
    private DebugDao dao;

    @Test
    public void doTest() {
        System.out.println(dao.select("SELECT * FROM `sys_user`"));
    }
}

CURD 功能

使用的是 mybatis,用于测试,直接写一个能执行任意 SQL 的 DAO,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.demo.admin.example.dao.DebugDao">
    <select id="select" parameterType="String" resultType="java.util.HashMap">
        ${value}
    </select>
</mapper>

DAO

import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author Mr.css
 * @version 2023-05-10 17:06
 */
@Repository
public interface DebugDao {

    /**
     * 执行任意 sql
     *
     * idea 环境下,如果出现 @MapKey 报错,不管它
     *
     * @param sql sql
     * @return list
     */
    List<Map<String, Object>> select(String sql);
}

Service

import cn.demo.admin.example.dao.DebugDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.css
 * @version 2023-05-10 17:23
 */
@Service
public class DebugService {

    @Resource
    private DebugDao dao;

    public List<Map<String, Object>> query(){
        return dao.select("SELECT 'field1', 'field2'");
    }
}

Controller

import cn.demo.admin.example.service.DebugService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.css
 * @version 2023-05-10 10:46
 */
@Controller
@RequestMapping("debug")
public class ExampleCtrl {

    @Resource
    private DebugService service;

    @ResponseBody
    @RequestMapping("data")
    public List<Map<String, Object>> get() {
        return service.query();
    }
}

maven依赖

<?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 https://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.1.18.RELEASE</version>
    </parent>

    <groupId>cn.seaboot</groupId>
    <artifactId>demo-admin</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>demo-admin</name>
    <description>demo-admin</description>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <log4j.version>2.5</log4j.version>

        <spring.version>5.1.19.RELEASE</spring.version>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--Starter-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--AOP-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!--Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </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>

        <!--使用 @ConfigurationProperties 的情况下可以阻止 idea 报错,只是其辅助作用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!--WebSocket-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

        <!--模版引擎-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!--数据库-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
        </dependency>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <optional>true</optional>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

yml 配置

#启动端口
server:
  port: 8088
  servlet:
    context-path: '/bao'

spring:
  datasource:
    name: test
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/med?useUnicode=true&amp;characterEncoding=utf8&amp;allowMultiQueries=true&amp;autoReconnect=true&amp;failOverReadOnly=false
    username: root
    password: root
    druid:
      initialSize: 5
      minIdle: 5
      maxActive: 10
      maxWait: 60000
      filters: stat
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: select 'x'
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxOpenPreparedStatements: 20

  #模版引擎
  freemarker:
    template-loader-path: classpath:/templates
    suffix: .ftl
    expose-request-attributes: true
    request-context-attribute: request

# 持久层框架 mybatis
mybatis:
  mapper-locations: classpath:mapping/*.xml

#分页工具
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

posted on 2018-05-27 01:48  疯狂的妞妞  阅读(1464)  评论(0编辑  收藏  举报

导航