Spring-Boot初体验
基础配置
-
环境配置
- java环境>>>1.8
- maven环境>>>3.5
- IDE编辑器>>>Idea2015
-
创建boot项目方式
- idea创建 >>> 选择springinitiallizr 创建
- spring官网创建.>>> start.spring.io
-
启动方式
- main方法启动
- 密令行启动
- 进入项目的文件夹
- 执行: mvn spring-boot:run
- 找到相应jar文件 执行java -jar 相应的文件名启动.....(我觉得不好用...)
-
相关配置在resource文件夹下的application.properties文件
-
服务相关信息,如端口,路径前缀的设置:
server.port=8081 //修改默认端口号
server.context-path=/prepath //访问路径前缀 -
服务相关信息推荐使用yml文件格式 ,Idea 也对相应的文件做了支持,推荐使用yml格式
yml格式如下: ( yml有自己的语法,:冒号后跟一个空格 )
yuml文件 >>>>>>>>>>>>>>>>>>> server: port: 8082 context-path: /pre
-
在配置文件中配置属性并调用
-
在配置中声明属性
-
在@RestController标注的类中调用配置信息
-
如果是简单的数据
yuml文件 >>>>>>>>>>>>>>>>>>> cupSize: B
@Value("${cupSize}") private String size;
-
Javabean数据类型
yuml文件 >>>>>>>>>>>>>>>>>>> boy: name: "张三" age: 18
@Autowired private Boy boy ; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String ss() { System.out.println(boy); return "Hello , boot !"; }
@Repository // 声明bean @ConfigurationProperties(prefix = "boy") public class Boy { private String name; private Integer age; ....封装略 }
-
-
处理多环境配置
创建多套配置环境后如何修改环境?
application.yml
application-dev.yml
application-product.yml
在application.yml文件中修改 spring: profiles: active: "product" //选择的环境 双引号加 - 后边的字符串内容
-
-
Controller 使用
-
RestController //spring 4 后出来的一个组合注解 ,原来返回json需要@Controller+@RequestBody
-
Controller
-
RequestMapping
GetMapping (value= " / path " ) //组合注解
@RequestMapping(value = "/hello1/{id}",method = RequestMethod.GET) @GetMapping(value = "/hello1/{id}") public String sss(@PathVariable("id") Integer id) { return "id : "+id ; } @PathVariable("id") 类似于 @RequestParam("id")
-
代码实现
-
-
springboot操作数据库
-
SpringBoot-Data-Jpa=====>>>>MySql
关于SpringBoot-Data-Jpa>>> 定义一些列对象持久化标准, 目前实现这一规范的产品有Hibernate,toplink等等
-
相应的配置
application.yml文件>>>>>> spring: profiles: active: "product" datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///test username: root password: mysql jpa: hibernate: ddl-auto: update show-sql: true
-
实体类:
@Entity public class Student { @Id @GeneratedValue private Integer id; private String name; private String pwd; private Integer age; ...... }
-
dao层
import java.util.List; @Repository public interface StudentDao extends JpaRepository<Student , Integer > { //扩展接口 根据年龄来操作数据库 public List<Student> getStuListByAge(Integer age); }
-
Service层
@Service public class StudentService { @Autowired private StudentDao studentDao ; @Transactional //声明事务 public void saveTwo () { Student student = new Student("刘邦","1234"); studentDao.save(student); Student student1 = new Student("项羽","1234"); studentDao.save(student1); throw new RuntimeException("newError"); } }
-
controller层
事务处理测试>>>>>>>>>>>>>>>>>.. @RestController @RequestMapping("/stu") public class StudentController {
-
-
@Autowired
private StudentService studentService;
@PostMapping("/sav")
public void saveTwoTest () {
studentService.saveTwo();
}
}
```
增删改查初学>>>>>>>>>>>
```
@Autowired
private Boy boy ;
@Autowired
private StudentDao studentDao;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String ss() {
return "index";
}
//@RequestMapping(value = "/hello1/{id}",method = RequestMethod.GET)
@GetMapping(value = "/hello1/{id}")
public String sss(@PathVariable("id") Integer id) {
return "id : "+id ;
}
//查询
@GetMapping(value = "/gets")
public List<Student> getlist(){
return studentDao.findAll();
}
//增加
@GetMapping(value = "/add")
public Student getlist(String name, String pwd){
return studentDao.save(new Student(name,pwd));
}
//根据id查询
@GetMapping(value = "/get/{id}")
public Student getStuById (@PathVariable("id") Integer id ) {
return studentDao.findOne(id) ;
}
//根据id更新
@PutMapping(value = "/up/{id}")
public Student upStuById (@PathVariable("id") Integer id ) {
Student student = new Student();
student.setId(id);
student.setName("ceshi");
student.setPwd("123");
return studentDao.save(student);
}
//根据id删除
@DeleteMapping(value = "/del/{id}")
public void delStuById ( @PathVariable ("id") Integer id ) {
studentDao.delete(id);
}
//根据age查询
@GetMapping(value = "/age/{age}")
public List<Student> getStuByAge (@PathVariable("age") Integer age ) {
return studentDao.getStuListByAge(age);
}
```
6. web层: 此处未做jsp页面......
使用postman测试.........
注意: PutMapping 在postman中应在body中选择 x-www-form-urlencodde 方式
网友评论