Intellij idea + Spring boot + Thymeleaf + MySQL

来源:互联网 发布:网络调研公司 编辑:程序博客网 时间:2024/05/22 04:40

1.版本

1.Intellij idea 2016.2.4 +;
2.Spring boot 1.3.8;
3.MySQL 5.6 +;

2.maven 配置

<!--  use a later Java version --><properties>    <java.version>1.8</java.version> <!--  use a later Java version --></properties>
<!-- Inherit defaults from Spring Boot --><parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.3.8.RELEASE</version></parent>

<!-- Add typical dependencies for a web application --><dependencies>
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-thymeleaf</artifactId>    <exclusions>        <exclusion>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-tomcat</artifactId>        </exclusion>    </exclusions></dependency>
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-undertow</artifactId></dependency>
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter</artifactId></dependency>
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-jdbc</artifactId></dependency>
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-test</artifactId>    <scope>test</scope>    <exclusions>        <exclusion>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-logging</artifactId>        </exclusion>    </exclusions></dependency>
<dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId></dependency>
<!-- Developer tools --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-devtools</artifactId></dependency>
</dependencies>
<repositories>    <repository>        <id>spring-milestone</id>        <url>https://repo.spring.io/libs-release</url>    </repository></repositories><pluginRepositories>    <pluginRepository>        <id>spring-milestone</id>        <url>https://repo.spring.io/libs-release</url>    </pluginRepository></pluginRepositories>

3.Application 启动类

@Configuration@EnableAutoConfiguration@ComponentScanpublic class Application {    private final Logger log = LoggerFactory.getLogger(Application.class);    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

4.application.properties 配置

multipart.max-request-size=100MBmultipart.max-file-size=100MBinfo.build.artifactId=@project.artifactId@info.build.version=@project.version@
# LOGGINGlogging.file=isy.loglogging.path=logsserver.undertow.accesslog.dir=logsserver.undertow.accesslog.enabled=true
spring.devtools.livereload.enabled=truespring.devtools.restart.enabled=true
spring.datasource.url=jdbc:mysql://localhost/isy?useUnicode=true&characterEncoding=utf-8&useSSL=falsespring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.schema=isy# Number of ms to wait before throwing an exception if no connection is available.spring.datasource.max-wait=10000# Maximum number of active connections that can be allocated from this pool at the same time.spring.datasource.max-active=50spring.datasource.max-idle=50# Validate the connection before borrowing it from the pool.spring.datasource.test-on-borrow=truespring.datasource.validation-query=select 1;
server.port = 8443server.context-path=
spring.resources.chain.strategy.content.enabled=truespring.resources.chain.strategy.content.paths=/**

5.说明

1.一般Application.java 是在你建的包最顶层,如我的在top.zbeboy.isy 下方,其余包都在top.zbeboy.isy下,如 top.zbeboy.isy.services,top.zbeboy.isy.domain等等,
这样启动时才能扫描到注解等等。
2.application.properties据官方文档解释有三种方式,其中一种是扫描根目录,因此放在resources目录下即可。
3.Intellij idea热加载方式是需要ctrl+F9来重新编译下才可,但有时还是会有问题,比如重新加载后丢失spring 创建的对象而报错,一般不改或增加注解没什么问题。
4.这里没有使用tomcat 而是 undertow,官方默认是tomcat,我这里是按照生产环境配置的,当然application.properties也可以配置成application-*.properties使用profile
来切换环境,这里的示例是简单配置。
5.启动类里我没有使用@SpringBootAutoConfiger,这样便于后期加缓存,spring security等,你也可以使用该方式简化配置。
6.在生产环境里应该将配置文件中的devtools设置为false。

到 github:ISY 查看更多高级配置。





0 0