第一步
认识 springboot :springboot 是为了解决配置文件多, 各个组件不统一的问题, 它省去了很多配置文件, 同时实现了 spring 产品的整合.
创建 springboot 项目: 通过选择 springinit 初始化 springboot, 我们发现它的 pom.xml 拥有绝大部分的 spring 所需要的包.
第二步
打开项目的结构, 发现有了有趣的部分
在原有的 java 结构上, 公司名称下多了一级叫做自己的项目名的一个目录, 这个目录下才是相应的 Controlller 等层, 而且在此目录下面有了一个文件, 此文件可以作为主函数的入口, 相同的是再整合 mybatis 时, 我们仍然将 dao 层改为 Mapper 层, 而且这里只定义接口.
往下看, 在 resources 的目录下, 有 com,lib,static,templates,views 文件夹, 以及一个 springboot 最为主要的一个配置文件.
介绍一下这些文件夹或者文件的作用:
com 下是和上面 java 下的目录结构一样, 不多只有一个 Mapper 文件夹和一个 config 文件夹, mapper 里放对应接口的 xml 文件来提供 sql 语句, config 文件夹放的是修改过的 mybatis-config.xml 配置文件.
lib 里面放一些添加的 jar 包
static 里面放的是静
态的文件 CSS,JS,img 之类
templates 未知...
views 放我们的页面
第三步
导入 jar 包
我们只需要导入需要的 jar 包, 关于 spring 产品的 jar 包 springboot 帮我们安排好了, 我们大概需要这些 jar 包.
- <!--oracle jar 包 -->
- <dependency>
- <groupId>com.oracle</groupId>
- <artifactId>ojdbc6</artifactId>
- <version>11.1.0.6.0</version>
- <scope>system</scope>
- <systemPath>${basedir}/src/main/resources/lib/ojdbc6-11.1.0.6.0.jar</systemPath>
- </dependency>
- <!--mysql jar 包 -->
- <dependency>
- <groupId>MySQL</groupId>
- <artifactId>MySQL-connector-java</artifactId>
- <version>5.1.46</version>
- </dependency>
- <!--springboot 和 mybatis 结合的 jar 包 -->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.0</version>
- </dependency>
- <!--fastjson 包 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.47</version>
- </dependency>
- <!--springboot 模板 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- <!-- 日志 -->
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-core</artifactId>
- <version>2.10.0</version>
- </dependency>
- <!-- 使用不严格的页面标签 -->
- <!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
- <dependency>
- <groupId.NET.sourceforge.nekohtml</groupId>
- <artifactId>nekohtml</artifactId>
- <version>1.9.22</version>
- </dependency>
配置配置文件
application.properties 文件
- #spring.datasource.platform=MySQL
- #spring.datasource.url=jdbc:MySQL://localhost/mydb
- #spring.datasource.username=root
- #spring.datasource.password=hnqy
- #spring.datasource.driverClassName=com.MySQL.jdbc.Driver
- spring.datasource.platform=oracle
- spring.datasource.url=jdbc:oracle:thin:@localhost:1521/orcl
- spring.datasource.username=scott
- spring.datasource.password=tiger
- spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
- server.port=8080
- server.session-timeout=30
- server.tomcat.uri-encoding=UTF-8
- #####springboot 整合 mybatis
- mybatis.mapper-locations= classpath:/com/qy/springboot01/mapper/*Mapper.xml
- mybatis.config-location= classpath:/com/qy/springboot01/mapper/config/mybatis-config.xml
- ##### 定义别名
- #####mybatis.type-aliases-package=com.qy.springboot01.domain
- #----------------------- 日志 ------------------------------
- #启用调试日志.
- #debug = true
- #启用跟踪日志.
- #trace = true
- #LOGGING
- #日志配置文件的位置. 例如, Logback 的 `classpath:logback.xml`.
- #logging.config =
- #记录异常时使用的转换字.
- #logging.exception-conversion-Word =%wEx
- # 名称可以是确切的位置或相对于当前目录.
- #logging.file = #?
- #要保留的归档日志文件的最大数量. 仅支持默认的登录设置.
- #logging.file.max-history = 0
- #最大日志文件大小. 仅支持默认的登录设置.
- #logging.file.max-size = 10MB
- #日志级别严重性映射. 例如 `logging.level.org.springframework = DEBUG`.
- #logging.level.* =
- #日志文件的位置. 例如,`/ var
- #logging.path =
- #------------------------- 模板 ------------------------------
- ###Thymeleaf 配置
- spring.thymeleaf.prefix=classpath:/view/
- spring.thymeleaf.suffix=.HTML
- spring.thymeleaf.cache=false
- spring.thymeleaf.encoding=UTF-8
- spring.thymeleaf.mode=LEGACYHTML5
- ### 过滤中文乱码
- spring.http.encoding.force=true
- spring.http.encoding.charset=UTF-8
- spring.http.encoding.enabled=true
配置 mybatis-config 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <settings>
- <!-- 是否使用缓存 开发中禁用 -->
- <setting name="cacheEnabled" value="false"/>
- <!-- 下划线和驼峰式命名法的匹配 -->
- <setting name="mapUnderscoreToCamelCase" value="true"/>
- <!-- 使用启用懒加载机制 true 启用 -->
- <setting name="lazyLoadingEnabled" value="true"/>
- <!-- 配置 myabtis 日志 -->
- <setting name="logImpl" value="LOG4J2"/>
- <setting name="multipleResultSetsEnabled" value="true"/>
- <setting name="useColumnLabel" value="true"/>
- <setting name="useGeneratedKeys" value="true"/>
- <setting name="autoMappingBehavior" value="PARTIAL"/>
- <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
- <setting name="defaultExecutorType" value="SIMPLE"/>
- <setting name="defaultStatementTimeout" value="25"/>
- <setting name="defaultFetchSize" value="100"/>
- <setting name="safeRowBoundsEnabled" value="false"/>
- <setting name="localCacheScope" value="SESSION"/>
- <setting name="jdbcTypeForNull" value="OTHER"/>
- <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
- </settings>
- <typeAliases>
- <!-- 给类起别名 -->
- <!-- 给包中所有的类起别名 默认名字为类名 -->
- <package name=""></package>
- </typeAliases>
- </configuration>
第四步
一些细节
1. 我们发现, controller 层我们的 mybatis 需要 @Controller
但是我们的 springboot 所需要的确是 @Controller
2.service 层注解没有变化
但在 Mapper 层我们要写上 @Mapper 和 @conment 两个注解
3. 我们不需要 tomcat, 应为 springboot 内置了 tomcat, 而且启动的很快
4. 在配置文件里的细节自己去深究吧.
5. 这里只是简单的介绍, 具体实操还需要自己去熟悉.
6. 还有, config 文件夹在 mapper 下面...
附上目录结构图
来源: http://www.bubuko.com/infodetail-2907140.html