为了账号安全,请及时绑定邮箱和手机立即绑定

SpringBoot项目实战(2):集成SpringBoot

标签:
Java

项目结构图
图片描述
1.pom文件添加SpingBoot依赖

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sbm</groupId>
    <artifactId>SpringBoot_Maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>
    <dependencies>
    <!--支持 Web 应用开发,包含 Tomcat 和 spring-mvc。 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!--模板引擎-->
    <!--  <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <!--Json Support-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.1.43</version>
    </dependency>
    <!--springboot中修改完文件后自动reload的插件,修改完文件Ctrl + F9 Make一下就可以-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
        <version>1.2.3.RELEASE</version>
    </dependency>
    <!--springboot中修改完文件后自动reload的插件 end-->
</dependencies>
<build>
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.3.RELEASE</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>
</build>
        <!--配置远程仓库地址-->
<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>
</project>

2.编写SB项目启动入口类Applaction.java

package com.sbm;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * sbm
 * Created by yadong.zhang on com.sbm.application
 * User:yadong.zhang
 * Date:2016/10/20
 * Time:18:15
 */
/**
 * 1).@SpringBootApplication标注启动配置入口,run()方法会创建一个Spring应用上下文(Application Context)。
 * SpringBoot通过启动内嵌的Servlet容器(默认tomcat)用来处理Http请求。
 * 2).@RestController是特殊的Controller,他的返回值直接作为Http Response的Body部分返回给浏览器
 * 3).Spring WebMvc框架会将Servlet容器里收到的Http请求根据路径分发到对应的@Controller下进行处理。
 */
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan
//@RestController
public class Applaction {
    @RequestMapping("/")
    public String index(){
        return "Spring Boot Application...";
    }
    public static void main(String[] args) {
        SpringApplication.run(Applaction.class, args);
    }
}

3.编写测试的Controller类

package com.sbm.controller;
import com.sbm.service.IMessageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * sbm
 * Created by yadong.zhang on com.sbm.controller
 * User:yadong.zhang
 * Date:2016/10/20
 * Time:18:26
 */
@Controller
public class HelloController {
    @RequestMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name, Model model) {
        model.addAttribute("name", name);
        model.addAttribute("age","25");
        model.addAttribute("sex","man");
        model.addAttribute("birth",new Date());
        return "hello";
    }
    @RequestMapping("/json")
    @ResponseBody
    public Map<String,Object> json(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name","Flyat");
        map.put("age","25");
        map.put("sex","man");
        return map;
    }
}

4.看效果
图片描述
图片描述
图片描述

前台页面渲染是使用的Freemarker模板技术,下节总结Spring Boot 整合Freemarker

项目地址:GitHub地址,点击查看

最后的最后,在呼吁一下:

图片描述
这个标签该更新一下了,这篇文章的标签我居然不知道该选什么...Spring没有,Spring Boot也没有,Maven更没有。

Github源码 - SpringBoot多模块项目

我可以对一个人无限的好,前提是值得。 ——慕冬雪

点击查看更多内容
26人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消