测试的代码:
import static org.junit.Assert.*;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.zw.wei.bean.User;
import com.zw.wei.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { “classpath:/spring/spring-mybatis.xml”})
public class TestMybatis {
private static Logger logger = Logger.getLogger(TestMybatis.class);
@Resource
private UserService userService=null;
// @Before
// public void setUp() throws Exception {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
// “spring/applicationContext.xml”);
// userService = (UserService) applicationContext
// .getBean(“userServiceImpl”);
// }
@Test
public void test() {
User user = userService.getUserById(1);
// System.out.println(user.getUsername());
// logger.info(“值:”+user.getUsername());
// logger.info(JSON.toJSONString(user));
}
}
运行后报错,代码在回复里
import static org.junit.Assert.*;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.zw.wei.bean.User;
import com.zw.wei.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { “classpath:/spring/spring-mybatis.xml”})
public class TestMybatis {
private static Logger logger = Logger.getLogger(TestMybatis.class);
@Resource
private UserService userService=null;
// @Before
// public void setUp() throws Exception {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
// “spring/applicationContext.xml”);
// userService = (UserService) applicationContext
// .getBean(“userServiceImpl”);
// }
@Test
public void test() {
User user = userService.getUserById(1);
// System.out.println(user.getUsername());
// logger.info(“值:”+user.getUsername());
// logger.info(JSON.toJSONString(user));
}
}
运行后报错,代码在回复里
解决方案
10
java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy
缺spring-jdbc的jar包吧
缺spring-jdbc的jar包吧
10
“sqlSessionFactory” defined in class path resource [spring/spring-mybatis.xml],它说在你的XML配置文件中没有定义SQLSessionFactory,少JDBC的jar包吧去Spring中找就是了
40
嗯好的,本人看看pom.xml里有没有配置