Spring Boot JdbcTemplate MySQL

1、配置pom.xml,增加jdbc、HikariCP连接池、MySQL驱动。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
  <groupId>com.zaxxer</groupId>
  <artifactId>HikariCP</artifactId>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

2、DataSourceConfig.java 阅读全文

Spring Boot @Retention

@Retention(RetentionPolicy.RUNTIME)
@Retention(保留)注解按照生命周期划分可以分为3类:
1、RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃。
2、RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃。默认的生命周期。
3、RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,在jvm加载class文件之后,仍然存在。

Spring Boot @Controller @RestController @RequestMapping @ResponseBody

package com.idodo.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn 
 * Project: cts 
 * Module Name: HelloController
 * Comments: 控制器
 * JDK version used: JDK1.8 
 * Author: jzh 
 * Create Date: 2018-12-12
 * Modified By: jzh 
 * Modified Date: 2018-12-12
 * Why & What is modified:
 * Version: <1.0>
 */

@Controller
public class HelloController {
  
  /**
   * @Controller     Spring MVC注解,表示此类用于负责处理Web请求
   * @RequestMapping Spring MVC注解,表示如果请求路径匹配,被注解的方法将被调用
   * @ResponseBody   表示此方法返回的是文本而不是视图名称
   */
  
  @RequestMapping("/hello")
  public @ResponseBody String say(String name) {
    return "hello " + name;
  }
}
package com.idodo.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn 
 * Project: cts 
 * Module Name: LoginController
 * Comments: 控制器
 * JDK version used: JDK1.8 
 * Author: jzh 
 * Create Date: 2018-12-15
 * Modified By: jzh 
 * Modified Date: 2018-12-15
 * Why & What is modified:
 * Version: <1.0>
 */

@RestController
public class LoginController {

  /**
   * @RestController 相当于 @Controller 和 @ResponseBody
   * @RequestMapping Spring MVC注解,表示如果请求路径匹配,被注解的方法将被调用
   */
  
  @RequestMapping("/login")
  public String say(String name) {
    return "username: " + name;
  }
}