自己在网上看视频整理的SpringBoot整合mybatis 做出来的基础项目 下面是我开发过程中整理的笔记
1. SpringBoot的作用
简化xml(除外mybatis)
独立的应用程序
单独的war工程 - 不需要集成tomcat插件运行
1.创建独立的Spring应用程序
2.嵌入的Tomcat,无需部署 WAR文件
3.简化Maven配置
4.自动配置Spring
5.土工生产就绪型功能,如指标,健康检查和外部配置
6.开箱即用,没有代码生成,也无需XML配置
2. SpringBoot注解
@RestController == Controller每个方法上加上@ResponseBody
@ResponseBody 返回 所有请求方法返回json格式。
@Value("${name}") 取出在配置文件中的数据
@ComponentScan(basePackages={"com.bkrn.user.controller","com.bkrn.user.service"})组件扫描,可自动发现和装配一些Bean。
@MapperScan(basePackages="com.bkrn.user.dao")//扫描dao 包
@EnableAutoConfiguration //运行注解
@ControllerAdvice 全局异常拦截器类上用的注解
@ExceptionHandler(RuntimeException.class)//捕获所有的运行异常
3. 在项目中配置pom.xml(注意项目路径问题)
4. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6. <modelVersion>4.0.0</modelVersion> 7. <groupId>com.li</groupId> 8. <artifactId>springboot-helloworld</artifactId> 9. <version>0.0.1-SNAPSHOT</version> 10. <packaging>war</packaging> 11. 12. <!-- 必须要引入继承 spring-boot-starter-parent 帮我实现很多jar的依赖管理不需要些 jar 版本 --> 13. <parent> 14. <groupId>org.springframework.boot</groupId> 15. <artifactId>spring-boot-starter-parent</artifactId> 16. <version>1.3.3.RELEASE</version> 17. </parent> 18. <dependencies> 19. <!-- Spring MVC (Spring Boot 默认集成) SpringBoot -Web 依赖即可 --> 20. <dependency> 21. <groupId>org.springframework.boot</groupId> 22. <artifactId>spring-boot-starter-web</artifactId> 23. </dependency> 24. 25. <!-- 引入freemarker依赖包 --> 26. <dependency> 27. <groupId>org.springframework.boot</groupId> 28. <artifactId>spring-boot-starter-freemarker</artifactId> 29. </dependency> 30. 31. <!--集成Mybatis --> 32. <dependency> 33. <groupId>org.mybatis.spring.boot</groupId> 34. <artifactId>mybatis-spring-boot-starter</artifactId> 35. <version>1.1.1</version> 36. </dependency> 37. 38. 39. <!-- 加入mysql依赖--> 40. <dependency> 41. <groupId>mysql</groupId> 42. <artifactId>mysql-connector-java</artifactId> 43. </dependency> 44. 45. 46. 47. 48. </dependencies> 49. 50. 51. <!-- 打包发布用的依赖 --> 52. <build> 53. <plugins> 54. <plugin> 55. <groupId>org.apache.maven.plugins</groupId> 56. <artifactId>maven-compiler-plugin</artifactId> 57. <configuration> 58. <source>1.8</source> 59. <target>1.8</target> 60. </configuration> 61. </plugin> 62. <plugin> 63. <groupId>org.springframework.boot</groupId> 64. <artifactId>spring-boot-maven-plugin</artifactId> 65. <configuration> 66. <!-- 项目中启动程序的的入口 --> 67. <mainClass>com.bkrn.App</mainClass> 68. </configuration> 69. <executions> 70. <execution> 71. <goals> 72. <goal>repackage</goal> 73. </goals> 74. </execution> 75. </executions> 76. </plugin> 77. </plugins> 78. </build> 79. 80. 81. </project> 82. |
83. |
4 项目运行类
@ComponentScan(basePackages={"com.bkrn.user.controller","com.bkrn.user.service"})//组件扫描,可自动发现和装配一些Bean。 @MapperScan(basePackages="com.bkrn.user.dao") //扫描dao包 @EnableAutoConfiguration //运行注解 publicclassApp{
publicstaticvoidmain(String[]args){ SpringApplication.run(App.class,args); } |
5 使用freemarker
在 resources 目录下建 templates文件夹存放ftl 格式文件
|
6 配属多个环境
在application.properties 里面配置 spring.profiles.active=prd
配置两个不同的 properties 分别是 application-prd.properties
application-pre.properties
想用哪一个直接在 application.properties 里面配置spring.profiles.active=prd
7 静态文件
静态文件放在resources 目录下 static 文件夹里 默认加载static文件夹里
8 打包部署
在pom.xml 中 有 <packaging>war</packaging> 打包成war包
在本地安装Maven
打开cmd
进入 到项目中 F:\Word\Eclipse\workspace\springboot-helloworld
输入 mvn clean 清除项目中target 文件
mvn package 打包
进入到项目中 target 文件中
在cmd 进入到 target 文件中java -jar 包名
启动