zoukankan      html  css  js  c++  java
  • springboot学习(八) 使用jpa访问数据库

    1、添加maven依赖

    <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>6.0.6</version>
    </dependency>
    
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    2、添加数据库连接信息

    spring.datasource.url=jdbc:mysql://localhost:3309/test
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.jpa.properties.hibernate.hbm2ddl.auto=update

    spring.jpa.properties.hibernate.hbm2ddl.auto是hibernate的配置属性,其主要作用是:自动创建、更新、验证数据库表结构。

    该参数的几种配置如下:

    • create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
    • create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
    • update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。
    • validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。

    3、创建实体类

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity
    public class User {
    
        @Id
        @GeneratedValue
        private Long id;
    
        private String name;
    
        .......  
    
    }

    4、创建数据访问接口

    import cn.origal.domain.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.query.Param;
    
    /**
     * Created by Administrator on 2017/9/24.
     */
    public interface UserRepository extends JpaRepository<User, Long> {
    
        User findUserById(long id);
    
        @Query("from User u where u.name = :name")
        List<User> findUser(@Param("name") String name);
    
    }

    5、进行单元测试

    import cn.origal.Application;
    import cn.origal.dao.UserRepository;
    import cn.origal.domain.User;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.SpringApplicationConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.web.WebAppConfiguration;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    @SpringApplicationConfiguration(Application.class)
    public class TestUser {
    
        @Autowired
        private UserRepository userRepository;
    
        @Test
        public void test() throws Exception {
    
            userRepository.save(new User("1"));
            userRepository.save(new User("2"));
            userRepository.save(new User("3"));
    
            System.out.println(userRepository.findUser("3"));
            System.out.println(userRepository.findUserById(1));
    
        }
    
    }
  • 相关阅读:
    Python 基础 字符串拼接 + if while for循环
    JTable 的使用方法
    java与数据库连接的几个步骤
    socket通信 _ 一个简单的群聊系统
    基本的文件读写
    多线程之碰撞小球
    java类的继承
    java类和对象
    java中的关键字
    java 线程实现方式
  • 原文地址:https://www.cnblogs.com/origalom/p/7588048.html
Copyright © 2011-2022 走看看