關(guān)于
這篇文章骏令,介紹SpringBoot常用的配置和請(qǐng)求處理蔬捷。大致分成三個(gè)部分介紹:常用的請(qǐng)求方式、Jpa配置榔袋、MySql配置周拐。
常用的請(qǐng)求方式
如下是常用的幾種請(qǐng)求方式:
get請(qǐng)求:一般用于查詢數(shù)據(jù)铡俐,獲取一些非重要性的信息。
post請(qǐng)求:一般用于插入數(shù)據(jù)妥粟。
put請(qǐng)求:一般用于數(shù)據(jù)更新高蜂。
delete請(qǐng)求:一般用于數(shù)據(jù)刪除。
那么罕容,在SpringBoot中,怎么對(duì)這些請(qǐng)求方式進(jìn)行處理稿饰。(這里以最常用的get锦秒、post請(qǐng)求為例,其他的類似)
get請(qǐng)求:
//get請(qǐng)求喉镰,獲取url路徑上的參數(shù) @PathVariable
//注:localhost:8080/test/11/hans
@RequestMapping(value = "/test/{id}/{name}", method = RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id, @PathVariable("name") String name) {
return "id:" + id + " name:" + name;
}
//get請(qǐng)求旅择,獲取url請(qǐng)求參數(shù)的值 @RequestParam
//localhost:8080/test?id=99
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String sayHello(@RequestParam Integer id) {
return "id:" + id;
}
//get請(qǐng)求,獲取url請(qǐng)求參數(shù)的值侣姆,增加參數(shù)映射生真,默認(rèn)值 @RequestParam
//required=false 表示url中可以無id參數(shù),此時(shí)就使用默認(rèn)參數(shù)
@RequestMapping(value = "/test2", method = RequestMethod.GET)
public String sayHello2(@RequestParam(value = "id", required = false, defaultValue = "1") Integer id) {
return "id:" + id;
}
post請(qǐng)求:
//post請(qǐng)求
//表單參數(shù)
@RequestMapping(value= "/getMessage", method = RequestMethod.POST)
public String getMessage(int code, String message) {
return "success";
}
//post請(qǐng)求
//json raw參數(shù)
@PostMapping(value= "/getMessageBody")
public String getMessagePost(@RequestBody HolidayEntity bean) {
return "success";
}
//匹配參數(shù)
//password如果匹配對(duì)捺宗,@RequestParam不寫都o(jì)k
public void login(@RequestParam("account") String name, @RequestParam String password) {
System.out.println(name + ":" + password);
}
//@RequestHeader注解用來將請(qǐng)求頭的內(nèi)容綁定到方法參數(shù)上柱蟀。
@PostMapping(value = "login")
public void login2(@RequestHeader("access_token") String accessToken,@RequestParam String name) {
System.out.println("accessToken:" + accessToken);
}
補(bǔ)充:
組合注解(RequestMapping的變形)
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
Jpa配置
1.pom文件加入Jpa配置
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2.Application入口類增加@EnableJpaRepositories注解
@EnableJpaRepositories
public class Application extends SpringBootServletInitializer {
3.dao接口
@Repository
public interface HolidayRepository extends JpaRepository<HolidayEntity, Long> {
@Query(value = "SELECT p FROM HolidayEntity p")
List<HolidayEntity> queryHoliday();
}
4.entity類
@Entity
@Table(name = "holiday_scheme")
@EntityListeners(AuditingEntityListener.class)
public class HolidayEntity extends AbstractPersistable<Long> {
@Column(name = "date")
public String date;
@Column(name = "hour")
public String hour;
@Column(name = "holiday")
public String holiday;
@Column(name = "holiday_explain")
public String holiday_explain;
@Column(name = "type")
public String type;//SUB假期,ADD調(diào)休
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHour() {
return hour;
}
public void setHour(String hour) {
this.hour = hour;
}
public String getHoliday() {
return holiday;
}
public void setHoliday(String holiday) {
this.holiday = holiday;
}
public String getHoliday_explain() {
return holiday_explain;
}
public void setHoliday_explain(String holiday_explain) {
this.holiday_explain = holiday_explain;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "HolidayEntity{" +
"date='" + date + '\'' +
", hour='" + hour + '\'' +
", holiday='" + holiday + '\'' +
", holiday_explain='" + holiday_explain + '\'' +
", type='" + type + '\'' +
'}';
}
}
5.執(zhí)行蚜厉,獲取數(shù)據(jù)
@Autowired
private HolidayRepository holidayRepository;
@RequestMapping("/test")
@ResponseBody
public List<HolidayEntity> test() {
return holidayRepository.findAll();
}
MySql配置
1.pom文件加入MySql配置
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
2.application.properties文件配置MySql相關(guān)
# tomcat配置
server.port=8090
# 數(shù)據(jù)庫配置
#Mysql屬性配置文件,Spring-boot系統(tǒng)配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://*.*.*.*:3306/db?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
spring.datasource.username=***
spring.datasource.password=***
#配置自動(dòng)建表:updata:沒有表新建长已,有表更新操作,控制臺(tái)顯示建表語句
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
#如下的配置會(huì)導(dǎo)致報(bào)錯(cuò) Unable to build Hibernate SessionFactory
#spring.jpa.properties.hibernate.hbm2ddl.auto=validate