2019年8月20日
SpringBoot整合Hibernate
基于springboot整合,基于MySQL数据库。创建项目:
image.png
依赖导入.png
maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
数据库名称demo
表:
DROP TABLE IF EXISTS `car`;
CREATE TABLE `car` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`price` decimal(10,2) DEFAULT NULL,
`createed` datetime DEFAULT NULL,
`updateed` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of car
-- ----------------------------
INSERT INTO `car` VALUES ('26', '奔驰', '420000.00', '2019-06-23 20:08:11', null);
INSERT INTO `car` VALUES ('27', '五菱宏光xc288.cn', '90000.00', '2019-06-23 20:08:11', '2019-06-23 20:53:53');
INSERT INTO `car` VALUES ('28', '宝马', '666666.00', '2019-08-03 13:31:36', null);
INSERT INTO `car` VALUES ('29', '路虎', '555555.00', '2019-08-03 13:32:04', null);
项目如下:
image.png
application配置文件如下:
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#spring.datasource.driverClassName = com.mysql.jdbc.Driver
# 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
控制层controller:
package com.example.hibernatedemo.controller;
import com.example.hibernatedemo.dao.CarRepositoty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DemoController {
@Autowired
private CarRepositoty carRepositoty;
@GetMapping("")
@ResponseBody
private Object index(){
return carRepositoty.findAll();
}
}
实体类
package com.example.hibernatedemo.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Entity
@Table(name = "car")//数据库的表名
public class Car {
/**
* 必须加ID、GeneratedValue 否则报错
*/
@Id
@GeneratedValue
private int id;
private String name;
private double price;
private Date createed;
private Date updateed;
Get和Set方法
持久层接口dao
package com.example.hibernatedemo.dao;
import com.example.hibernatedemo.entity.Car;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CarRepositoty extends JpaRepository<Car, Long> {
//查询所有,其他接口自行尝试或查文档
List<Car> findAll();
}
运行结果:http://localhost:8080/
网友评论