SpringBoot实践之---JPA连接数据库+idea全新创建该工程
2018-02-05 11:32
656 查看
录结构如下:
1.新建一个新的gradle项目
2.build.gradle配置文件
3.创建实体类
4.创建JPA仓库类
5。创建controller类
6.新建属性文件application.properties,配置连接数据库的信息
7.最后配置启动主函数*(spring Boot的被@SpringBootApplication注解的Application.Java必须放在所有的RestController的根路径的package下)
访问路径: http://127.0.0.1:8080/test
1.新建一个新的gradle项目
2.build.gradle配置文件
group 'com.great' version '1.0-SNAPSHOT' buildscript { ext { springBootVersion = '1.5.8.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' jar { baseName = 'ng-spring-boot' version = '0.0.1-SNAPSHOT' } sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { mavenCentral() maven { url "https://repo.spring.io/libs-release" } } dependencies { compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-actuator") compile("mysql:mysql-connector-java") compile("org.springframework.boot:spring-boot-starter-test") } task wrapper(type: Wrapper) { gradleVersion = '2.3' }
3.创建实体类
package com.great.dept.domain; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; /** * 部门表实体类 */ @Entity @Table(name = "dept") public class Dept { //部门编号 主键 @Id @Column(name = "id") @GeneratedValue(generator = "uuid2") @GenericGenerator(name = "uuid2", strategy = "uuid2") private String id; //部门编码 @Column(name = "code") private String code; //部门名称 @Column(name = "name") private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.创建JPA仓库类
package com.great.dept.repository; import com.great.dept.domain.Dept; import org.springframework.data.jpa.repository.JpaRepository; /** * 实现Jap仓库接口类 */ public interface DeptRepository extends JpaRepository<Dept, String> { }
5。创建controller类
package com.great.dept.web; import com.great.dept.domain.Dept; import com.great.dept.repository.DeptRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * 部门控制器 */ @RestController @RequestMapping("/test") public class DeptController { //部门Jap仓库接口 @Autowired private DeptRepository deptRepository; @RequestMapping(method = RequestMethod.GET) public List<Dept> deptfind() { List<Dept> deptList = deptRepository.findAll(); for (int i = 0; i < deptList.size(); i++) { System.out.println(deptList.get(i)); } return deptList; } }
6.新建属性文件application.properties,配置连接数据库的信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.jpa.database=mysql spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update #spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
7.最后配置启动主函数*(spring Boot的被@SpringBootApplication注解的Application.Java必须放在所有的RestController的根路径的package下)
package com.great; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
访问路径: http://127.0.0.1:8080/test
相关文章推荐
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- SpringBoot实践之---Hibernate·连接数据库
- springboot+jpa+hibernate+idea做个实现数据库mysql增数据乱码,中文乱码为???
- Spring boot通过JPA和Mybatis同时连接2个关系数据库的例子
- 用Intelij idea 创建 Spring Boot项目一工程项目名称: OpenFirein(1-1)
- spring-boot-data-jpa 连接数据库
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- SpringBoot-创建RESTful风格的 http接口访问jpa 来操作数据库
- 关于idea SpringBoot项目中出现与数据库连接加载不到驱动类异常
- spring-boot-data-jpa 连接数据库
- Spring Boot 实践1 --创建WEB restful项目,集成jpa
- SpringBoot实践之---Mybatis连接数据库(mybatis-generator-plugin)
- IDE maven 创建spring boot 用hibernate 实现连接数据库
- SpringBoot项目创建与数据库连接
- Spring Boot JPA 连接数据库
- 关于IDEA中创建springboot+security+jpa
- Spring Boot JPA 连接数据库
- SpringBoot实践之---JDBC连接数据库
- IntelliJ IDEA工具创建入门的springBoot项目
- Spring Boot MyBatis 连接数据库