当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot 添加MySQL数据库及JPA实例

Spring Boot 添加MySQL数据库及JPA实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

最近在学习spring boot,继续前面的学习,这一次我们加入mysql数据库和jpa。

配置:

pom.xml文件

<!-- 添加mysql和jpa--> 
  <dependency> 
  <groupid>org.springframework.boot</groupid> 
  <artifactid>spring-boot-starter-data-jpa</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>mysql</groupid> 
   <artifactid>mysql-connector-java</artifactid> 
  </dependency> 

在application.properties(在resource文件夹下新建,进行配置)文件中添加数据进行配置:

spring.datasource.url = jdbc:mysql://localhost:3306/spring_boot 
spring.datasource.username = root 
spring.datasource.password = root 
spring.datasource.driverclassname = com.mysql.jdbc.driver 
 
 
# specify the dbms 
spring.jpa.database = mysql 
# show or not log for each sql query 
spring.jpa.show-sql = true 
# hibernate ddl auto (create, create-drop, update) 
spring.jpa.hibernate.ddl-auto = update 
# naming strategy 
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.improvednamingstrategy 
 
# stripped before adding them to the entity manager) 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.mysql5dialect 

user类

package com.seawater.bean; 
 
import javax.persistence.*; 
import javax.validation.constraints.notnull; 
 
/** 
 * created by zhouhs on 2016/12/30. 
 */ 
@entity 
@table(name = "user") 
public class user { 
 
 @id 
 @generatedvalue(strategy = generationtype.auto) 
 private long id; 
 private string name; 
 private int age; 
 
 public long getid() { 
  return id; 
 } 
 
 public void setid(long id) { 
  this.id = id; 
 } 
 
 public string getname() { 
  return name; 
 } 
 
 public void setname(string name) { 
  this.name = name; 
 } 
 
 public int getage() { 
  return age; 
 } 
 
 public void setage(int age) { 
  this.age = age; 
 } 
} 

usercontroller

package com.seawater.controller; 
import com.seawater.dao.userdao; 
import com.seawater.bean.user; 
import io.swagger.annotations.api; 
import io.swagger.annotations.apiimplicitparam; 
import io.swagger.annotations.apiimplicitparams; 
import io.swagger.annotations.apioperation; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.requestmethod; 
import org.springframework.web.bind.annotation.requestparam; 
import org.springframework.web.bind.annotation.restcontroller; 
 
import javax.annotation.resource; 
 
/** 
 * created by zhouhs on 2016/12/30. 
 */ 
@restcontroller 
@requestmapping(value = "/user") 
@api(description = "用户") 
public class usercontroller { 
 
 @resource 
 userdao userdao; 
 @apioperation(value = "添加用户") 
 @apiimplicitparams({ 
   @apiimplicitparam(name = "name" , value = "name" , paramtype = "query" , required = true ), 
   @apiimplicitparam(name = "age" , value = "age" , paramtype = "query" , required = true ) 
 }) 
 @requestmapping(value = "/adduser" , method = requestmethod.post) 
 public string adduser(@requestparam(value = "name") string name,@requestparam(value = "age") int age){ 
 
  user user = new user(); 
  user.setname(name); 
  user.setage(age); 
 
  userdao.save(user); 
 
  return "add user success !"; 
 } 
 
 @apioperation(value = "查找用户") 
 @apiimplicitparam(name = "id" , value = "id" , paramtype = "query" , required = true , datatype = "int") 
 @requestmapping(value = "/findbyid" , method = requestmethod.post) 
 public string findbyid(@requestparam(value = "id") long id){ 
 
  user user = userdao.findbyid(id); 
 
  if(user == null){ 
   return "error"; 
  }else{ 
   return "name:" + user.getname() + " , age:" + user.getage(); 
  } 
 } 
 
 @apioperation(value = "查询所有用户") 
 @requestmapping(value = "/findall" , method = requestmethod.post) 
 public iterable findall(){ 
 
  iterable<user> userlist = userdao.findall(); 
 
  return userlist; 
 
 } 
 
 @apioperation(value = "删除用户") 
 @apiimplicitparam(name = "id" , value = "id" , paramtype = "query" , required = true , datatype = "int") 
 @requestmapping(value = "/deletebyid" , method = requestmethod.post) 
 public string deletebyid(@requestparam(value = "id") long id){ 
 
  userdao.delete(id); 
  return "delete success !"; 
 
 } 
} 

数据表(id定义为integer):

表1

userdao:

package com.seawater.dao; 
 
import com.seawater.bean.user; 
import org.springframework.data.repository.crudrepository; 
 
/** 
 * created by zhouhs on 2016/12/30. 
 */ 
public interface userdao extends crudrepository<user, long> { 
 
 public user findbyid(long id); 
 
} 

然后启动项目:访问http://localhost:8081/swagger-ui.html

结果:

结果1

方法我就不一一操作了。

源码地址(项目中的源码可能会更多哦,需要自己找到对应源码):

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网