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

 更新时间:2017年03月20日 09:26:57   作者:seawaterzhou  
本篇文章主要介绍了Spring Boot 添加MySQL数据库及JPA,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

最近在学习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

方法我就不一一操作了。

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

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

相关文章

  • SpringBoot整合easyExcel实现CSV格式文件的导入导出

    SpringBoot整合easyExcel实现CSV格式文件的导入导出

    这篇文章主要为大家详细介绍了SpringBoot整合easyExcel实现CSV格式文件的导入导出,文中的示例代码讲解详细,具有一定的参考价值,感兴趣的小伙伴可以参考下
    2024-02-02
  • JVM内存参数配置详解

    JVM内存参数配置详解

    本文主要介绍了JVM内存参数配置详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • java使用Jco连接SAP过程

    java使用Jco连接SAP过程

    这篇文章主要介绍了java使用Jco连接SAP过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • 深入理解Java设计模式之中介者模式

    深入理解Java设计模式之中介者模式

    这篇文章主要介绍了JAVA设计模式之中介者模式的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解
    2021-11-11
  • Java14对于NullPointerException的新处理方式示例解析

    Java14对于NullPointerException的新处理方式示例解析

    这篇文章主要为大家介绍了Java14对于NullPointerException的新处理方式示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • SpringBoot请求参数相关注解说明小结

    SpringBoot请求参数相关注解说明小结

    这篇文章主要介绍了SpringBoot请求参数相关注解说明,主要包括@PathVariable,@RequestHeader、@CookieValue、@RequestBody和@RequestParam,本文结合实例代码给大家讲解的非常详细,需要的朋友可以参考下
    2022-05-05
  • SpringBoot整合JPA的实例代码

    SpringBoot整合JPA的实例代码

    本篇文章主要介绍了SpringBoot整合JPA的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • MyBatis-Plus updateById不更新null值的方法解决

    MyBatis-Plus updateById不更新null值的方法解决

    用Mybatis-Plus的updateById()来更新数据时,无法将字段设置为null值,更新后数据还是原来的值,本文就来详细的介绍一下解决方法,具有一定的参考价值,感兴趣的可以了解一下
    2023-08-08
  • java实现将汉语转换为拼音功能

    java实现将汉语转换为拼音功能

    这篇文章主要介绍了java实现将汉语转换为拼音功能,非常不错,具有参考借鉴价值 ,需要的朋友可以参考下
    2017-05-05
  • Spring的跨域的几个方案

    Spring的跨域的几个方案

    这篇文章主要介绍了Spring的跨域的几个方案,CrossOrigin、addCorsMappings、CorsFIlter等方案,具有一定的参考价值,需要的小伙伴可以参考一下,希望对你有所帮助
    2022-02-02

最新评论