1.application.yml配置文件的使用,内容格式如下(注意.yml文件的属性值,冒号后面有一个空格):
aaa: 55
bbb: 66
#引用属性值
ccc: "aaa内容是:${aaa},bbb的内容是:${bbb}"
girl:
cupSize: B
age: 18
1.1 用标签读取:
@value("@{aaa}"}
private String aaa;
@value("@{ccc}")
private String ccc;
1.2 用对象读取:
@Component//实例化到spring容器中
@ConfigurationProperties(prefix = "girl")//读取配置文件内前缀为girl的的属性进行映射
public class GirlProperties {
private String cupSize;
private Integer age;
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
1.3 指定配置文件,如厂商环境下使用的是application-dev.yml配置,生产环境下使用的是application-prod.xml配置,
可在application.yml文件中,指定哪个配置文件起作用
spring:
profiles:
active: dev
profiles:
active: dev
这样指定起作用的是application-dev.yml这个配置文件
2 controller的使用
@Controller 处理http请求
@RestController Spring4之后新加的注解,原来返回json格式的数据需要@ResponseBody配合@Controller,现在用只用这个注解就可以实现
@RequestMapping 配置url映射
@PathVariable 获取url中的数据
@RequestParam 获取请求参数的值
@GetMapping 组合注解
2.1 一个方法配置两个访问路径
@RequestMapping(value={"/hello","/hi"},method=RequestMethod.GET)
2.2 获取url中的数据,如:http://localhost:8080/hello/say/666
@Requestcontroller
@RequestMaping("/hello")
public class HelloController{
@Autowired
private GirlProperties GirlProperties;
@RequestMapping(value="/say/{id}",method=RequestMethod.GET)
public String say(@PathVariable("id") Integer id){
return "id:"+id;
}
}
或者:http://localhost:8080/hello?id=666
@Requestcontroller
@RequestMaping("/hello")
public class HelloController{
@RequestMapping(value="/say",method=RequestMethod.GET)
public String say(@RequestParam("id") Integer myid){
return "id:"+ myid;
}
}
或者
@Requestcontroller
@RequestMaping("/hello")
public class HelloController{</pre><pre name="code" class="java"> @RequestMapping(value="/say",method=RequestMethod.GET)
public String say(@RequestParam(value="id",requird=false,defaultValue="0") Integer myid){
//required=false:非必填,defaultValue="0":默认值
return "id:"+ myid;
}
}
2.3 @GetMapping(value="/say")与
@RequestMapping(value="/say",method=RequestMethod.GET) 效果一样
3. 数据库操作
Spring-Data-Jpa:
JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate、TopLink等。
3.1 maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
3.2 application.yml添加配置:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1/mytest
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
ddl-auto:create每次服务启动时,都会创建与对象对应的表,如果表存在,则删除重新创建
ddl-auto:update每次服务启动时,都会创建与对象对应的表,如果表存在,则保留原来的表和数据
ddl-auto:create-drop服务停止时则删除与对象对应的表
ddl-auto:none什么都不做
ddl-auto:validate验证对象里面的属性与表结构是否一致,如果不一致则报错
3.3 创建对象
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
private String name;
public Girl() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
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;
}
}
public interface GirlRepository extends JpaRepository<Girl,Integer> {
//通过年龄查询,方法名需要按照格式来写
public List<Girl> findByAge(Integer age);
//根据名字模糊查询
@Query(value = "select * from girl g where name like %?1%",nativeQuery = true)
//nativeQuery本地查询,就是使用原生的sql语句(根据数据库的不同,在sql的语法或结构方面可能有所区别)进行查询数据库的操作。
public List<Girl> findByName(String Name );
}
3.5 创建服务层
package com.example.demo.service;
import com.example.demo.domain.Girl;
import com.example.demo.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by admin on 2017/12/20.
*/
@Service
public class GirlService {
@Autowired
private GirlRepository girlRepository;
@Transactional
public void insertTwo(){
Girl giralA=new Girl();
giralA.setCupSize("A");
giralA.setAge(17);
girlRepository.save(giralA);
Girl giralB=new Girl();
giralB.setCupSize("CCC");
giralB.setAge(18);
girlRepository.save(giralB);
}
}
package com.example.demo.controller;
import com.example.demo.domain.Girl;
import com.example.demo.repository.GirlRepository;
import com.example.demo.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
/**
* Created by admin on 2017/12/20.
*/
@RestController
public class GirlController {
@Autowired
private GirlRepository girlRepository;
@Autowired
private GirlService girlService;
/**
* 查询所有对象
* @return
*/
@GetMapping(value = "girls")
public List<Girl> girlList(){
return girlRepository.findAll();
}
/**
* 保存对象1
* @return
*/
@PostMapping(value = "/girls1")
public Girl girlAdd1(Girl girl){
girl.setAge(girl.getAge());
girl.setCupSize(girl.getCupSize());
return girlRepository.save(girl);
}
/**
* 保存对象2
* @return
*/
@PostMapping(value = "/girls2")
public Girl girlAdd2(@RequestParam("cupSize") String cupSize,@RequestParam("age") Integer age){
Girl girl=new Girl();
girl.setAge(girl.getAge());
girl.setCupSize(girl.getCupSize());
return girlRepository.save(girl);
}
/**
* 根据id查询一个对象
* @param id
* @return
*/
@GetMapping(value = "/girls/{id}")
public Girl girlFindOne(@PathVariable("id") Integer id){
return girlRepository.findOne(id);
}
/**
* 更新
*/
@PutMapping(value = "/girls/{id}")//PutMapping用于put请求
public Girl girlUpdata(@PathVariable("id") Integer id,@RequestParam("cupSize") String cupSize,@RequestParam("age") Integer age){
Girl girl=new Girl();
girl.setAge(age);
girl.setId(id);
girl.setCupSize(cupSize);
return girlRepository.save(girl);
}
//删除
@DeleteMapping(value = "/girls/{id}")
public void girlDelete(@PathVariable("id") Integer id){
girlRepository.delete(id);
}
//通过年龄查询
@GetMapping(value = "/girls/age/{age}")
public List<Girl> girlListByAge(@PathVariable("age") Integer age){
return girlRepository.findByAge(age);
}
@PostMapping(value = "/girls/two")
public void girlTwo(){
girlService.insertTwo();
}
@GetMapping(value = "girl/getbyname")
public List getgirlbyname(@Param("name") String name){
// return girlRepository.findByName("%"+name+"%");
return girlRepository.findByName(name);
}
}
4。事物管理
用标签@Trasactional