ssm遇坑记

  1. <mvc:annotation-driven>
  2. <!-- 返回utf-8的字符串,否则中文乱码 这里要放在 <context:component-scan 后面,不然不起作用-->
  3. <mvc:message-converters register-defaults="true">
  4. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  5. <constructor-arg value="UTF-8"/>
  6. <property name="writeAcceptCharset" value="false"/>
  7. </bean>
  8. </mvc:message-converters>
  9. </mvc:annotation-driven>
fori ==>
    for(int i =0 ;i< ;i++){

    }
iter ==>
    for(Book book: books){

    }

如何优雅的在ssm项目种进行单元测试?

  1. 设置好配置文件1628.jpeg
  2. 在test/java下面先创建一个BaseTest类
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})
public class BaseTest {


}

在测试前需要让程序读入spring-dao和mybatis等配置文件,前面的注释就是导入配置文件和junit,因为是测试dao,mybatis,所以导入service 和 dao就可以了

  1. 创建每个dao对应的Test类
import com.lvgj.dao.AppointmentDao;
import com.lvgj.entity.Appointment;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseAppointmentTest extends BaseTest {
    @Autowired
    private AppointmentDao appointmentDao;

    @Test
    public void testInsertAppointment() {
        long bookId = 1000;
        long studentId = 11111L;
        int i = appointmentDao.insertAppointment(bookId, studentId);
        System.out.println(i);
    }

    @Test
    public void testQueryBookByKeyWithBook() {
        long bookId = 1000;
        long studentId = 11111L;
        Appointment appointment = appointmentDao.queryByKeyWithBook(bookId, studentId);
        System.out.println(appointment);
    }
}

例如在上面这个测试类中,直接继承BaseTest就可以开始测试了,而不用其他更多余的东西!!

java报错—>Error creating bean with name ‘org.springframework.web.servlet.resource.ResourceHttpRequest1660.jpeg

这个默认是到webapp下面

但是我们的路径是

即src/main/resources,所以会报错

1、如何在spring项目中引入log4j?

1)如果resource是建立在webapp的目录下面的,那么就不用特别的更改,直接复制log4j.properties到这个目录下面就可以了

2)如果resource是建立在java代码模块下面的那么就要使用log4jConfigLocation这个参数进行引用!

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>

1850.jpeg