springboot搭建

1.首先创建Mavne项目。

springboot搭建_第1张图片springboot搭建_第2张图片springboot搭建_第3张图片

第二步骤:

添加修改pom.xml

[html]   view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0modelVersion>  
  4.   
  5.     <groupId>webgroupId>  
  6.     <artifactId>spring_bootartifactId>  
  7.     <version>0.0.1-SNAPSHOTversion>  
  8.     <packaging>warpackaging>  
  9.   
  10.     <name>spring_bootname>  
  11.     <url>http://maven.apache.orgurl>  
  12.   
  13.     <parent>  
  14.         <groupId>org.springframework.bootgroupId>  
  15.         <artifactId>spring-boot-starter-parentartifactId>  
  16.         <version>1.4.2.RELEASEversion>  
  17.     parent>  
  18.   
  19.     <properties>  
  20.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  21.         <boot.version>1.4.2.RELEASEboot.version>  
  22.     properties>  
  23.   
  24.     <dependencies>  
  25.           
  26.         <dependency>  
  27.             <groupId>org.springframework.bootgroupId>  
  28.             <artifactId>spring-boot-starter-webartifactId>  
  29.             <version>${boot.version}version>  
  30.         dependency>  
  31.         <dependency>  
  32.             <groupId>org.springframework.bootgroupId>  
  33.             <artifactId>spring-boot-starter-testartifactId>  
  34.             <scope>testscope>  
  35.         dependency>  
  36.           
  37.         <dependency>  
  38.             <groupId>org.springframework.bootgroupId>  
  39.             <artifactId>spring-boot-starter-tomcatartifactId>  
  40.             <scope>providedscope>  
  41.         dependency>  
  42.           
  43.         <dependency>  
  44.             <groupId>org.mybatis.spring.bootgroupId>  
  45.             <artifactId>mybatis-spring-boot-starterartifactId>  
  46.             <version>1.1.1version>  
  47.         dependency>  
  48.           
  49.         <dependency>  
  50.             <groupId>org.springframework.bootgroupId>  
  51.             <artifactId>spring-boot-starter-jdbcartifactId>  
  52.         dependency>  
  53.           
  54.         <dependency>  
  55.             <groupId>mysqlgroupId>  
  56.             <artifactId>mysql-connector-javaartifactId>  
  57.             <version>5.1.6version>  
  58.         dependency>  
  59.   
  60.   
  61.           
  62.         <dependency>  
  63.             <groupId>org.apache.tomcat.embedgroupId>  
  64.             <artifactId>tomcat-embed-jasperartifactId>  
  65.             <scope>providedscope>  
  66.         dependency>  
  67.   
  68.           
  69.         <dependency>  
  70.             <groupId>com.alibabagroupId>  
  71.             <artifactId>druidartifactId>  
  72.             <version>1.0.5version>  
  73.         dependency>  
  74.           
  75.         <dependency>  
  76.             <groupId>org.springframework.bootgroupId>  
  77.             <artifactId>spring-boot-starter-actuatorartifactId>  
  78.         dependency>  
  79.           
  80.         <dependency>  
  81.             <groupId>com.alibabagroupId>  
  82.             <artifactId>fastjsonartifactId>  
  83.             <version>1.1.43version>  
  84.         dependency>  
  85.   
  86.     dependencies>  
  87.   
  88.     <build>  
  89.         <plugins>  
  90.             <plugin>  
  91.                 <groupId>org.springframework.bootgroupId>  
  92.                 <artifactId>spring-boot-maven-pluginartifactId>  
  93.                 <dependencies>  
  94.                     <dependency>  
  95.                         <groupId>org.springframeworkgroupId>  
  96.                         <artifactId>springloadedartifactId>  
  97.                         <version>1.2.5.RELEASEversion>  
  98.                     dependency>  
  99.                 dependencies>  
  100.             plugin>  
  101.             <plugin>  
  102.                 <artifactId>maven-compiler-pluginartifactId>  
  103.                 <configuration>  
  104.                     <source>1.8source>  
  105.                     <target>1.8target>  
  106.                 configuration>  
  107.             plugin>  
  108.   
  109.         plugins>  
  110.     build>  
  111.   
  112. project>  
以上特意注意2点 。如果想要添加jsp,(org.apache.tomcat.embed 是支持jsp格式,Javax.serlet.jsp.jstl是支持jsp一些语法)标准的2个。否则加载不了jsp文件。
如果不想用jsp,使用模板的话,具体jsp和模板的区别,可以单独百度。使用模板可以用一下内容



org.springframework.boot
spring-boot-starter-thymeleaf

3.添加修改配置文件。

application.properties

[plain]   view plain  copy
  1. spring.datasource.driver-class-name=com.mysql.jdbc.Driver  
  2. spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&rewriteBatchedStatements=TRUE  
  3. spring.datasource.username=root  
  4. spring.datasource.password=****  
  5. spring.datasource.max-active=10  
  6. spring.datasource.max-idle=5  
  7. spring.datasource.min-idle=0  
  8.   
  9.   
  10. server.port=8082  
  11. server.session.timeout=10  
  12. server.tomcat.max-threads=800  
  13. server.tomcat.uri-encoding=UTF-8  
  14. # MVC  分版本  
  15. spring.mvc.view.prefix=/WEB-INF/views/  
  16. spring.mvc.view.suffix=.jsp  


这里配置的是jsp的位置以及数据库一些配置。
添加文件。

springboot搭建_第4张图片


4.添加程序

[java]   view plain  copy
  1. package web.spring_boot;  
  2.   
  3.   
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6.   
  7.   
  8. @Configuration
    @EnableAutoConfiguration
    @ComponentScan("cn.yinchong.controller")
      
  9. public class TestA {  
  10.     public static void main(String[] args) {  
  11.         SpringApplication.run(TestA.class);  
  12.     }  
  13. }  


[java]   view plain  copy
  1. package web.spring_boot.controller;  
  2.   
  3.   
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7.   
  8. import org.springframework.boot.builder.SpringApplicationBuilder;  
  9. import org.springframework.boot.web.support.SpringBootServletInitializer;  
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.ui.Model;  
  12. import org.springframework.web.bind.annotation.RequestMapping;  
  13. import org.springframework.web.bind.annotation.ResponseBody;  
  14.   
  15.   
  16. @Controller  
  17. public class SampleController extends SpringBootServletInitializer {  
  18.   
  19.   
  20.     @Override  
  21.     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
  22.         return application.sources(SampleController.class);  
  23.     }  
  24.   
  25.   
  26.     @RequestMapping("/")  
  27.     @ResponseBody  
  28.     String home() {  
  29.         System.out.println("xxxxxxxxxxxxx");  
  30.         return "Hello 22!";  
  31.     }  
  32.   
  33.   
  34.     //这里指定是条状的jsp界面  
  35.     @RequestMapping(value = "/index")  
  36.     public String index(Model model) {  
  37.         model.addAttribute("sb""this is my fries测试不是好领导了副经理看fjldj 1123123");  
  38.         return "index";  
  39.     }  
  40.   
  41.   
  42.     @RequestMapping(value = "json")  
  43.     @ResponseBody  
  44.     public Map mytest() {  
  45.         Map map = new HashMap();  
  46.         map.put("name""Ryan");  
  47.         map.put("age""3");  
  48.         map.put("sex""man");  
  49.         return map;  
  50.     }  
  51. }  



index.jsp如下:
[html]   view plain  copy
  1. >  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>Insert title heretitle>  
  6. head>  
  7. <body>xxxxxxxxxxxxxx  
  8. ${sb}  
  9. body>  
  10. html>  

你可能感兴趣的:(springboot搭建)