第一步:通过ide创建springBoot项目
第二步:运行SpringBoot
在Application中启动项目,然后浏览器中输入:http://localhost:8080/
第三步:编写第一个请求
http://localhost:8080/hello
第四步:修改请求端口号,和配置
我们需要在 application.properties 文件下添加端口和路径
server.port=8081 server.context-path=/girl
这样再次访问该项目的时候,就使用 http://localhost:8081/girl/hello
第五步:使用配置文件中设置的参数值
在application.properties文件下添加cupSize参数
cupSize = B
在项目中使用:
@Value("${cupSize}") private String cupSize; @RequestMapping(value="/hello",method= RequestMethod.GET) public String say(){ return "Hello malei"+cupSize; }ok!
第六步:了解Controller的使用
@RestController = @Controller + @ ResponseBody
@Controller 处理http请求
@RestController 是spring4之后的新注解
@RequestMapping 配置url映射
第七步:当我们想要通过两个路径去访问一个接口的方式:
@RequestMapping(value={"/hello","/hi"},method= RequestMethod.GET) public String say(){ return "Hello malei "+cupSize+" "; }
http://localhost:8081/girl/hi
http://localhost:8081/girl/hello
这两个地址都可以使用。
第八步:使用POST方式请求
@RequestMapping(value="/helloByPost",method= RequestMethod.POST) public String helloByPost(){ return "Hello malei "+cupSize+"post "; }
第九步:get方式传值
@RequestMapping(value="/helloByPost/{id}",method= RequestMethod.GET) public String hello2(@PathVariable("id") Integer id){ return "Hello malei "+ id ; }
访问方式:http://localhost:8081/girl/helloByPost/3
第十步:get的传统访问方式
怎么通过 http://localhost:8081/girl/helloByPost?id=3 来访问呢?
@RequestMapping(value="/hello3",method= RequestMethod.GET) public String hello3(@RequestParam("id") Integer id){ return "Hello malei "+ id ; }
第十一步:现在是不是发现RequestMapping里面的东西太多了,想要简化下,可以使用如**解
@GETMapping
@GetMapping(value="/hello4") public String hello4(@RequestParam("id") Integer id){ return "Hello malei "+ id ; }
第十二步:现在让我们进入到最关键的部分,连接数据库
首先我们要在pom.xml文件引入两个插件
<!-- 数据库查找语句他都写好了 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- mysqlqu驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
接下来,我们需要在application.yml文件下来添加数据库配置,(备注:原来使用的是application.propretre,但是我删除了)
server: port: 8090 spring: datasource: url : jdbc:mysql://localhost:3306/malei username : root password : root driverClassName : com.mysql.jdbc.Driver jpa: database : MYSQL show-sql : true hibernate: ddl-auto : update naming-strategy : org.hibernate.cfg.ImprovedNamingStrategy properties: hibernate: dialect : org.hibernate.dialect.MySQL5Dialect
第十三步:项目和数据库已经连接完成了,现在要向数据库中添加表了
添加数据表,我们只需要创建一个实体类就可以了
@Entity @Table(name = "workers") public class GrilInfo { @Id @GeneratedValue private Integer id; private String cupSize; private Integer age; public Integer getId() { return id; } ...@Entity : 这个类是个实体bean
@Table : 数据表名称
@Id : 这是一个主键
@GeneratedValue : 生成一个唯一的标识的主键
第十四步:让我们开发一个接口,获取works中的所有数据,通过postman来获取
首先我们需要创建一个查询接口GirlRepository.class
public interface GirlRepository extends JpaRepository<GrilInfo,Integer>{ }然后我们需要创建一个Controller了
@RestController public class GirlController { @Autowired private GirlRepository girlRepository; @GetMapping(value = "/girls") public List<GrilInfo> getGirls(){ return girlRepository.findAll(); } }http://localhost:8090/girl/girls 完成!
@Autowired : 是spring提供的注解,用来进行依赖注入,将对象注入,可直接使用
第十五步:以下的步骤就是扩展了,我们要通过接口实现数据的增删改工作
增加一个女生:
/** * 添加一条女孩 * @return */ @PostMapping(value = "/addGirl") public GrilInfo addGirl(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){ GrilInfo info = new GrilInfo(); info.setCupSize(cupSize); info.setAge(age); return girlRepository.save(info); }
/** * 查询一个女生 */ @GetMapping(value = "/findGirl/{id}") public GrilInfo addGirl(@PathVariable("id") Integer id){ return girlRepository.findOne(id); }
/** * 更新一个女生 */ @PostMapping(value = "/updateGirl/{id}") public GrilInfo updateGirl(@PathVariable("id") Integer id, @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){ GrilInfo info = new GrilInfo(); info.setId(id); info.setCupSize(cupSize); info.setAge(age); return girlRepository.save(info); }
/** * 删除一个女生 */ @GetMapping(value = "/deleteGirl/{id}") public String deleteGirl(@PathVariable("id") Integer id){ girlRepository.delete(id); return "成功"; }
最后,jpa为我们提供了所有的增删改查的数据库语句实现,但是有时候可能不满足业务需要,我们需要通过年龄查询的时候。
public interface GirlRepository extends JpaRepository<GrilInfo,Integer>{ /** * 通过年龄查询 */ List<GrilInfo> findByAge(Integer age); }
/** * 通过年龄查询女生列表 */ @GetMapping(value = "/getGirlsByAge/{age}") public List<GrilInfo> getGirlsByAge(@PathVariable("age") Integer age){ return girlRepository.findByAge(age); }完成,真的是SpringBoot 让开发变的太简单了
第十六步:事物操作
当购买一个物品的时候,我们需要修改订单表和余额表,但是当一个表修改成功,另一个失败的时候,这就出现问题了,
必须应该保证,两表的同步,一个出错,另一个也不能修改,这个时候我们只要在方法上添加 @Transactional 就可以了。