1、web.xml中的配置文件不能按照idea默认的来,不然配置filter会报错,要按照下边这个来。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
2、springmvc的配置文件因为是要只扫描某一种类的,它的默认配置要关掉
<contex:component-scan base-package="ssm" use-default-filters="false">
3、创建数据库
4、mybatis 在线文档
http://www.mybatis.org/mybatis-3/configuration.html
5、位于标记的文件夹下的java程序的相对路径是整个项目
下边这个例子中,配置文件只有在项目之下它才找得到
package ssm.bean;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MBGTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
// DefaultShellCallback callback = new DefaultShellCallback(overwrite);
// MyBatisGenerator myBatisGenerator = new
}
}
6、在java 类中获取resources 目录下的文件使用
URL resource = 类名.class.getClassLoader().getResource("资源文件名.xml");
File file = new File(resource.getFile());
7、当开启ssm项目报下边这个错的时候,就是你的配置文件中出现了id相同的配置,不管是sql配置也好,别的什么配置也好,总之就是他们的id相同了
8、在ssm项目中,使用单元测试的时候应该是要实例化容器,然后寻找组件的,这时候不用在程序中使用什么 new ClassPathConfiguration之类的代码,应该通过以下的步骤
1、导入spring test 依赖包(通过maven)
2、在程序的test目录新建测试类
3、通过以下这两个注解分别指定使用spring的Junit和指定spring 的配置文件,然后就可以在代码中使用自动注入了
package dao_test;
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 ssm.dao.TblDeptMapper;
/**
* 以下两个注解分别是指定spring的Junit和指定spring的配置文件
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class DaoTest {
@Autowired
TblDeptMapper deptMapper;
@Test
public void testDao(){
System.out.println(deptMapper);
}
}
9、一旦你自己为某个pojo类增加了一个有参构造器,就一定要在加上一个无参构造器,这个无参构造器对于一些框架来说很重要
10、当出现下边这个bug的时候,检查xml配置文件的namespace是否配置正确
11、使用spring对某个controller进行单元测试
可能现在大家在编写完某个controller之后,要对它进行测试都是得启动tomcat,然后再浏览器输入对应的url进行访问,但是使用spring 的单元测试就可以不用这么麻烦。
1、首先需要在pom文件中配置speing test 的依赖和junit 的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
2、在项目的测试文件夹中新建测试类
3、下边是具体的代码,
package mvc_test;
import com.github.pagehelper.PageInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import ssm.bean.TblEmp;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:springmvc-config.xml"})
@WebAppConfiguration
//这三个配置是必须的,分别是指定junit的运行方式, 指定 spring和springmvc的配置文件, 第三个配置是为了可以在程序中注入sringmvc 容器
public class mvcTest {
// 注入springmvc 容器
@Autowired
WebApplicationContext webApplicationContext;
// 进行controller 主要是用MockMvc类进行测试,mock的意思是虚假的意思,就是进行虚拟的springmvc测试
MockMvc mockMvc;
// 读取springmvc 配置文件, 实例化mockMvc
@Before
public void intMockMvc(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testGetEmps( ) throws Exception{
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();
// 这一句就是对某个具体url进行测试, 并且是带参数地访问这个url,andReturn() 是要获取它返回来的数据
MockHttpServletRequest request = result.getRequest();
// 得到request,因为下边的操作时在request 中获取数据
/**
* *从这里开始的操作就是很简单的,就是对分页器, 键为pageinfo 进行获取响应的值而已
*/
PageInfo pi = (PageInfo) request.getAttribute("pageInfo");
System.out.println("当前页码" + pi.getPageNum());
System.out.println("总页码" + pi.getPages());
System.out.println("总记录数" + pi.getTotal());
System.out.println("在页面需要显示的页码");
int [] nums = pi.getNavigatepageNums();
for(int i : nums){
System.out.print(" " + i);
}
// 获取员工数据
List<TblEmp> list = pi.getList();
for(TblEmp emp : list){
System.out.println(emp.getEmpName());
}
}
}