zoukankan      html  css  js  c++  java
  • spring boot 实践

    二、实践

    一些说明:

    项目IDE采用Intellij(主要原因在于Intellij颜值完爆Eclipse,谁叫这是一个看脸的时代)

    工程依赖管理采用个人比较熟悉的Maven(事实上SpringBoot与Groovy才是天生一对)

    1.预览:

    (1)github地址

    https://github.com/djmpink/springboot-mybatis

    git :  https://github.com/djmpink/springboot-mybatis.git

    (2)完整项目结构

     

     

     

    (3)数据库

    数据库名:test 

    【user.sql】

    2.Maven配置

    完整的【pom.xml】配置如下:

    3.主函数

    【Application.java】包含main函数,像普通java程序启动即可。

    此外,该类中还包含和数据库相关的DataSource,SqlSeesion配置内容。

    注:@MapperScan(“cn.no7player.mapper”) 表示Mybatis的映射路径(package路径)

    4.Controller

    请求入口Controller部分提供三种接口样例:视图模板,Json,restful风格

    (1)视图模板

    返回结果为视图文件路径。视图相关文件默认放置在路径 resource/templates下:

    (2)Json

    返回Json格式数据,多用于Ajax请求。

    (3)restful

    REST 指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是 RESTful。

    此外,有一款RESTFUL接口的文档在线自动生成+功能测试功能软件——Swagger UI,具体配置过程可移步《Spring Boot 利用 Swagger 实现restful测试》

    5.Mybatis

    配置相关代码在Application.java中体现。

    (1)【application.properties】

    注意,在Application.java代码中,配置DataSource时的注解

    @ConfigurationProperties(prefix=“spring.datasource”) 

    表示将根据前缀“spring.datasource”从application.properties中匹配相关属性值。

    (2)【UserMapper.xml】

    Mybatis的sql映射文件。Mybatis同样支持注解方式,在此不予举例了。

    (3)接口UserMapper

    三、总结

    (1)运行 Application.java

    (2)控制台输出:

     

    …..(略过无数内容)

    (3)访问:

    针对三种控制器的访问分别为:

    视图:

    http://localhost:8080/hello?name=7player

     

    Json:

    http://localhost:8080/getUserInfo

     

    Restful(使用了swagger):

    http://localhost:8080/swagger/index.html

     

    四、参阅

    《Spring Boot – Quick Start》

    http://projects.spring.io/spring-boot/#quick-start

    《mybatis》

    http://mybatis.github.io/mybatis-3/

    《使用 Spring Boot 快速构建 Spring 框架应用》

    http://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/

    《Using @ConfigurationProperties in Spring Boot》

    http://www.javacodegeeks.com/2014/09/using-configurationproperties-in-spring-boot.html?utm_source=tuicool

    《Springboot-Mybatis-Mysample》

    https://github.com/mizukyf/springboot-mybatis-mysample

    《Serving Web Content with Spring MVC》

    http://spring.io/guides/gs/serving-web-content/

    《理解RESTful架构》

    http://www.ruanyifeng.com/blog/2011/09/restful

     

    附录:

    Spring Boot 推荐的基础 POM 文件

    名称

    说明

    spring-boot-starter

    核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持。

    spring-boot-starter-amqp

    通过 spring-rabbit 支持 AMQP。

    spring-boot-starter-aop

    包含 spring-aop 和 AspectJ 来支持面向切面编程(AOP)。

    spring-boot-starter-batch

    支持 Spring Batch,包含 HSQLDB。

    spring-boot-starter-data-jpa

    包含 spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA。

    spring-boot-starter-data-mongodb 

    包含 spring-data-mongodb 来支持 MongoDB。

    spring-boot-starter-data-rest

    通过 spring-data-rest-webmvc 支持以 REST 方式暴露 Spring Data 仓库。

    spring-boot-starter-jdbc

    支持使用 JDBC 访问数据库。

    spring-boot-starter-security

    包含 spring-security。

    spring-boot-starter-test

    包含常用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。 

    spring-boot-starter-velocity

    支持使用 Velocity 作为模板引擎。

    spring-boot-starter-web

    支持 Web 应用开发,包含 Tomcat 和 spring-mvc。

    spring-boot-starter-websocket

    支持使用 Tomcat 开发 WebSocket 应用。

    spring-boot-starter-ws

    支持 Spring Web Services。

    spring-boot-starter-actuator

    添加适用于生产环境的功能,如性能指标和监测等功能。

    spring-boot-starter-remote-shell

    添加远程 SSH 支持。

    spring-boot-starter-jetty

    使用 Jetty 而不是默认的 Tomcat 作为应用服务器。

    spring-boot-starter-log4j

    添加 Log4j 的支持。

    spring-boot-starter-logging

    使用 Spring Boot 默认的日志框架 Logback。

    spring-boot-starter-tomcat

    使用 Spring Boot 默认的 Tomcat 作为应用服务器。

     

     转自:http://7player.cn/2015/08/30/%E3%80%90%E5%8E%9F%E5%88%9B%E3%80%91%E5%9F%BA%E4%BA%8Espringboot-mybatis%E5%AE%9E%E7%8E%B0springmvc-web%E9%A1%B9%E7%9B%AE/

     

  • 相关阅读:
    时序图和类图
    word文档一个表格分割成两个表格
    Spring 报错:Error creating bean with name
    怎么让线条快速选中并组合多个形状
    [扩展阅读] Python 函数修饰符(装饰器)的使用
    第039 类和对象 拾遗
    吴恩达深度学习 第一课第三周课后编程作业
    第038讲:类和对象:继承 课后测试题及答案
    廖雪峰Python教程--使用模块
    第037讲:类和对象:面向对象编程
  • 原文地址:https://www.cnblogs.com/fangyuan303687320/p/5616816.html
Copyright © 2011-2022 走看看