继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

「JavaWeb」Springboot + MySQL + JPA

慕码人8056858
关注TA
已关注
手记 1092
粉丝 350
获赞 1320

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 configspring.datasource.url=jdbc:mysql://localhost:3306/db_examplespring.datasource.username=root //MySQL userNamespring.datasource.password=password //MySQL password

4.2 Create the @Entity model

@Entity // This tells Hibernate to make a table out of this classpublic 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, Deletepublic interface UserRepository extends CrudRepository<User, Long> {
}

4.4 Create a new controller for your Spring application

@RestControllerpublic 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();
    }
}



作者:DB_Liu
链接:https://www.jianshu.com/p/9804e48fe2fd


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP