快速入门SpringBoot2之环境搭建

“springboot”已经风靡很久了,身边还是有伙计没接触过,有的甚至不愿意接触新的框架,还停留在JAVA6、SSM、XML时代。其实框架的出现就是为了提高开发人员的效率,所以框架只会越来越容易使用。“springboot”极大简化了配置及部署,使开发人员可以更加快速构建应用、开发业务逻辑。

本文简单快速普及下基本环境搭建:bootstart、profiles、mybatis、controller、thymeleaf。

一、boot-starter

pom直接继承boot-parent:

<!-- Inherit defaults from Spring Boot -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
  </parent>

如pom已经继承了其他pom,可使用pom依赖管理:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

二、profiles

这里使用properties属性文件,application.properties中指定需要激活的配置文件:

spring.profiles.active=dev

在resources下添加指定的配置文件:

application-${env-name}.properties

三、mybatis

使用mybatis持久层框架,这里数据库使用oracle,连接池组件使用druid,springboot可以自动识别配置,pom需要引入oracle依赖,druid依赖包,相关属性添加到配置文件:

# mybatis
mybatis.type-aliases-package=com.xxxx.xxx
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000

# datasource
spring.datasource.url=jdbc:oracle:thin:@192.168.0.1:1521:test
spring.datasource.username=test
spring.datasource.password=123456
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive=20
spring.datasource.initialSize=10
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 'x' FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20

dao模块使用mapper注解,sql语句使用对应注解。假设有一个user对象,对应数据库表t_user,基本操作如下,将dao直接注入到service即可。

@Mapper
public interface UserDao {
  
  @Select("select * from t_user where id = #{id}")
  public User getById(@Param("id")int id  );

  @Insert("insert into t_user(id, name)values(#{id}, #{name})")
  public int insert(User user);
}

四、controller

pom引入web-starter依赖,controller还是那个controller,这里记录下几种获取参数的方式:

@RestController
@RequestMapping("hello")
public class HelloController {

  @RequestMapping(value="/say",method= RequestMethod.GET)
  //http://localhost:8080/hello/say
  public String say() {
    return "hello";
  }
  
  @RequestMapping(value="/sayHello/{name}",method= RequestMethod.GET)
  //http://localhost:8080/hello/sayHello/star
  public String sayHello(@PathVariable("name") String name) {
    return "hello " + name;
  }
  
  @GetMapping(value="/sayHi/{name}")
  //http://localhost:8080/hello/sayHi/star
  public String sayHi(@PathVariable("name") String name) {
    return "hi " + name;
  }
  
  @GetMapping(value="/sayNice")
  //http://localhost:8080/hello/sayNice?name=star
  public String sayNice(@RequestParam(value="name",required=true) String name) {
    return "nice " + name;
  }
}

五、thymeleaf

使用thymeleaf模板,pom需要引入thymeleaf依赖,使用th属性标签,将后台数据取出并展示:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'hello:'+${name}" ></p>
</body>
</html>

这样基本环境就搭建好了。喜欢也可以关注微信公众号:码农小麦

原创文章 17 获赞 13 访问量 7626

猜你喜欢

转载自blog.csdn.net/weixin_43275277/article/details/88525938