用maven 搭建 Spring boot工程

1.创建Maven项目

用maven 搭建 Spring boot工程_第1张图片
2.选择项目类型
用maven 搭建 Spring boot工程_第2张图片

3.选择项目
用maven 搭建 Spring boot工程_第3张图片

4.编写项目组和名称-finish即可
用maven 搭建 Spring boot工程_第4张图片

5.修改pom.xml文件
加入如下配置:

 
<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>1.5.6.RELEASEversion>
parent>

6.pom.xml中添加依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>

7.pom.xml中添加编译插件

<build>
    <plugins>
        
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
        plugin>
    plugins>
build>

8.基础包和类
用maven 搭建 Spring boot工程_第5张图片

9.App.java

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

@SpringBootApplication
public class App {

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

10.OneController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class OneController {

    @RequestMapping("/")
    @ResponseBody
    public String index(){
        return "hello spring boot";
    }
}


11.启动项目
运行 App.java 中的 main方法
12.访问项目
http://localhost:8080/
会访问到 OneController.java 中的index方法

以上即可完成spring boot的项目搭建;
然而当我尝试加入数据源配置时,遇到了各种奇葩的问题,搞了一上午,终于找到了一个能正常运行的配置:

首先为了实现dao层类的注入,加入如下代码:

SpringBootApplication
/*@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})*/
@ComponentScan(basePackages= {"com.test.userManage.dao","com.test.userManage.controller"})
public class App {  .........  }

然后,在pom.xml中加入新的依赖:

     <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>


         <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.1.1version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
          dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>

最后,创建配置文件application.properties(若工程中已有,则忽略),在此文件中加入数据源配置:
(我使用的是mysql数据库,请他类型可做参考)

pring.datasource.url=jdbc:mysql://ip:3306/preetccrm?useSSL=false
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=18800
spring.datasource.jdbc-interceptors=ConnectionState;SlowQuyReport(thresh
old=0)

需要注意的是:输入用户名和密需要注意的是:输入用户名和密码时,要保证前后没有空格,否则会连接失败,因为连接时把空格也算作字符了,亲身经历,这个地方困扰了好久,大家引以为戒。

你可能感兴趣的:(maven,springboot,数据源配置,spring,java基础)