springboot(一)
springboot(二)
之前我整理了2偏关于springboot的文章,本次主要说明开发当中最基本的操作。springboot继承mybatis,用generator生成entity,mapper,mapper.xml.另外集成了jsp,不过spring官方不推荐springboot集成jsp,因为集成后就代表微服务丧失了一些其他功能,后面会做介绍。
开发工具:idea
jdk:1.8
项目框架:springboot+mybatis

使用JSP渲染Web视图
pom文件引入以下依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
在application.properties创建以下配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
后台代码
@Controller
public class IndexController {
@RequestMapping("/index")
public String index() {
return "index";
}
}
这里要注意,@Controller,然后在方法上注明@RequestMapping,返回String,这种是返回视图,如果把@Controller换成@RestController或者在@RequestMapping上面加上@ResponseBody那么就返回json了.但是新建的springboot项目没有webapp,WEB-INF怎么办,自己新建就可以了


springboot学习手册
这是我的github,这上面的内容会不断更新,我把他作为自己练习的一个小demo.

首先要说明的是generatorConfig.xml,在这面有个classPathEntry loaction,这个地址是要访问你本地mysql-connector-java-5.1.25.jar这个包,这个包我也放在了项目里面.
sql文件夹下面有对应的sql语句.生成对应的mapper,model还有mapper.xml以后,书写controller层。对数据库操作进行测试。

我在最上面加了jsp的配置,然后启动项目报错。

然后我把jsp配置放到下面来,竟然不报错了,我以为就这么结束了,发现路漫漫其修远兮,吾将上下而求索,路没这么简单.

然后我操作generator就报错
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
然后去掉jsp的配置就好了
所以用springboot的话不推荐使用jsp,用html即可,用了jsp,其他功能可能用不了.
springboot的出现其实主要还是以返回json为主,个人推荐前后端分离。
原因:以前开发基本就是一个网页(前端),一个后台,俗称javaWeb.所以前后端连在一起,用jsp开发.现在呢.越来越多的公司不在满足于一个前端网页,微信小程序,公众号,移动端app,网页,可能还有其他前端,如果前后端一起,必然需要开发多套服务端供其他客户端用。其实前端也希望可以一处编写,然后在各个平台适配运行,facebook的react技术其实基本实现了网页端和移动端,但是针对网页,安卓,ios又要编写不同的代码,只是在不同设备上进行判断,比如判断是ios的那就运行ios代码,所以对前端要求还是非常高的.
网友评论