赞
踩
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对于新建项目而言,比较简单方便,是利用面向对象的思想创建操作数据库。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。