当前位置:   article > 正文

springBoot利用spring Data jpa整合hibernate_springboot使用 starter-data-jpa与hibernate

springboot使用 starter-data-jpa与hibernate

1.创建springBoot项目并导入jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

创建项目时,勾选web即可,其他jar包用到时再加载即可。
2.创建model

添加@Entity的作用在于说明该类是个实体类

@Entity
public class News {
    @Id    //标明是id属性并且自增长
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private String title;

    public News() {
    }

    public News(String name, String title) {
        this.name = name;
        this.title = title;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof News)) return false;
        News news = (News) o;
        return id.equals(news.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", title='" + title + '\'' +
                "}\n";
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

3.在springboot项目的启动类上面添加注解:
在这里插入图片描述

@SpringBootApplication
@EnableJpaRepositories
  • 1
  • 2

添加该注解是jpa的注解配置,是basePackages的别名,用于配置扫描Repositories所在的包。
4.配置文件properties

#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/hibernate?useunicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.max-idle=10
#spring.datasource.max-wait=10000
#spring.datasource.min-idle=5
#spring.datasource.initial-size=5

# Specify the DBMS
spring.jpa.database = MYSQL

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

5.写dao接口

@Repository
public interface NewsDao extends JpaRepository<News,Integer> {
    //    约定大于配置,消除耦合性
    List<News> findByIdGreaterThanOrderByTitleDesc(Integer id);

    @Query(value = "select * from news where title= ?",nativeQuery = true)
    News test1(String title);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

6.写测试类
与springMVC项目的测试类的不同之处在于新增了springBootTest注解,Spring Boot Test 是在Spring Test之上的再次封装,增加了切片测试,增强了mock能力,RunWith合用增加了测试的高效性。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class NewsDaoTest {
    @Autowired
    private NewsDao newsDao;
    @Test
    public void testAdd(){
       /* News news = newsDao.save(new News("马拉多纳挂了","这次是真的"));
        assertTrue(news.getId()>0);*/
        for (int i = 0; i < 100; i++) {
            newsDao.save(new News("新闻"+(i+1),"内容"+(i+1)));
        }
    }
    @Test
    public void testGetAll(){
        List<News> list = newsDao.findAll();
        System.out.println(list);
    }
    @Test
    public void testIdThan20(){
        List<News> list = newsDao.findByIdGreaterThanOrderByTitleDesc(20);
        System.out.println(list);
    }
    @Test
    public void test1(){
        News news = newsDao.test1("内容99");
        System.out.println(news);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

遇到的错误记录:
在这里插入图片描述
此处如果为create-drop会一直建表不成功。
总结:hibernate对于新建项目而言,比较简单方便,是利用面向对象的思想创建操作数据库。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/815260
推荐阅读
相关标签
  

闽ICP备14008679号