前置知识: maven, idea
最简易的项目及启动
1.idea快速新建项目
2.application.properties配置(非必需)
3.启动方式
1.idea新建项目
2.maven spring-boot:run
进入项目目录, 执行
maven spring-boot:run
3.maven编译后执行
maven install
cd target
java -jar ***.jar
配置文件的方式
不用
properties
的方式, 用yml
的形式
yml取值方式
单个值
server:
port: 8081
context-path: /demo
age: 10
name: mark
java代码
/**
* @author mengqa
**/
@RestController
public class DemoController {
@Value("${age}")
private Integer age;
@RequestMapping(method = RequestMethod.GET, value="hello")
public Integer hello() {
return age;
}
}
配置里再使用配置变量
server:
port: 8081
context-path: /demo
age: 10
name: mark
student: "age:${age}, name:${mark}"
java代码
@Value("${studentStr}")
private String studentStr;
@RequestMapping(method = RequestMethod.GET, value="hello")
public String hello() {
return studentStr;
}
对象
server:
port: 8081
context-path: /demo
student:
age: 10
name: mark
java代码
/**
参数Bean
* @author mengqa
**/
@Component
@ConfigurationProperties(prefix = "student")
public class StudentProperties {
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "StudentProperties{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
// ------------------------------------------
// Controller代码:
@Autowired
private StudentProperties studentProperties;
@RequestMapping(method = RequestMethod.GET, value="hello")
public String hello() {
return studentProperties.toString();
}
浏览器输出:StudentProperties{age=20, name='mark'}
生产环境和开发环境
分别定义生产和开发环境配置文件
application-prod.yml
server:
port: 8082
context-path: /demo
application-dev.yml
server:
port: 8081
context-path: /demo
配置主文件appliaction.yml
写上spring.profiles.active
spring:
profiles:
active: prod
代表读取生产配置文件
maven编译后执行也行:
java -jar ***.jar --spring.profiles.active=prod
Controller相关
@RequestMapping
多url映射
@RequestMapping(value={"/hello", "/test"}, method = RequestMethod.GET)
组合注解
@GetMapping
@PostMapping
@DeleteMapping...
@PathVariable
获取Url中的参数
http://localhost:8082/demo/show/213
@RequestMapping(method = RequestMethod.GET, value="show/{id}")
public String show(@PathVariable String id) {
return id;
}
@RequestParam
http://localhost:8082/demo/show?id=213
@RequestMapping(method = RequestMethod.GET, value="show/{id}")
public String show(@PathVariable String id) {
return id;
}
// 设默认值
@RequestMapping(method = RequestMethod.GET, value="show")
public String show(@RequestParam(value = "id", required = false, defaultValue = "000") String reId) {
return reId;
}
boot数据库操作
pom依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
配置中加入数据库配置
spring:
profiles:
active: prod
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/sc?useUnicode=true&characterEncoding=UTF-8
username: root
password: mqa
jpa:
hibernate:
ddl-auto: update
show-sql: true
# ddl-auto: update 表示自动建表(已经存在的表不会删除)
java准备
- 实体类
package com.example.demo.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.List;
/**
* @author mengqa
**/
@Entity
public class Student {
private String name;
private Integer age;
@Id
@GeneratedValue
private Integer id;
@Transient
private List<Score> scores;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<Score> getScores() {
return scores;
}
public void setScores(List<Score> scores) {
this.scores = scores;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
", scores=" + scores +
'}';
}
}
- 数据层继承JPA接口
public interface IStudentDao extends JpaRepository<Student, Integer> {
}
- Service暂时省略, 写Controller Api接口, 增删改查
/**
* @author mengqa
**/
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private IStudentDao studentDao;
@GetMapping("/list")
public List<Student> list() {
return studentDao.findAll();
}
@PostMapping("/save")
public Student save(@RequestBody Student student) {
return studentDao.save(student);
}
@GetMapping("/show/{id}")
public Student show(@PathVariable Integer id) {
return studentDao.findOne(id);
}
@PutMapping("/update")
public Student update(@RequestBody Student student) {
return studentDao.save(student);
}
@DeleteMapping("/del/{id}")
public void delete(@PathVariable Integer id) {
studentDao.delete(id);
}
}
- jpa扩展查询
public interface IStudentDao extends JpaRepository<Student, Integer> {
List<Student> findByNameAndAge(String name, Integer age);
}
- 事务控制通过 @Transactional-事务注解实现