美文网首页
A.2 springboot data jpa

A.2 springboot data jpa

作者: 成长的键盘手 | 来源:发表于2018-02-09 21:07 被阅读0次

    springboot data jpa

    1. 介绍

    1.1 什么是jpa?

    jpa是java persistence API。是java持久层的标准,起源于EJB3.0。描述的是领域模型和关系表的映射,并持久化到数据库中。实现该标准的厂商有:

    • hibernate
    • openjpa

    1.2 什么是spring data jpa?

    spring致力于减少数据访问层(DAO)的开发工作量,对标准jpa的包装。开发者只需要生成持久层接口即可。开发者不需要关注:

    • EntityManager如何创建
    • 基本的增删改查方法不用编写
    • 事物如何提交

    1.3 本文的重点

    本文主要讲解如何在 springboot 中使用 spring data jpa,使用hibernate作为jpa的实现,使用mysql作为数据库。

    2. 修改工程

    依据第一章节的样例工程,进行更改。

    2.1 修改POM

    在pom中增加jpa相关的maven依赖

    <!-- jpa的pom插件 (包括了spring-jpa、hibernate、spring jdbc)-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <!-- DB -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    

    2.2 修改application.properties

    主要是增加数据源和JPA相关的配置,具体内容如下:

    #数据源配置
    spring.datasource.url=jdbc:mysql://localhost:3306/spring-boot-demo?characterEncoding=utf-8&autoReconnect=true
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.max-active=5
    spring.datasource.max-idle=3
    spring.datasource.test-on-borrow=true
    spring.datasource.test-while-idle=true
    spring.datasource.validation-query=SELECT 1;
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    #jpa属性配置
    spring.jpa.database = MYSQL
    spring.jpa.show-sql = true
    spring.jpa.hibernate.ddlAuto = update
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    #hibernate实体类扫描包路径
    entitymanager.packagesToScan=pers.mateng
    

    3. 业务编码

    3.1 创建领域模型

    package pers.mateng.demo.springboot.domain;
    
    @Entity
    @Table(name="TB_USER")
    public class User {
        
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="U_ID")
        private Long id;
        
        @Column(name="U_NAME", nullable = false, length=32, unique=true)
        private String name;
        
        @Column(name="U_AGE", length=4)
        private Integer age;
    
        get/set……
        
    }
    

    3.2 创建DAO

    package pers.mateng.demo.springboot.dao;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import pers.mateng.springdemo.domain.User;
    
    /**
     * 用户管理增删改查的dao
     * @author mateng
     *
     */
    public interface UserDao extends JpaRepository<User, Long> {
        
    
    }
    

    3.3 创建controller

    package pers.mateng.demo.springboot.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import pers.mateng.demo.springboot.dao.UserDao;
    import pers.mateng.demo.springboot.domain.User;
    
    /**
     * 用户管理的controller
     * @author mateng
     */
    @RestController
    public class UserController {
        
        @Autowired
        private UserDao userDao;
        
        @RequestMapping(method=RequestMethod.GET)
        public List<User> findAll() {
            return userDao.findAll();
        }
        
        @RequestMapping(method=RequestMethod.POST)
        public User add(@ModelAttribute User user) {
            return userDao.save(user);
        }
        
        @RequestMapping(path="/{id}", method=RequestMethod.GET)
        public User findById(@PathVariable Long id) {
            return userDao.findOne(id);
        }
    }
    

    4. 验证

    使用eclipse的run as重新启动Application的main函数

    使用如下命令测试增加、查询接口。注意:下面连接中的ip地址(当前开发机器的ip地址)

    1、增加:

    curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' -d "name=zhansan&age=30" 'http://192.168.50.7:8888/user'

    2、查询:

    查询全部

    curl -X GET 'http://192.168.50.7:8888/user'

    根据id查询

    curl -X GET 'http://192.168.50.7:8888/user/1'

    5. 源码

    springboot-demo-2

    相关文章

      网友评论

          本文标题:A.2 springboot data jpa

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