spring boot操作数据库
1、pom文件
<!--数据库支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.4.0</version> </dependency>
2、application.yml
spring: profiles: active: dev datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@127.0.0.1:1521:test username: test password: root jpa: hibernate: ddl-auto: create show-sql: true
ddl-auto: update(有就插入,没有就创建) create(服务启动创建) create-drop(启动时创建,停止是删掉表结构)
none(什么也不做) validate(验证类里面的属性和表结构是否一致,不一致报错)
3.实体类girl
@Entity public class Girl { @Id private Integer id; private String cupSize; private Integer age; public Girl() { }
spring boot 会根据实体类创建对应的表
4 使用sping jpa 完成增删改查操作
findByAge(自定义方法命名有规范)
public interface GirlRepository extends JpaRepository<Girl,Integer> { //拓展 通过年龄来查询 public List<Girl> findByAge(Integer age); }
a、127.0.0.1:8081/girl/1
/* 查询一个女孩 */ @GetMapping(value = "/girl/{id}") public Girl selectGirl(@PathVariable("id")Integer id){ Girl save = girlRepository.findOne(id); return save; } /*
b、127.0.0.1:8081/girl/age/22
/* 自己修改查询的条件(按年龄查询) */ @GetMapping(value = "/girl/age/{age}") public List<Girl> selectGirlByAge(@PathVariable("age")Integer age){ girlRepository.findByAge(age); return girlRepository.findByAge(age); }
c、新增
/* 新增一个女生 */ @PostMapping(value="/girl") public Girl addGirl(@RequestParam("cupSize")String cupSize,@RequestParam("age") Integer age,@RequestParam("id")Integer id){ Girl girl =new Girl(); girl.setAge(age); girl.setCupSize(cupSize); girl.setId(id); Girl save = girlRepository.save(girl); return save; }
e、127.0.0.1:8081/girl
/* 查询女生列表 */ @GetMapping(value = "/girl") public List<Girl> girlList(){ return girlRepository.findAll(); }
f、修改
/* 修改有个女孩 */ @PutMapping (value = "/girl/{id}") public Girl updateGirl(@RequestParam("cupSize")String cupSize,@RequestParam("age") Integer age,@PathVariable("id")Integer id){ Girl girl =new Girl(); girl.setId(id); girl.setCupSize(cupSize); girl.setAge(age); Girl save = girlRepository.save(girl); return save; }
d、127.0.0.1:8081/girl/3
/* 删除一个女孩 */ @DeleteMapping(value = "/girl/{id}") public void deletGirl(@PathVariable("id")Integer id){ Girl girl =new Girl(); girl.setId(id); girlRepository.delete(girl); }
注意的地方在映射时一定要注意PathVariable和RequestParam
PathVariable用于在url接收参数,RequestParam用于在body中接收参数
5 spring boot 事务管理
@Transactional
@RestController public class GirlController { @Autowired private GirlRepository girlRepository; @Autowired private GirlService girlService; /* 事务管理 */ @PostMapping (value = "/girl/insertMany") public void insertMany(){ girlService.insertMany(); } }
@Service public class GirlService { @Autowired private GirlRepository girlRepository; @Transactional public void insertMany(){ Girl a=new Girl(); a.setAge(17); a.setCupSize("D"); a.setId(4); girlRepository.save(a); Girl b=new Girl(); b.setCupSize("GGGG"); b.setId(5); b.setAge(23); girlRepository.save(b); } }