sts+mysql数据源_sts中MySQL数据库的连接和一些简单的操作
本文主要向大家介绍了sts中MySQL数据库的连接和一些简单的操作,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。1.1在sts中连接Mysql数据库是在application中写jdbc的连接spring:profiles:active:-devdatasource:driver-class-name:com.mysql.jdbc.Driverurl:jdbc:my...
本文主要向大家介绍了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 ListfindByAge(Integer age);
}
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代表把Man类中的信息的对象都在里面了
*/
public Listmanlist() {
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 ListmanListAge(@PathVariable("age") Integer age){
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数据库频道!
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)