美文网首页我爱编程
springboot 创建SSM应用

springboot 创建SSM应用

作者: 阿西Enzo | 来源:发表于2018-05-11 11:30 被阅读0次

springboot 创建SSM应用

@(springboot)[spring|ssm]


[TOC]

创建工程

    1. 使用工具

使用IDEA 开发, 也可以使用spring.io创建新工程

  • 2.选择组件

我们需要实现的功能有: SpringMVC + Spring + Mybatis

  1. web: web
  2. Template engine: Thymeleaf 模板引擎(Thymeleaf 是springboot 默认且推荐使用的,同样也支持freemark、mustach、groovy 可供选择. 特别指出的是springboot 是不推荐使用jsp的,如果需要使用jsp的话,需要自己手动修改相应配置与引入相关依赖)
  3. SQL: MyBatis s数据库持久框架Mybatis+Spring 组件是提供starter的,安装即用,无需配置
  4. 其他: 如NoSql配置非关系数据库 如Redis、MongoDB等; Integration: 可以选择开箱即用的JMS组件如kafka\RabbitMQ等; 如Cloud相关,可以结合Spring Cloud构件云服务(Spring cloud 默认服务注册使用的Eureka ,更加强大的是他还带负载均衡组件ribbon等)
@SSM组件组成
@Web版|center
  • 3.工程目录


    @工程目录

pom: maven项目构建文件
application.yml: 项目配置文件
Application.java: 工程启动入口程序使用@SpringBootApplication声明

SSM基础配置

MVC 开箱即用,无需配置(需要引用 web starter 与 template starter)
SQL:

  • 1.添加数据库相关依赖
<!--druid 数据连接池与PG数据库驱动依赖-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.9</version>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>
  • 2.配置数据源

这里sqlSession\sessionfactory\transactionmanager都是自动进行配置的,无需自己动手。(如果有自定义需要,可以自定义configuration 覆盖指定Bean。 Spring 在DB上主流应用都进行了自动组织的检测与自动配置,你只需要选择所提供的starter即可,复杂的线程池、JTA都有现成的starter可供使用, 自定义配置的需求很少,此处不做拓展)

spring:
  datasource:
    name: xxxx
    username: name
    password: xxxxxxx
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://serverIP:PORT/xxxxx?useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull
    druid:
      max-active: 5
      min-idle: 0
      initial-size: 3

这里我们使用druid 进行数据库连接管理,维护线程池,同时他还提供数据库连接监控,可以通过网页: http://serverIP:port/context/druid 进行监控

调试

# 开启sql调试 debug: true
logging:
  level:
    com:
      enzo:
        demo: DEBUG
  file: ../logs/spring-boot-logging.log
# 开启app 调试 application.yml
debug: true
@调试|center

创建demo

@项目结构|center
源代码

遇到的错误

未找到Mapper文件

  • 错误描述: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
  • 追踪: 我们定义的mapper.xml 文件放在包内目录中,虽然使用了 mybatis:mapper-locations 申明了mapper文件扫描路径, 但是使用maven打包时,包内的非java文件是不会打包进jar中的,所以工程找不到。如果是将mapper文件放在resources目录下,就不会出现这个问题
  • 解决: 在pom build中声明,将包内xml资源一同打包
<resource>
    <directory>${basedir}/src/main/java</directory>
    <includes>
        <include>**/*.xml</include>
    </includes>
</resource>

相关文章

网友评论

    本文标题:springboot 创建SSM应用

    本文链接:https://www.haomeiwen.com/subject/gwcfdftx.html