• Spring boot集成spring-boot-starter-data-jpa环境搭建


    1、创建Spring boot项目

    2、保存等待构建完成

    3、增加spring-boot-starter-data-jpa、内存数据库依赖包hsqldb

            <!-- 添加data jpa依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
                <scope>runtime</scope>
            </dependency>

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.kevin</groupId>
        <artifactId>boot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>boot</name>
        <description>Demo project for Spring Boot</description>
    
        <!--
       spring-boot-starter-parent项目中继承以获得合理的默认值。父项目提供以下功能:
       Java 1.8作为默认的编译器级别。
       UTF-8源码编码。
       一个依赖管理部分,从春天启动依赖性继承POM,管理公共依赖的版本。这种依赖关系管理可以让您在自己的pom中使用这些依赖关系时忽略<version>标记。
       明智的 资源过滤。
       明智的插件配置(exec插件, Git提交ID和 阴影)。
       明智的资源过滤application.properties和application.yml 包括配置文件特定的文件(例如application-dev.properties和 application-dev.yml)-->
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- 添加data jpa依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
                <scope>runtime</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>

    4、项目结构图

    5、相关文件

    applicationContext.properties

    ddl-auto:create----每次运行该程序,没有表格会新建表格,表内有数据会清空

    ddl-auto:create-drop----每次程序结束的时候会清空表

    ddl-auto:update----每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新

    ddl-auto:validate----运行程序会校验数据与数据库的字段类型是否相同,不同会报错

    spring.jpa.hibernate.ddl-auto = create-drop

    实体类User.java

    package com.kevin.boot.model;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import java.io.Serializable;
    
    /**
     * @author pingan.yang
     * @program SpringBootJpa
     * @description
     * @create 2018-05-29 19:10
     */
    @Entity
    public class User implements Serializable {
        @Id
        @GeneratedValue
        private int id;
    
        @Column(nullable = false)
        private String name;
    
        @Column(nullable = false)
        private int age;
    
        public User() {
    
        }
    
        public User (String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    数据库底层操作类UserRepository.java

    package com.kevin.boot.repository;
    
    import com.kevin.boot.model.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import java.util.List;
    
    public interface UserRepository extends JpaRepository<User, Long> {
    
        User getUserById(int id);
    
        List<User> getUserByName(String name);
    
    }

    User业务中心控制类

    package com.kevin.boot.controller;
    
    import com.kevin.boot.model.User;
    import com.kevin.boot.repository.UserRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * @author kevin.yang
     * @program SpringBootJpa
     * @description
     * @create 2018-05-29 19:11
     */
    // RestController = Controller + ResponseBody
    @RestController
    public class UserController {
    
        @Autowired
        private UserRepository userRepository;
    
        @RequestMapping("/saveUser")
        public String saveUser(@RequestParam("name") String name, @RequestParam("age") int age) {
            try {
                User user = new User(name, age);
                userRepository.save(user);
            } catch (Exception e) {
                e.printStackTrace();
                return "error";
            }
    
            return "save success";
        }
    
        @RequestMapping("/getUserById")
        public User getUserById(@RequestParam("id") int id) {
            return userRepository.getUserById(id);
        }
    
        @RequestMapping("/getAllUser")
        public List<User> getAllUser() {
            return userRepository.findAll();
        }
    }

    6、运行测试

    运行Spring boot启动类BootApplication.java

    浏览器中输入一下测试地址

     1>http://127.0.0.1:8080/saveUser?name=kevin.yang&age=1

     2>http://127.0.0.1:8080/getAllUser

    测试结果

    1> 测试添加用户信息

    2> 测试查询用户信息

      

  • 相关阅读:
    帝国CMS采集
    帝国CMS常用资料
    IIS 支持PHP(与Apache环境共存)
    Firebug+Firefox 脚本调试
    javascript 参考
    [__NSCFString objectFromJSONString]: unrecognized selector sent to 解决办法
    大家好,我在CSDN开通了博客
    Apple MachO Linker Error
    ios 排序
    ios 二维码生成 扫瞄
  • 原文地址:https://www.cnblogs.com/Kevin00/p/9107990.html
Copyright © 2020-2023  润新知