摘要:前言访问关系型数据库是工程常见的需求。是当今业界应用最广泛的开源关系型数据库。全家桶提供了,可以让开发者方便快捷地开发出访问的业务代码。
前言
访问关系型数据库是Web工程常见的需求。MySQL是当今业界应用最广泛的开源关系型数据库。Spring Boot全家桶提供了JPA,可以让开发者方便快捷地开发出访问MySQL的业务代码。
准备工作1 安装jdk1.8
2 安装maven
3 具备Spring和SpringMVC的基础知识
</>复制代码
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`phone` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
2. 在pom.xml中加入Spring Boot中跟MySQL相关的引用
</>复制代码
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin
3. 创建一个程序入口类
</>复制代码
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4. 修改application.properties文件,添加MySQL的配置
</>复制代码
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=
5. 添加一个user表对应的实体类User
</>复制代码
package com.example.demo.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
private String name;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
6. 创建一个UserRepository接口
</>复制代码
package com.example.demo.repository;
import com.example.demo.entity.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository {
}
7. 创建一个测试方法
</>复制代码
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;
@Controller
public class UserController {
@Autowired
private UserRepository userRepository;
/**
* add user
* @param name
* @param phone
* @return
*/
@RequestMapping(path="/user", method = RequestMethod.POST)
@ResponseBody
public String addNewUser (@RequestParam String name, @RequestParam String phone) {
User n = new User();
n.setName(name);
n.setPhone(phone);
userRepository.save(n);
return "Success";
}
}
8. 调用接口添加一条数据到mysql
</>复制代码
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
摘要:是一个基于映射的标准协议目前最新版本是。的主要实现由和等完成,我们只要使用来开发,无论是哪一个开发方式都是一样的。是的一个子项目,它通过基于的极大地减少了作为数据访问方案的代码量。源码下载后语以上为使用访问数据库的教程。 微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题。 前言 如题,今天介绍 Spring Data JPA 的使用。 什么是 Spring D...
摘要:前言如题,今天介绍的声明式事务。提供一个注解在配置类上来开启声明式事务的支持。而在配置里还开启了对声明式事务的支持,代码如下所以在中,无须显式开启使用注解。源码下载后语以上为声明式事务的教程。 微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题。 前言 如题,今天介绍 SpringBoot 的 声明式事务。 Spring 的事务机制 所有的数据访问技术都有事务处...
阅读 3768·2021-08-10 09:42
阅读 657·2019-08-30 15:55
阅读 956·2019-08-30 15:54
阅读 3176·2019-08-30 13:45
阅读 628·2019-08-29 16:23
阅读 2056·2019-08-29 16:23
阅读 1059·2019-08-29 15:18
阅读 2333·2019-08-29 12:57