Spring Boot 静态资源处理
spring boot项目如果要展示静态页面,可以把html、js、css等文件放在resources目录下的static或public目录里面(如果没有可以直接创建)。
Html测试
js测试
css测试
Spring Boot – data-jpa
1、添加依赖
<!--连接数据库 需要使用mysql驱动做测试 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<!--使用spring boot 开发data-jpa项目的基础依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2、编写实体对象,添加注解
3、书写配置 在application.yml添加如下配置
#连接池的配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?characterEncoding=utf-8
username: root
password: root
jpa:
hibernate:
ddl-auto: update #ddl-auto: create 每次都创建表,若存在则先删除 update 表不存在则创建,有更新字段才重新创建
4、启动项目会发现数据库中新增了一个表seller
Spring-data-jpa自动创建的,因为有配置ddl-auto: update
5、编写dao
只需要写接口并继承JpaRepository即可即可,不需要写实现类
6、编写controller,注入repository
package com.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.dao.SellerRepository;
import com.springboot.domain.Seller;
@RestController
public class SellerController {
@Autowired
private SellerRepository sellerRepository;
@RequestMapping("/addSeller")
public String addSeller(Seller seller) {
sellerRepository.save(seller);
return "OK";
}
@RequestMapping("/sellerList")
public List<Seller> sellerList() {
return sellerRepository.findAll();
}
@RequestMapping("/updateSeller")
public String updateSeller(Seller seller) {
sellerRepository.save(seller);
return "OK";
}
@RequestMapping("/deleteSeller")
public String deleteSeller(Integer id) {
sellerRepository.delete(id);
return "OK";
}
}
7、测试
http://localhost:8088/addSeller?sellerName=alibaba&age=18 添加成功
http://localhost:8088/sellerList 查询列表
http://localhost:8088/updateSeller?id=5&sellerName=alibaba5&age=25 更新成功
http://localhost:8088/deleteSeller?id=5 删除成功
接口定义方法规则 fingBy{条件属性首字母大写}
package com.springboot.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.springboot.domain.Seller;
public interface SellerRepository extends JpaRepository<Seller, Integer> {
Seller findById(Integer id);//不需要写实现方法,只需要按照规则编写方法名称
}
Controller中添加条件查询
@RequestMapping("/findSeller")
public Seller findSeller(Integer id) {
return sellerRepository.findById(id);
}
根据条件查询
http://localhost:8088/findSeller?id=2
来源:oschina
链接:https://my.oschina.net/u/4331949/blog/3643668