idea中搭建基于maven的spring-boot项目

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_23086307/article/details/83153385

其实我一直很想写一篇文章来说明一下如何在idea中创建一个spring-boot项目的,但是由于找工作和换工作的时间一直耽搁到现在,趁今天晚上加班的时间,终于挤出半个小时来讲述如何在idea中创建spring-boot项目。

在上篇文章关于在《idea中搭建基于maven的ssm整合框架》得到读者的好拼反馈,但是由于spring配置使得spring的使用起来入门比较困难,所以有了 spring-boot这样优秀的框架。简直是天秀。废话不说,直接上教程。

首先我们打开idea。创建一个maven项目。

在这里插入图片描述

选择spring Initializr,选择jdk版本,然后next。

**第二步:**输入组名称,artifact名称,选择maven配置, 项目名称,一般在输入组名称下面就会根这变化,无序手动修改。
配置完成以后我们直接点击next。
在这里插入图片描述

第三步:添加web依赖

在这里插入图片描述

第四步:添加项目名称和位置

在这里插入图片描述

注意!!!!!刚新建的项目可能没有初始化制定项目路径的角色。。。
在这里插入图片描述

扫描二维码关注公众号,回复: 3681530 查看本文章

添加完成后直接finish。得到如下目录
第一次创建可能有点慢,需要下载maven相关的jar。

在这里插入图片描述

第五步:添加相关配置文件
(1)添加application.properties

#tomcat
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/twjitm?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
#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

#视图层控制
#这个地方只能用templates,麻蛋,我写成web就是找不到,我也是醉了,我能吐槽一下springboot吗?
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**

配置文件中有数据库地址,tomcat端口等信息。

第五步:添加JPA配置
创建一个类:JpaConfiguration.java

package com.springboot.config;

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 文江 on 2017/12/20.
 */
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories(basePackages = "com.springboot.dao")
@EntityScan(basePackages = "com.springboot.entity")
public class JpaConfiguration {
    @Bean
    PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

添加dao位置和entity位置。编写一个dao和entity,此处就省略了。需要看源码的小伙伴下载源码,其实很简单。

在这里插入图片描述

第六步:运行spring-boot项目
直接运行TwjitmBootApplication中的main函数。

package com.twjitm.twjitmboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TwjitmBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(TwjitmBootApplication.class, args);
    }
}

在这里插入图片描述

在这里插入图片描述

当看到这我们的服务正常启动起来了,所以在idea中创建spring-boot项目我们能够正常启动了。所以我们可以利用springboot来做分布式微服务框架了,也可以用来做系统了。

有需要源码的小伙伴可以直接在我的GitHub下载。欢迎star和fork。

https://github.com/twjitm/springboot

猜你喜欢

转载自blog.csdn.net/baidu_23086307/article/details/83153385