「JavaWeb」Springboot + MySQL

作者: dongbingliu | 来源:发表于2018-07-31 12:32 被阅读5次

1. 前言

Java Web 核心逻辑操作数据库,数据库默认优先选择 MySQL

SpringBoot 项目中 Java 代码操作数据库提供了常用的 2 种框架,JPA「Java Persistence API」与 MyBatis, JPA 是 Spring 家族框架中一种,简洁易用。相对而言,MyBatis 互联网公司使用较多。最为入门优先学习下 JPA 。

2. 构建依赖配置

2.1 项目构建Gradle与Maven构建,选择一种构建即可;

Gradle 构建添加依赖:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    // Use MySQL Connector-J
    compile 'mysql:mysql-connector-java'
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Maven 构建添加依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- Use MySQL Connector-J -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3. 创建数据库

数据库操作 GUI 界面软件: navicat 「推荐,All 平台」SQLyog

使用 Navicat 软件创建 db_example 数据库;

4. Java 代码实现

4.1 application.properties 文件添加如下配置:

spring.jpa.hibernate.ddl-auto=create //the first one,after must update update config
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=root //MySQL userName
spring.datasource.password=password //MySQL password

4.2 Create the @Entity model

@Entity // This tells Hibernate to make a table out of this class
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String name;
    private String email;
    ....ignore ... getMethod and setMethod 
}

4.3 Create the repository

import hello.User;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
public interface UserRepository extends CrudRepository<User, Long> {
}

4.4 Create a new controller for your Spring application

@RestController
public class MainController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired // This means to get the bean called userRepository 
//Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    @GetMapping(value = "/add")
    public String addNewUser(@RequestParam String name,  @RequestParam String email){
        System.out.println(name+email);
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        userRepository.save(user);

        logger.error("addNewUser name = "+name + "; email = "+email);
        return "Save";
    }

    @GetMapping(value = "/all")
    public Iterable<User> getAllUser(){
        return userRepository.findAll();
    }
}

备注::测试工具推荐,Postman

相关文章

本文标题:「JavaWeb」Springboot + MySQL

本文链接:https://www.haomeiwen.com/subject/ideqvftx.html