ssm整合至版本之谜+CVweb.xml报错的解决方法+笔记+mockMvc使用+实训+day0806
选择版本一定要选择人数最多的合适的版本,最新的版本并不是最好的,我对最新的版本用ssm作了一个测试,发现依赖版本的选择真的很重要,尤其是一下版本
各种要版本如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.neuedu</groupId>
<artifactId>ssmday0806</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>5.2.7.RELEASE</spring.version>
<springorm.version>5.2.7.RELEASE</springorm.version>
<servlet.version>3.1.0</servlet.version>
<jsp.version>2.3.1</jsp.version>
<jstl.version>1.2</jstl.version>
<mysql.version>5.1.47</mysql.version>
<mybatis.version>3.4.6</mybatis.version>
<mybatis-spring.version>1.3.2</mybatis-spring.version>
<lombok.version>1.18.12</lombok.version>
<druid.version>1.1.12</druid.version>
<common.version>6.0.0</common.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>org.duracloud</groupId>
<artifactId>common</artifactId>
<version>6.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
笔记如下:
### day0806
* 复习ssm框架的搭建和使用
### 流程
#### 1、搭建框架
* springmvc springioc mybatis,简称ssm
* 搭建springmvc,第一步骤创建项目,
打包方式
* 添加依赖,就是导包
* 配置前端控制器(DispatcherServlet),
给DispatcherServlet添加配置文件 springmvc.xml
* springmvc.xml开启注解驱动,
* 配置包路径扫描
* 配置视图解析器(jsp模式)
* 写一个controller包下的控制器,测试类(写业务代码)
还有jsp
看见页面了,表示mvc环境行了
* 实现步骤:利用骨架(张老师)
* 问题:Junit用4.12版本以及以上,因为他支持spring-test*******
* 添加的依赖:springweb-mvc,servelt,jsp,jstl,lombok,(springmvc的能够跑起来了),junit(单元测试)
* <scope>作用域,jsp的四大作用域,
provided在main和test好用
Tomcat自己有servlet和jsp
* 用javaweb写的servlet目录最终在na?web/lib
* springmvc.xml放在java中的配置**********
* 一般情况下放在resources,默认编译xml文件
##### springioc
* 目的是使用IOC容器管理 各种 bean对象,service,dao,等等
* 配置IOC核心配置文件
* 创建一个applicationContext.xml
* 1、扫描service包
2、service验证成功(依赖注入)
service一个接口,一个实现类
##### mybatis
* 导包
* 数据库引擎,InnoDB支持事务
* 需要导入的包有,mybatis,mybatis-spring,mybatis-orm,druid,mysql,common(一个工具)
* 能够让mapper接口放到IOC容器中管理,
原生:sqlSesssion.getMapper();获取到到对象
sqlSession线程不安全的api,在IOC容器中注入SqlSessionFactory,
sqlsessionFactory需要数据源,
* mybatis-srping
mybatis和spring的桥,SqlSessionFactoryBean
原来的不方便IOC容器管理,他依赖了orm,所以得引入
mybatis-orm,得自己手动导入**************
* 数据源:druid
common (lang log4j ...都在)
配置数据源,在IOC容器中,配置sqlSessionFactoryBean,配置
让ioc容器管理Maper代理对象
ref引用的 是对象
配置别名,TypeAlisesPackage
配置让IOC容器管理的的Mapper代理
* 声明一个Mapper接口和Mapper.xml***************
pojo,
其中对应一致,UserDao和UserDao.xml(包名对应一致)
其中namespace是接口的全限定名字************
* 写* 需要在mysql服务器需要转换成 字段枚举,其中* 的效率不高,不建议使用;***********
*
* 其中Mapper代理对象需要包扫描, basePackage,
* 测试:
### 测试,使用MockMVC
* 方便我们做测试
* 写一个单元测试类
* ognl表达式,在structs2 的标签中使用的比较多。(语法的类库), #{username}
对于web.xml中配置DispatchetServlet中param-name报错的问题,纯粹是测试jar包版本中懒得写直接cv了,然后发现报错了,神奇,百度后发现居然是web.xml你必须自己写,不要cv,不然会报错,cv后解决方案如图:
对于web.xml和springmvc.xml和application.xml在上篇已经发布了,下面对于mockMvc工具的使用
package com.neuedu.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
//注入webapplication
@WebAppConfiguration()
//找到spring的配置文件
@ContextConfiguration({"classpath:mvc/springmvc.xml","classpath:applicationContext.xml"})
public class UserTestTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void list() throws Exception {
String url ="http://localhost/test/list";
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get(url)).
andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
System.out.println(result.getResponse().getContentAsString());
}
@Test
public void add() throws Exception {
String url ="http://localhost/test/add";
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(url);
System.out.println(mockHttpServletRequestBuilder+"****************8");
ResultActions perform = mockMvc.perform(mockHttpServletRequestBuilder);
System.out.println(perform+"66666666666666666666666666");
// MvcResult result = mockMvc.perform(
// MockMvcRequestBuilders.get(url)
// .param("username","兔子")
// .param("password","123434")
// ).
// andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
//
// System.out.println(result.getResponse().getContentAsString());
}
}