基于springmvc mybatis junit搭建分工程,分模块的web工程框架(二)

1,macow-service层的集成测试

一,在src/main/java下,service接口和实现

package com.macow.home.first.service;

import java.util.List;

import com.macow.home.first.entity.User;
import com.macow.home.first.vo.UserVo;

public interface UserService {

	public List<User> select(UserVo userVo);
	
	public List<User> selectByName(User user);
	

	
}

package com.macow.home.first.service;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macow.home.first.entity.User;
import com.macow.home.first.mapper.UserMapper;
import com.macow.home.first.vo.UserVo;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	UserMapper userMapper;

	@Override
	public List<User> select(UserVo userVo) {
		PageHelper.startPage(3,2);
		User user=new User();
		BeanUtils.copyProperties(userVo, user);
		List<User> select = userMapper.select(user);
		PageInfo<User> page = new PageInfo<User>(select);
		System.out.println("---------->JSON:"+JSON.toJSONString(page));
		return select;
	}

	@Override
	public List<User> selectByName(User user) {
		
		return userMapper.selectByName(user);
	}

}

二,src/main/resources下的spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
	    http://www.springframework.org/schema/task
	    http://www.springframework.org/schema/task/spring-task.xsd
	    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
	    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- 自动扫描 -->
	<context:component-scan base-package="com.macow.home.first.service" />
	
	<!-- 事务管理 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="servicePiontCut"
			expression="execution(* com.macow.home.first.service..*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePiontCut" />
	</aop:config>
</beans>

三,src/test/java下的测试类

package com.macow.home.first.service;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.macow.home.first.entity.User;
import com.macow.home.first.vo.UserVo;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-context.xml")
public class UserServiceImplTest {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	UserService userService;
	

	@Test
	public void testSelect() {
		User user=new User();
		user.setName("小白");
		user.setPassword("2313213");
		List<User> selectList = userService.select(new UserVo());
		for(User u:selectList){
			logger.info("---------->"+u.getName()+"<---------");	
		}
		logger.info("---------->testSelect end<---------");
	}
}

四,src/test/resources下的配置文件spring-content.xml和jdbc.properties

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">

	<context:property-placeholder location="classpath*:jdbc.properties" />
	
	<import resource="spring-service.xml" />
	<import resource="spring-dao.xml" />
</beans>

#db
ds.driverClassName=org.postgresql.Driver
ds.url=jdbc:postgresql://localhost:5432/postgres
ds.username=postgres
ds.password=11111111
#ds.url=jdbc:postgresql://10.20.130.25:7440/toaasset
#ds.username=assetopr
#ds.password=paic1234
#durid datasource
ds.initialSize=2
ds.minIdle=5
ds.maxActive=5
#ds.filters=stat,config
ds.filters=stat
ds.maxWait=60000
ds.timeBetweenEvictionRunsMillis=60000
ds.minEvictableIdleTimeMillis=300000
ds.validationQuery=SELECT 1
ds.testWhileIdle=true
ds.testOnBorrow=false
ds.testOnReturn=false


五,运行测试类的方法testSelect()

17:26:54.601 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
17:26:54.616 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7e5130]
17:26:54.663 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@1735b26] will be managed by Spring
17:26:54.663 [main] DEBUG c.m.h.f.m.U.select_PageHelper_Count - ==>  Preparing: SELECT count(*) FROM T_USER 
17:26:54.679 [main] DEBUG c.m.h.f.m.U.select_PageHelper_Count - ==> Parameters: 
17:26:54.694 [main] DEBUG c.m.h.f.m.U.select_PageHelper_Count - <==      Total: 1
17:26:54.694 [main] DEBUG c.m.h.f.m.U.select_PageHelper - ==>  Preparing: SELECT id,name,password,create_Date AS createDate FROM T_USER limit ? offset ? 
17:26:54.694 [main] DEBUG c.m.h.f.m.U.select_PageHelper - ==> Parameters: 2(Integer), 4(Integer)
17:26:54.710 [main] DEBUG c.m.h.f.m.U.select_PageHelper - <==      Total: 2
17:26:54.710 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession

工程结构如图:
基于springmvc mybatis junit搭建分工程,分模块的web工程框架(二)_第1张图片

六,不知道大家用这个有没有遇到问题

	<beans profile="dev"  >
		<context:property-placeholder location="classpath*:jdbc-dev.properties" />
	</beans>

这个只能放到spring-context.xml里面其他bean的最后面,另外TestCase中要加上@ActiveProfiles(value="dev")



你可能感兴趣的:(基于springmvc mybatis junit搭建分工程,分模块的web工程框架(二))