1、项目启动方式
第一种启动方式:直接用编辑器启动
第二种启动方式:mvn spring_boot run
第三种启动方式: mvn install (先编译项目,targe 生成 jar)
java -jar project.jar
后两种启动方式都需先进入到项目目录上再运行命令。
示例代码
写个简单的Java类,用于输出!
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say(){
return "Hello Spring Boot!!";
}
}
@RestController(-Spring4 之后新加的注解-) 等同于 @Controller(-处理http请求-)+@ResponseBody
@RequestMapping(value="/hello",method=RequestMethod.GET)(-配置url映射-)等同于@GetMapping(value="/hello")
同理还有@PostMapping,@PutMapping,@DeleteMapping
配置文件代码
application.properties
server.port=8081
server.context-path=/springboot
application.yml
server:
port: 8081
context-path: /springboot
两种配置文件都可以使用,yml的代码更简洁推荐使用。特别注意:yml配置时,属性与属性值之间要用空格隔开!
如果配置文件中有自定义的属性和属性值,需要用@Value注入
例:
application.yml
hxAge: 23
注入使用:
@Value("${hxAge}")
private Integer age;
配置文件不需要配置参数类型,只需要在注入时配置即可。
配置文件中属性的相互调用:
hxAge: 23
content: "hx age is ${hxAge}."
当某个对象的属性很多时,这时候@Value就会变得比较不方便,所以可以使用增加前缀的方式进行,属性归类注入。
man:
age: 23
name: hx
然后创建一个ManProperties.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "man")
public class ManProperties {
private String name;
private Integer age;
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;
}
}
@ConfigurationProperties(prefix = “man”)可以把配置文件里的man下面的属性映射过来
@Component用于注入,这样才可被@Autowired获取到。
使用:
import com.hx.ManProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private ManProperties manProperties;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return manProperties.getName()+manProperties.getAge();
}
}
@Value:可以注入一个配置项,引用容器的一个bean方法
@Component:把普通pojo实例化到spring容器中,相当于配置文件中的
@ConfigurationProperties:把同类的配置信息自动封装成实体类
当在不同环境中需要使用到不同配置时解决方法
application.yml
spring:
profiles:
active: a
application-a.yml
server:
port: 8081
context-path: /springboot
man:
name: hx
age: 23
application-b.yml
server:
port: 8081
context-path: /springboot
man:
name: hzy
age: 20
命令方式启动:java -jar target/springboot-0.0.1-SNAPSHOT.jar --spring.profiles.active=a
@PathVarible RESTful风格url 获取参数
@RequestParam 获取Post里的参数 例如 127.0.0.1:8081/hello/say?id=10
SpringBoot数据库操作
JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
spring-data-jpa 是spring对hibernate的整合
pom.xml加入JPA和MySQL的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置数据库
application.yml
spring:
profiles:
active: a
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_man
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
ddl-auto属性:
create 每次调用都会创建一个新表
update 只是更新不覆盖
创建实体类
Man.java
package com.hx.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Man {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;
//必须要有构造函数
public Man() {
}
//set/get方法
}
此处知识点是关于Hibernate的。
。增删改查
ManController.java
/**
* 添加一个人员
*
* @param name
* @param age
* @return
*/
@PostMapping(value = "/man")
public Man manAdd(@RequestParam("name") String name,
@RequestParam("age") Integer age) {
Man man = new Man();
man.setName(name);
man.setAge(age);
return manRepository.save(man);
}
/**
* 查询一个人员
*
* @param id
* @return
*/
@GetMapping(value = "/man/{id}")
public Man manFindOne(@PathVariable("id") Integer id) {
return manRepository.findOne(id);
}
/**
* 删除一个人员
*
* @param id
*/
@DeleteMapping(value = "/man/{id}")
public void manDelete(@PathVariable("id") Integer id) {
manRepository.delete(id);
}
/**
* 更新一个人员
*
* @param id
* @param name
* @param age
* @return
*/
@PutMapping(value = "/man/{id}")
public Man manUpdate(@PathVariable("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age) {
Man man = new Man();
man.setId(id);
man.setName(name);
man.setAge(age);
return manRepository.save(man);
}
非id查询
在ManRepository增加一个方法findByAge(Integer age)
public interface ManRepository extends JpaRepository<Man,Integer> {
/**
* 通过年龄来查询
* 方法名固定findByAge
* @param age
* @return
*/
public List<Man> findByAge(Integer age);
}
在ManController中加入相应的查询方法
/**
* 通过年龄来查询
* @param age
* @return
*/
@GetMapping(value = "/man/age/{age}")
public List<Man> manListByAge(@PathVariable("age") Integer age) {
return manRepository.findByAge(age);
}
事务管理
两条 sql 语句同时在一个方法中执行,为了防止一个 sql 语句执行成功而另一个 sql 语句执行失败,引入了事务管理,需要在方法上加 @Transactional事务注解
事务确保了数据库数据的完整性和一致性
ManService.java
package com.hx.service;
import com.hx.dao.ManRepository;
import com.hx.entity.Man;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Service
public class ManService {
@Autowired
private ManRepository manRepository;
/**
* 事务管理测试
* 两条数据同时成功,或者同时不成功
* 保证数据库数据的完整性和一致性
*/
@Transactional
public void insertTwo(){
Man manA = new Man();
manA.setName("罗征");
manA.setAge(19);
manRepository.save(manA);
System.out.print(1/0);
Man manB = new Man();
manB.setName("昊天");
manB.setAge(25);
manRepository.save(manB);
}
}
在ManController中测试
@Autowired
private ManService manService;
/**
* 事务测试
*/
@PostMapping("/man/two")
public void manTwo(){
manService.insertTwo();
}
重新运行项目,执行请求 post方式http://localhost:8081/springboot/man/two
数据库并没有添加第一条数据,说明存在事务管理