摘要:本文主要向大家介绍了sts中MySQL数据库的连接和一些简单的操作,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。
本文主要向大家介绍了sts中MySQL数据库的连接和一些简单的操作,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。
1.1 在sts中连接Mysql数据库是在application中写jdbc的连接
spring:
profiles:
active:
- dev
datasource :
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/student
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
1.2对数据库进行增删查改
1.2.1:现在Model层里定义用户
package com.cy.coo.li;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Man {
@Id
@GeneratedValue
private Integer id;
private Integer age;
private String cupSize;
public Man(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
}
1.2.2:运用了一个JpaRepository<>的接口来实现对命名的规范
package com.cy.coo.li;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
public interface manInterface extends JpaRepository
//通过年龄查询,因为查出来很可能有很多个,所以使用集合
public List
}
1.2.3:在Dao层实现对数据库的增删查改
package com.cy.coo.li;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ManContent {
@Autowired
private manInterface manl;
@Autowired
private manService mans;
/*
* 查询所有人的数据
*/
@GetMapping(value = "/man")
/*
* List指的是集合.<>是泛型,里面指定了这个集合中存放的是什么数据. List
*/
public List
return manl.findAll();
}
/*
* 添加
*/
@PostMapping(value = "/man")
// @RequestParam获取参数
/*
* 因为save返回的时添加进去的对象,所以返回类型就是这个对象
*/
public Man manAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) {
Man man = new Man();
man.setCupSize(cupSize);
man.setAge(age);
return manl.save(man);
}
// 查询
@GetMapping(value = "/man/{id}")
//因为
public Man manFindOne(@PathVariable("id") Integer id) {
System.out.println(id);
return manl.findOne(id);
}
//更新
@PutMapping(value="/man{id}")
public Man manUpdata(@RequestParam(value = "id") Integer id,
@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){
Man manll=new Man();
manll.setId(id);
manll.setAge(age);
manll.setCupSize(cupSize);
return manl.save(manll);
}
//删除
@DeleteMapping(value="/man{id}")
public void manDelete(@RequestParam("id") Integer id){
//因为delete的返回值为null所以就是没得1返回值
manl.delete(id);
}
//通过年龄查询
@GetMapping(value="/man/age/{age}")
public List
return manl.findByAge(age);
}
@PostMapping(value="/man/two")
public void manTwo(){
mans.InsertTwo();
}
}
1.2.4service层里实现业务
package com.cy.coo.li;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class manService {
@Autowired
private manInterface manl;
public void InsertTwo(){
Man manA=new Man();
manA.setAge(21);
manA.setCupSize("F");
manl.save(manA);
Man manB=new Man();
manB.setAge(21);
manB.setCupSize("F");
manl.save(manB);
}
}
本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号