博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一分钟搭建起ssm框架
阅读量:3959 次
发布时间:2019-05-24

本文共 5313 字,大约阅读时间需要 17 分钟。

一分钟搭建起SSM框架(干货,收藏起来!!)


SSM框架是我们进入企业最基本的框架之一,ssm的重要性可想而知,下面我就带着大家简单的搭建一下最基本的ssm框架,并且简单的在一些场景使用。

在这里插入图片描述

文章目录


前言

搭建工具:idea

jdk版本:jdk1.8
数据库:Mysql5

一、Maven

使用idea,创建maven项目管理工具

在idea中创建新的project:

在这里插入图片描述

选择maven,点击next:

在这里插入图片描述

选择项目名,选择项目存放的路径,点击finish:

在这里插入图片描述

二、搭建Spring

1、maven创建成功后,在pom.xml配置文件中加入Spring相关依赖
org.springframework
spring-webmvc
5.3.3
org.springframework
spring-test
5.3.3
org.springframework
spring-jdbc
5.3.3
org.projectlombok
lombok
1.18.10
junit
junit
4.13.1
test
2、在Test中进行测试

创建配置文件applicationContext.xml,开启注解扫描

创建pojo实体类,添加注解@Component将它交给Spring管理,添加注解@Data自动生成getter、setter方法

@Component@Datapublic class Tuser {
@Value("100") private Integer id;}

断言机制进行Spring测试

1.将测试类交给Spring,注入配置文件
2.通过@Autowrite将实体类注入
3.创建Test测试方法进行测试

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext-dao.xml")public class Test1 {
@Autowired private Tuser tuser; @Test public void test1(){
Assert.assertEquals((long)100,(long)tuser.getId()); }}

若出现下图绿色对勾,则证明测试通过,Spring搭建完成

在这里插入图片描述

三、搭建Mybatis

1.注入Mybatis需要的依赖
mysql
mysql-connector-java
8.0.11
org.mybatis
mybatis
3.5.6
org.mybatis
mybatis-spring
2.0.6
org.mybatis.generator
mybatis-generator-core
1.4.0
com.alibaba
druid
1.2.4
2.进行Mybatis测试

在applicationContext.xml中加入配置,连接数据库

Mybatis通过在Maven中注入逆向工程的代码后,就可以逆向工程生成mapper、pojo、xml文件

在Test中创建测试方法测试Mybatis与Spring之间的整合

1.@AutoWirte注入逆向工程生成的Mapper文件
2.执行mapper中的方法,通过断言机制测试

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext-dao.xml")public class Test1 {
@Autowired private SUserMapper mapper; @Autowired private Tuser tuser; @Test public void test1(){
Assert.assertEquals((long)100,(long)tuser.getId()); } @Test public void test2(){
List
sUsers = mapper.selectByExample(null); Assert.assertEquals(7,sUsers.size()); }}

测试通过,也出现绿色对勾,证明spring与Mybatis整合成功

四、搭建SpringMVC

1.加入相关依赖文件
com.fasterxml.jackson.core
jackson-databind
2.11.4
javax.servlet
javax.servlet-api
4.0.1
provided
jstl
jstl
1.2
2.进行spring mvc的相关配置

在web.xml中配置前端控制器,将applicationContext.xml配置文件加载进去

ssm
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
ssm
/

加入tomcat插件,通过插件运行

org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
9999
/
utf-8

配置applicationContext.xml

//使用最新的处理器适配器和处理器映射器	
//配置多媒体视图解析器
//配置视图解析器
//加载静态资源
3.进行测试spring mvc

创建业务层,创建接口定义方法

创建接口实现类,重写里面的方法,通过@Service将实现类交给Spring管理。将Mapper文件注入,调用方法

创建控制层,通过@Controller注解,证明该类为控制层。注入业务层,进行整合

@Controllerpublic class TestController {
@Autowired private SUserService service; //@RequestMapping同步请求,返回页面 @RequestMapping("/test") public String test5(Model model){
List
user = service.selectAll(); model.addAttribute("user",user); return "list"; } //@ResponseBody异步请求,返回数据 @GetMapping("/test1") @ResponseBody public Map test1(){
Map map = new HashMap(); map.put("userName","admin"); return map; }}

Spring、Mybatis、SpringMVC整合完成。

写的时候时间有些紧迫,上面有些代码没有写出来,如果有什么问题留言!!

转载地址:http://euazi.baihongyu.com/

你可能感兴趣的文章
POJ3903(dp,最长上升子序列,最基础题)
查看>>
POJ1836-Alignment(最长上升子序列)
查看>>
POJ 1251 Jungle Roads(最小生成树简单题)
查看>>
HDU 1690---Bus System(Floyd模板题+合理定义INF)
查看>>
POJ 2240---Arbitrage(Floyd的dp思想)
查看>>
Dijkstra算法---模板
查看>>
POJ 3680(费用流)
查看>>
校oj10532: 生成字符串(dp,最优状态转移)
查看>>
平衡二叉树(AVL树)
查看>>
POJ1521---哈夫曼编码,求最优WPL
查看>>
POJ---2010(Moo University - Financial Aid,优先队列)
查看>>
POJ---3662(Telephone Lines,最短路+二分*好题)
查看>>
L2-007. 家庭房产(并查集)
查看>>
L2-016. 愿天下有情人都是失散多年的兄妹(搜索)
查看>>
L2-019. 悄悄关注
查看>>
POJ 3468 A Simple Problemwith Integers(SplayTree入门题)
查看>>
营业额统计 HYSBZ - 1588 (伸展树简单应用)
查看>>
HDU 1890 Robotic Sort(伸展树---反转应用)
查看>>
POJ 3580 SuperMemo(伸展树的几个基本操作)
查看>>
(十) Web与企业应用中的连接管理
查看>>