您的位置:首页 > 编程语言 > Java开发

springMvc+MyBatis+Maven下JUnit使用的一种方式

2015-12-28 21:15 537 查看
</pre>springMvc+MyBatis+Maven下JUnit使用一种方式,项目中实战。在第一次启动时可能有弹框忽略即可,再次启动即可!JUnit代码:<pre name="code" class="java">package com.unionx.wanxue;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.unionx.wanxue.dao.stagesMapper;
import com.unionx.wanxue.domains.Stages;

public class jsonTest {
/**
* 主要拼接数据库中表的id及name为json字段
*/
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:root-context.xml");
SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) ac.getBean("sqlSessionFactory");
SqlSession session = sqlSessionFactory.openSession();
//配置的一个dao接口
stagesMapper s = session.getMapper(stagesMapper.class);

StringBuffer json=new StringBuffer("[");
List<Stages> ss =s.insertSelective();
for(Stages sss : ss){
System.out.println(sss.toString());
if(sss.getId()==1){
json.append("{'id':'"+sss.getId());
}else{
json.append(",{'id':'"+sss.getId());
}
json.append("','name':'"+sss.getName());
json.append("','is_open':0}");
}
json.append("]");
System.out.println(json.toString());
}
}


root-context.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"
default-lazy-init="true">
<!-- 使用配置文件加载数据库配置 -->
<context:property-placeholder
ignore-unresolvable="true" location="classpath*:/localdataSource.properties" />

<bean id="localdataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="filters" value="${filters}" />
<property name="maxActive" value="${maxActive}" />
<property name="initialSize" value="${initialSize}" />
<property name="maxWait" value="${maxWait}" />
<property name="minIdle" value="${minIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<property name="poolPreparedStatements" value="${poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${maxPoolPreparedStatementPerConnectionSize}" />
<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
<property name="removeAbandoned" value="${removeAbandoned}" />
<!-- 单位秒 -->
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
<property name="logAbandoned" value="${logAbandoned}" />
</bean>

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="localdataSource" />
<property name="mapperLocations" value="classpath:com/unionx/wanxue/mapper/**.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.unionx.wanxue.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
ps:root-context必须封装Dao下的自动装配及连接数据库的配置

localdataSource.properties配置文件:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://IP地址:3306/数据库名?useUnicode=true&characterEncoding=utf-8
jdbc.username=用户名
jdbc.password=密码
filters=stat
maxActive=20
initialSize=5
maxWait=60000
minIdle=10
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 'x'
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=true
maxOpenPreparedStatements=10
removeAbandoned=true
removeAbandonedTimeout=108000
logAbandoned=true
maxPoolPreparedStatementPerConnectionSize=20
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: