为了账号安全,请及时绑定邮箱和手机立即绑定

「JavaWeb」Springboot + MySQL + JPA

标签:
Java

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


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消