springboot项目集成ssm

最近在面试,面试官问我会不会自己搭建ssm框架,我说会,然后问到了一点知识没回答上来,可能当时搭建的时候很多东西没有理解透彻,于是就重新搭建一下,有一个深刻的记忆。也特此记录一下。

如何搭建springboot这里就不多说了,请看这篇搭建springboot项目

接下来开始集成一个ssm项目:

第一步:引入spring,springmvc,mybatis,mysql的依赖

<!-- 对JDBC数据库的支持 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<!-- mysql jdbc驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!-- 面向切面编程的支持,包括spring-aop和AspectJ -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<!-- spring boot整合mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<!-- 对全栈web开发的支持,包括Tomcat和spring-webmvc 此包可以用于返回JSP视图-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- thymeleaf模板引擎,访问静态资源 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!--开启热部署,需要在maven下配置才会生效-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!--lombox依赖,用来省去get、set、tostring方法-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<!--springboot test单元测试-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

复制即可使用。

第二步:创建包,结构如下。

第三步:使用spring注解注入

控制层

服务层

mybatis支持的xml标签,写入一个简单的查询

第四步:配置数据源

这个就不复制了,网上都有范例,直接搜索就有的。

第五步:最后写一个查询列表,小试牛刀

因为查询列表,所以用list接受,返回出去。然后查看结果:

这么多英语,但是只要认识数字就好了,500说明我们的代码错了,然后到控制台看下是什么原因,排查下问题。

可以清楚的看到我查询出来的长度已经输出出来,说明方法是没问题的。

解决如下:

控制台出现了这个异常:Error resolving template "xxx", template might not exist or might not be accessible by any of the configured Template Resolvers

解决办法:在报出这个异常的方法上添加注解@ResponseBody

@ResponseBody 这个注解, 就表明该方法的返回值直接写入到 HTTP Response Body 中。 这就是说,如果返回的是JSON, 就得必须添加 @ResponseBody 这个注解,一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。

比如异步获取json数据,加上@responsebody后,会直接返回json数据。

controller层加注解@Controller 和@RestController都可以在前端调通接口,但是二者的区别在于,当用前者的时候在方法上必须添加注解@ResponseBody,如果不添加@ResponseBody,就会报上面错误,因为当使用@Controller 注解时,spring默认方法返回的是view对象(页面)。而加上@ResponseBody,则方法返回的就是具体对象了。@RestController的作用就相当于@Controller+@ResponseBody的结合体

看到这里,相信你已经明白了。我这里换成@RestController

返回结果如图,这里只贴一部分。

      至此使用springboot搭建ssm框架就完成了,个人感想:使用springboot确实很方便,减少了许多的复杂繁琐的配置文件,每天进步一点点。如有表达不当的地方,还望大家指正。

      每日一笑:程序员面试:一个程序员去一家公司面试,面试官拿着他的简历说:“你不是16年毕业的吗,按道理来说不是才有两年工作经验吗,简历上怎么写着三年经验啊”,然后程序员语重心长说道:“多的一年是我的加班”。面试官顿时竟无语凝噎,默默点头不说话

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的蚂蚁【你若】

如果帮助到了您,一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值