步骤
1.在pom.xml添加mysql,spring-data-jpa依赖
2.在application.properties文件中配置mysql连接配置文件
3.在application.properties文件中配置JPA配置信息
4.编写测试例子
内容
1.在pom.xml添加mysql,spring-data-jpa依赖
1 2 3 4 5 6 7 8
| <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
|
2.在application.properties文件中配置mysql连接配置文件
1 2 3 4 5 6 7 8
| spring.datasource.url = jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10
|
3.在application.properties文件中配置JPA配置信息
1 2 3 4 5 6 7 8 9
| //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 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
|
4.编写测试例子
(1)创建实体类Demo,如果已经存在,可以忽略
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
| /** * 创建了一个实体类。 * * 如何持久化呢? * * 1、使用@Entity进行实体类的持久化操作,当JPA检测到我们的实体类当中有 * * @Entity 注解的时候,会在数据库中生成对应的表结构信息。 * * * 如何指定主键以及主键的生成策略? * * 2、使用@Id指定主键. */ @Entity public class Cat { /** * 使用@Id指定主键. * * 指定主键的生成策略,mysql默认的是自增长。 * */ @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id;//主键. private String catName;//姓名. cat_name private int catAge;//年龄. cat_age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCatName() { return catName; } public void setCatName(String catName) { this.catName = catName; } public int getCatAge() { return catAge; } public void setCatAge(int catAge) { this.catAge = catAge; } }
|
(2)创建jpa repository类操作持久化(CrudRepository)
1 2
| public interface CatRepository extends CrudRepository<Cat, Integer>{ }
|
(3)创建service类
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
| @Service public class CatService { @Resource private CatRepository catRepository; /** * save,update ,delete 方法需要绑定事务. * * 使用@Transactional进行事务的绑定. * * @param cat */ //保存数据. @Transactional public void save(Cat cat){ catRepository.save(cat); } //删除数据》 @Transactional public void delete(int id){ catRepository.delete(id); } //查询数据. public Iterable<Cat> getAll(){ return catRepository.findAll(); } }
|
(4)创建restful请求类
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
| @RestController @RequestMapping("/cat") public class CatController { @Resource private CatService catService; @RequestMapping("/save") public String save(){ Cat cat = new Cat(); cat.setCatName("jack"); cat.setCatAge(3); catService.save(cat); return "save ok."; } @RequestMapping("/delete") public String delete(){ catService.delete(1); return "delete ok"; } @RequestMapping("/getAll") public Iterable<Cat> getAll(){ return catService.getAll(); } }
|
(5)测试
在前端输入save.do,delete.do,getAll时候后台数据库执行正确
参考源码