Spring Boot微框架使用——从环境搭建到整合MyBatis

  1. springboot的引言
    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的 初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不 再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应 用开发领域(rapid application development)成为领导者。

springboot(微框架) = springmvc(控制器) + spring(项目管理)

  1. springboot的特点
    1.微框架,与spring4一起诞生,如@RestController
    2.springboot可以创建独立运行的应用而不依赖于容器
    3.内嵌Tomcat,无需部署WAR文件
    4.提供maven极简配置,缺点是会引入很多你不需要的包
    5.根据项目来依赖,从而自动配置spring,需要什么配置什么,做到了可控
    6.提供了监控、应用的健康等功能
    7.简化配置,不需要配置过多的xml,只需要用注解的方式就可以达到同样的效果

  2. springboot的环境搭建
    环境要求:

MAVEN 3.x+
Spring FrameWork 4.x+
JDK7.x +
Spring Boot 1.5.x+
3.1 项目中引入依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
</parent>

<dependencies>
    <!--引入springboot的web支持-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

3.2 引入配置文件

项目中src/main/resources/application.yml

3.3 建包并创建控制器

//在项目中创建指定的包结构

/*
   com
      +| xxx
          +| controller */ 
                  @Controller
                    @RequestMapping("/hello")
                    public class HelloController {
                        @RequestMapping("/hello")
                        @ResponseBody
                        public String hello(){
                            System.out.println("======hello world=======");
                            return "hello";
                        }
                    }

3.4 编写入口类

//在项目中如下的包结构中创建入口类 Application

/*
  com
    +| xxx                  */
            @SpringBootApplication
            public class Application {
                public static void main(String[] args) {
                    SpringApplication.run(Application.class,args);
                }
            }

3.5 运行main启动项目

o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8989 (http)
com.xxxx.Application : Started Application in 2.152 seconds (JVM running for 2.611)

//说明: 出现以上日志说明启动成功
3.6 访问项目

//注意: springboot的项目默认没有项目名
//访问路径: http://localhost:8080/hello/hello
4. 启动tomcat端口占用问题
在application.yml配置

server:
port: 8989 #用来指定内嵌服务器端口号
context-path: /springboot #用来指定项目的访问路径
5. springboot相关注解说明
说明:

spring boot通常有一个名为 xxxApplication的类,入口类中有一个main方法, 在main方法中使用
SpringApplication.run(xxxApplication.class,args)启动springboot应用的项目。

@RestController: 就是@Controller+@ResponseBody组合,支持RESTful访问方 式,返回结果都是json字符串。

@SpringBootApplication 注解等价于下面三个注解:
@Configuration 项目启动时自动配置spring 和 springmvc 初始搭建
@EnableAutoConfiguration 自动与项目中集成的第三方技术进行集成
@ComponentScan 扫描入口类所在子包以及子包后代包中注解

  1. springboot中配置文件的拆分
    #说明: 在实际开发过程中生产环境和测试环境有可能是不一样的 因此将生产中的配置和测试中的配置拆分开,是非常必要的在springboot中也提供了配置文件拆分的方式. 这里以生产中项名名称不一致为例:
    生产中项目名为: cmfz
    测试中项目名为: springboot
    端口同时为: 8080

拆分如下:
#主配置文件:
application.yml #用来书写相同的的配置
server:
port: 8080 #生产和测试为同一个端口

#生产配置文件:
    application-pord.yml
      server:
        context-path: /cmfz
#测试配置文件:
    application-dev.yml
      server:
        context-path: /springboot
  1. springboot中集成jsp展示
    7.1 引入jsp的集成jar包
jstl jstl 1.2 org.springframework.boot spring-boot-starter-tomcat org.apache.tomcat.embed tomcat-embed-jasper 7.2 引入jsp运行插件 springboot_day1 org.springframework.boot spring-boot-maven-plugin 7.3 配置视图解析器

#在配置文件中引入视图解析器
spring:
mvc:
view:
prefix: / # /代表访问项目中webapp中页面
suffix: .jsp
7.4 启动访问jsp页面

http://localhost:8989/cmfz/index.jsp
8. springboot集成mybatis
8.1 引入依赖

org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.3 com.alibaba druid 1.1.12 mysql mysql-connector-java 5.1.38 >说明:由于springboot整合mybatis版本中默认依赖mybatis 因此不需要额外引入mybati版本,否则会出现冲突 8.2 配置配置文件

spring:
mvc:
view:
prefix: /
suffix: .jsp
datasource:
type: org.apache.commons.dbcp.BasicDataSource #指定连接池类型
driver-class-name: com.mysql.jdbc.Driver #指定驱动
url: jdbc:mysql://localhost:3306/cmfz #指定url
username: root #指定用户名
password: xxxx #指定密码
8.3 加入mybatis配置

#配置文件中加入如下配置:

mybatis:
mapper-locations: classpath:com/baizhi/mapper/*.xml #指定mapper配置文件位置
type-aliases-package: com.xxxx.entity #指定起别名来的类
//入口类中加入如下配置:
@SpringBootApplication
@MapperScan(“com.baizhi.dao”) //必须在入口类中加入这个配置
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
8.4 建表

8.5 开发实体类

8.6 开发DAO接口以及Mapper

8.7 开发Service以及实现

8.8 引入测试依赖

org.springframework.boot spring-boot-starter-test test 8.9 编写测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TestClazzService {

@Autowired
private ClazzService clazzService;

@Test
public void test(){
    List<Clazz> all = clazzService.findAll();
    for (Clazz clazz : all) {
        System.out.println(clazz);
    }

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值