用IDEA搭建第一个SpringBoot+SpringData Jpa框架
2018-01-25 14:01
316 查看
本文纯属自己的一些摸索和实践,如有写的不好的地方欢迎大家一起讨论。
首先我们来创建一个maven项目。
创建java目录并将其变为Sources Root文件夹。
3.接下来配置pom.xml文件里的一些依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.atben</groupId> <artifactId>sboot</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>sboot Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>1.4.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>1.5.1.RELEASE</version> </dependency> </dependencies> <build> <finalName>sboot</finalName> </build> </project>
4.配置application.properties配置文件,放在resources目录下。
server.port=8080 server.tomcat.uri-encoding=utf-8 #MySQL spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=123 #Spring Data JPA spring.jpa.database=MYSQL spring.jpa.show-sql=true 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.到此框架配置文件就结束了,没错,springboot最大的优点就是省去了繁琐的xml配置文件,接下来我们来写一个接口试试。
/**Sboot.java**/ package cn.atben.sboot.pojo; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; /** * Created by atben * 2018-01-24 14:58 */ @Entity @Table(name = "sboot") public class Sboot { @Id @GeneratedValue private Integer id; private String test1; private String test2; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTest1() { return test1; } public void setTest1(String test1) { this.test1 = test1; } public String getTest2() { return test2; } public void setTest2(String test2) { this.test2 = test2; } }
/**SbootController**/ package cn.atben.sboot.controller; import cn.atben.sboot.repository.SbootRepositoty; import cn.atben.sboot.pojo.Sboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * Created by atben * 2018-01-24 14:31 */ @RestController @EnableAutoConfiguration public class SbootController { @Autowired private SbootRepositoty sbootRepositoty; @RequestMapping(value = "/hi") public String hi(){ return "hi!"; } @GetMapping(value = "/list") public List<Sboot> list(){ return sbootRepositoty.findAll(); } @GetMapping(value = "/delete") public int deletelist(int id){ sbootRepositoty.delete(id); return 1; } }
/**SbootRepositoty**/ package cn.atben.sboot.repository; import cn.atben.sboot.pojo.Sboot; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** * Created by atben * 2018-01-24 15:11 */ @Repository public interface SbootRepositoty extends JpaRepository<Sboot,Integer>{ }
由于springboot集成了tomcat,不需要讲程序打包部署到tomcat服务器,我们只需要写一个入口。
/**Entry**/ package cn.atben.sboot.configuration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; /** * Created by atben * 2018-01-24 15:36 * *项目启动入口,配置包根路径 */ @SpringBootApplication @ComponentScan(basePackages = "cn.atben") public class Entry { public static void main(String[] args) throws Exception { SpringApplication.run(Entry.class, args); } }
/**JpaConfiguration**/ package cn.atben.sboot.configuration; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * Created by atben * 2018-01-24 15:33 */ @Order(Ordered.HIGHEST_PRECEDENCE) @Configuration @EnableTransactionManagement(proxyTargetClass = true) @EnableJpaRepositories(basePackages = "cn.atben.sboot.repository") @EntityScan(basePackages = "cn.atben.sboot.pojo") public class JpaConfiguration { @Bean PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){ return new PersistenceExceptionTranslationPostProcessor(); } }
到此框架搭建完成,找到Entry.java右键run,即可将程序跑起来了。
相关文章推荐
- 使用IDEA搭建第一个SpringBoot程序
- IDEA 搭建自己的第一个 SpringMvc Hello Word
- idea maven 搭建第一个maven项目的注意事项和问题
- idea 搭建java项目
- Spring重温(一) — —搭建第一个spring程序
- IDEA上面安装vue插件,并极速搭建vue后台管理模板
- 使用IDEA整合Spring+Spring MVC+Mybatis+Maven+Jetty框架的搭建
- 从零搭建基于sbt和IDEA的远程spark调试工程
- Android:入门、开发环境搭建、第一个Android程序、源码下载
- Struts2之struts-2.3.20开发环境的搭建并实现第一个Hello World小应用
- Ibatis开发环境搭建和编写第一个程序
- IDEA搭建SpringBoot+Maven+Mybatis+MySQL-Web项目流程
- Intellij-IDEA搭建SrpingMVC+Spring+Mybatis+Bootstrap+分页(二)
- 手把手教你在STM32下搭建属于自己的第一个应用程序
- 【一】cocos2dx开发环境的搭建,第一个程序——HelloWorld
- [maven学习笔记]第一节,认识maven,搭建maven开发环境,写第一个HelloWorld
- idea+Spring Boot的第一个应用,以及热部署的配置
- [学习笔记]java基础Java8SE开发环境搭建、第一个Java Hello World、Java程序的编译与执行
- MongoDB和grails框架搭建(IDEA)