mysql 1292 jpa_SpringBoot 1024行代码 - 用JPA访问MySQL

前言

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

准备工作

1 安装jdk1.8

2 安装maven

3 具备Spring和SpringMVC的基础知识

具体步骤

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

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表,发现增加了一条新数据

源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值