SpringBoot使用总结
来源:互联网 发布:二次元测量仪软件模拟 编辑:程序博客网 时间:2024/06/03 14:53
工具:idea
注册idea:http://idea.lanyus.com/
一、创建第一个springboot
二、编写第一个springboot
启动springboot
测试是否成功
除了这种启动方式SpringBoot还有两种启动方式
第一种:
1.用cmd进入到项目目录下
2.输入:mvn spring-boot:run
第二种:
1.先进入到项目目录下
2.编译下项目 mvn install
3.进入到target目录下会发现多出一个项目的.jar包
4.使用java命令启动:java -jar 项目名.jar
三、属性配置
基础配置:
#配置端口server.port=8081#配置基础路径:访问时为localhost:8081/girl/..server.context-path=/girl#自定义配置cupSize=Bage=18#配置里面使用配置content="cupSize=${cupSize} and age=${age}"#映射到实体中girl.cupSize=Agirl.age=20
package com.lei.controller;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "girl")public class GirlProperties { private String cupSize; private String age; public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public String getAge() { return age; } public void setAge(String age) { this.age = age; }}使用配置
package com.lei.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController { //读取配置里面的信息 @Value("${cupSize}") private String cupSize; @Autowired private GirlProperties girlProperties; @RequestMapping(value = "/say",method = RequestMethod.GET) public String say(){ return "Hello Spring Boot"; } @RequestMapping(value = "/cupSize",method = RequestMethod.GET) public String getCupSize(){ return cupSize; } @RequestMapping(value = "/getGrilSize",method = RequestMethod.GET) public String getGirlCupSize(){ return girlProperties.getCupSize(); }}
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>
四、Controller的使用
1)@Controller的使用
这个注解要配合模板使用,就相当于SpringMVC的视图解析器,最后返回的值会将其对应的HTML页面返回
要加入下面这个包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class thymeleafController { @RequestMapping(value = "/toIndex",method = RequestMethod.GET) public String toIndex(){ return "index"; }}
2)设置多个访问路径
@RequestMapping(value ={"/say","/hi"},method = RequestMethod.GET) public String say(){ return "Hello Spring Boot"; }
3)@RequestParam的使用
@RequestMapping(value ={"/say","/hi"},method = RequestMethod.GET) public String say(@RequestParam(value = "name",required = false,defaultValue = "lei") String myNaem){ return myNaem; }接受参数name的值,required为false时则可以不传,默认为lei
4)@GetMapping的使用
@GetMapping("/say")
就等于@RequestMapping(value ="/say",method = RequestMethod.GET)
@PostMapping同理五、数据库操作
添加两个jar包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>
配置文件:
#配置数据库spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.data-username=rootspring.datasource.data-password=123456spring.jpa.show-sql=true#自动创建表(通过@Entity注解之后的实体,会自动在数据库中建立一个表spring.jpa.hibernate.ddl-auto=create
实体:
package com.lei.controller;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Girl { //@GeneratedValue自增 @Id @GeneratedValue private Integer id; private String name; private String cupSize; //一定要有个无参的构造方法 public Girl() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; }}
定义Girl的DAO层接口去操作数据库
package com.lei.controller;import org.springframework.data.jpa.repository.JpaRepository;/** * Girl 实体的Dao层接口 */public interface GirlRepository extends JpaRepository<Girl,Integer> {}
Girl的Controller层
package com.lei.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;@RestController@RequestMapping(value = "/girl")public class GirlController { @Autowired private GirlRepository girlRepository; /** * 查询全部女生 * @return */ @GetMapping("/getAll") public List<Girl> getAll(){ return girlRepository.findAll(); } /** * 添加一个女生 */ @PostMapping(value = "/addGirl") public Girl addGirl(String name,String cupSize){ Girl girl = new Girl(); girl.setName(name); girl.setCupSize(cupSize); return girlRepository.save(girl); } /** * 根据id查询一个女生 * @param id * @return */ @GetMapping(value = "/getGirl/{id}") public Girl getGirl(@PathVariable("id") Integer id){ return girlRepository.findOne(id); } /** * 更新 * @param girl * @return */ @PostMapping(value = "/updateGirl") public Girl updateGirl(Girl girl){ return girlRepository.save(girl); } /** * 删除一个女生 * @param id * delete 请求 */ @DeleteMapping(value = "/deleteGirl/{id}") public void deleteGirl(@PathVariable("id") Integer id){ girlRepository.delete(id); } /** * 根据名字查询 * @param name * @return */ @GetMapping(value = "/getGirl/name/{name}") public List<Girl> getGirlByName(@PathVariable("name") String name){ return girlRepository.findByName(name); }}
扩展接口package com.lei.controller;import org.springframework.data.jpa.repository.JpaRepository;import java.util.List;/** * Girl 实体的Dao层接口 */public interface GirlRepository extends JpaRepository<Girl,Integer> { //通过名字查询 public List<Girl> findByName(String name);}六、事务管理
事务管理是为了能让操作原子性,比如同时插入两条数据,要么两条同时插入,要么两者都不插入
package com.lei.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Servicepublic class GirlService { @Autowired private GirlRepository girlRepository; //事务注解 @Transactional public void insertTwo(){ Girl girlA = new Girl(); girlA.setName("小明"); girlA.setCupSize("C"); girlRepository.save(girlA); //如果girlB出现异常了,那么用事务就可以避免只插入girlA而没有插入girlB //用事务管理就可以让两者要么都插入,要么都不插入 Girl girlB = new Girl(); girlB.setName("小花"); girlB.setCupSize("EEEE"); girlRepository.save(girlB); }}
阅读全文
0 0
- SpringBoot使用总结
- springboot总结
- SpringBoot总结
- SpringBoot总结
- springboot使用
- SpringBoot使用Delete方式接收请求问题总结
- springboot使用Redis完整过程详解及常见问题总结
- springboot学习总结
- Springboot问题总结
- springboot常用注解总结
- SpringBoot 错误总结
- Springboot状态码总结
- Springboot项目开发总结
- springboot学习总结
- SpringBoot入门总结
- SpringBoot AutoConfigure学习总结
- BUI+Springboot,问题总结
- springboot笔记(2)springboot-Mybatis使用
- Eureka 源码解析 —— 任务批处理
- 求1+2+3+...100的和的递归和循环实现
- Linux下查看内核、CPU、内存及各组件版本的命令和方法
- ARIMA预测
- AEM 发布bundle过程中遇到的问题
- SpringBoot使用总结
- linux 编辑器 vim
- sql server中查询的值区分大小写
- 在窗口小部件里快速设置如何默认同步关闭
- WA恶之源
- Effective C++ 01
- 项目案例:二进制文件与十六进制字符串转化常用操作
- ZigBee中Profile(规范),Cluster(簇)
- 1*2*...100的积的递归和循环实现