springBoot利用spring 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>

创建项目时,勾选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;
    }

}

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

@SpringBootApplication
@EnableJpaRepositories

添加该注解是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


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);
}

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);
    }
}

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值