资讯专栏INFORMATION COLUMN

SpringBoot 1024行代码 - 用JPA访问MySQL

JerryWangSAP / 1764人阅读

摘要:前言访问关系型数据库是工程常见的需求。是当今业界应用最广泛的开源关系型数据库。全家桶提供了,可以让开发者方便快捷地开发出访问的业务代码。

前言

访问关系型数据库是Web工程常见的需求。MySQL是当今业界应用最广泛的开源关系型数据库。Spring Boot全家桶提供了JPA,可以让开发者方便快捷地开发出访问MySQL的业务代码。

准备工作

1 安装jdk1.8
2 安装maven
3 具备Spring和SpringMVC的基础知识

具体步骤 1. 搭建一个MySQL数据库,创建一个名叫test的数据库,在test库中创建一个user表

</>复制代码

  1. CREATE TABLE `user` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(45) DEFAULT NULL,
  4. `phone` varchar(45) DEFAULT NULL,
  5. PRIMARY KEY (`id`),
  6. KEY `name` (`name`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
2. 在pom.xml中加入Spring Boot中跟MySQL相关的引用

</>复制代码

  1. org.springframework.boot
  2. spring-boot-starter-parent
  3. 1.5.8.RELEASE
  4. org.springframework.boot
  5. spring-boot-starter-web
  6. org.springframework.boot
  7. spring-boot-starter-data-jpa
  8. mysql
  9. mysql-connector-java
  10. org.springframework.boot
  11. spring-boot-maven-plugin
3. 创建一个程序入口类

</>复制代码

  1. package com.example.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class DemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9. }
4. 修改application.properties文件,添加MySQL的配置

</>复制代码

  1. spring.jpa.hibernate.ddl-auto=none
  2. spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
  3. spring.datasource.username=root
  4. spring.datasource.password=
5. 添加一个user表对应的实体类User

</>复制代码

  1. package com.example.demo.entity;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.GenerationType;
  5. import javax.persistence.Id;
  6. @Entity
  7. public class User {
  8. @Id
  9. @GeneratedValue(strategy= GenerationType.AUTO)
  10. private Integer id;
  11. private String name;
  12. private String phone;
  13. public Integer getId() {
  14. return id;
  15. }
  16. public void setId(Integer id) {
  17. this.id = id;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public String getPhone() {
  26. return phone;
  27. }
  28. public void setPhone(String phone) {
  29. this.phone = phone;
  30. }
  31. }
6. 创建一个UserRepository接口

</>复制代码

  1. package com.example.demo.repository;
  2. import com.example.demo.entity.User;
  3. import org.springframework.data.repository.CrudRepository;
  4. public interface UserRepository extends CrudRepository {
  5. }
7. 创建一个测试方法

</>复制代码

  1. package com.example.demo.controller;
  2. import com.example.demo.entity.User;
  3. import com.example.demo.repository.UserRepository;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. @Controller
  11. public class UserController {
  12. @Autowired
  13. private UserRepository userRepository;
  14. /**
  15. * add user
  16. * @param name
  17. * @param phone
  18. * @return
  19. */
  20. @RequestMapping(path="/user", method = RequestMethod.POST)
  21. @ResponseBody
  22. public String addNewUser (@RequestParam String name, @RequestParam String phone) {
  23. User n = new User();
  24. n.setName(name);
  25. n.setPhone(phone);
  26. userRepository.save(n);
  27. return "Success";
  28. }
  29. }
8. 调用接口添加一条数据到mysql

</>复制代码

  1. curl -X POST 127.0.0.1:8080/user -d "name=tom&phone=654321"

观察数据库user表,发现增加了一条新数据

源码

https://github.com/gzllol/spr...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70672.html

相关文章

  • SpringBoot 实战 (八) | 使 Spring Data JPA 访问 Mysql 数据

    摘要:是一个基于映射的标准协议目前最新版本是。的主要实现由和等完成,我们只要使用来开发,无论是哪一个开发方式都是一样的。是的一个子项目,它通过基于的极大地减少了作为数据访问方案的代码量。源码下载后语以上为使用访问数据库的教程。 微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题。 前言 如题,今天介绍 Spring Data JPA 的使用。 什么是 Spring D...

    hedzr 评论0 收藏0
  • SpringBoot 实战 (十) | 声明式事务

    摘要:前言如题,今天介绍的声明式事务。提供一个注解在配置类上来开启声明式事务的支持。而在配置里还开启了对声明式事务的支持,代码如下所以在中,无须显式开启使用注解。源码下载后语以上为声明式事务的教程。 微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题。 前言 如题,今天介绍 SpringBoot 的 声明式事务。 Spring 的事务机制 所有的数据访问技术都有事务处...

    ygyooo 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<