简单的mybatis+spring+junit整合

来源:互联网 发布:skb平面设计方案优化 编辑:程序博客网 时间:2024/06/04 17:45

使用的数据库是:oralce

建立一张TEST表,再建一个ID字段用于简单的测试

需要的包:spring的jar包,mybatis.jar,mybatis-spring.jar,commons-logging.jar,aopalliance.jar,aspectjweaver-1.6.9.jar,ojdbc6.jar,junit.jar,junit-dep.jar


spring配置文件:applicationContext.xml

<span style="font-size:18px;"><?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:aop="http://www.springframework.org/schema/aop"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:jdbc="http://www.springframework.org/schema/jdbc"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <!-- org.springframework.jdbc.datasource.DriverManagerDataSource -->    <!-- org.springframework.jndi.JndiObjectFactoryBean -->    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>        <property name="username" value="test"/>        <property name="password" value="test"/>    </bean>    <!-- 配置</span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">jdbcTransactionManager</span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;"> --></span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;"></span><span style="font-size:18px;">    <bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 配置spring组件扫描 -->        <context:component-scan base-package="*" />    <!-- 启用autowire -->    <context:annotation-config />    <!-- 设置exposeProxy为true -->    <aop:aspectj-autoproxy expose-proxy="true"/>    <!-- </span><span style="font-family:microsoft yahei;color:#555555;"><span style="font-size: 15px; line-height: 35px;">支持注解</span></span><span style="font-size:18px;"> -->    <tx:annotation-driven />    <!-- mybatis的SqlSessionFactory -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="typeAliasesPackage" value="mybatis.model" />    </bean>    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"><constructor-arg index="0" ref="sqlSessionFactory" />    </bean>    <!-- 扫描映射 -->     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="mybatis.mapper" />    </bean> </beans></span>
在mybatis.mapper下建立mapper

TestMapper.java

package mybatis.mapper;import java.util.List;public interface TestMapper {public List<String> selectAllInfo();}
TestMapper.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="mybatis.mapper.TestMapper">  <select id="selectAllInfo" resultType="java.lang.String">    SELECT          ID    FROM TEST  </select></mapper>
建立模板类:Test.java

模板类需要映射数据库对应表TEST的所有字段

package mybatis.model;/** * 实体类 * */public class Test {private Integer id;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}}
这时我们就构建了简单的mybatis+spring

然后建立junit测试类:

首先建立一个junit测试类的父类,这个父类中写了需要加载的spring配置文件,事务的控制,便于新的junit类来继承这个父类

package mybatis.junit;import org.junit.runner.RunWith;  import org.springframework.test.context.ContextConfiguration;  import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  import org.springframework.test.context.transaction.TransactionConfiguration;/**  * Junit 基础类,加载环境   */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})  @TransactionConfiguration(transactionManager="jdbcTransactionManager",defaultRollback=true)public  class TestBase extends AbstractTransactionalJUnit4SpringContextTests {  }  
建立测试类:@Autowired是spring注解的一种方式,@Test是junit测试找的执行方法

package mybatis.junit.test;import java.util.List;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import mybatis.junit.TestBase;import mybatis.mapper.TestMapper;public class TestMybatis extends TestBase {@Autowiredprivate TestMapper testMapper;@Testpublic void getCallCenterAccountByCrmAccount(){try {List<String> list = testMapper.selectAllInfo();System.out.println(list.size());} catch (Exception e) {e.printStackTrace();}}}
这样我们就将mybatis,spring,junit整合完毕了,这套整合主要用于平时的测试。

如果需要在web项目中启动还需要配置一个aciton层

web.xml文件:

<?xml version="1.0" encoding="utf-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">          <!-- 初始化spring配置文件-->     <context-param><param-name>contextConfigLocation</param-name><span style="white-space:pre"></span><param-value><span style="white-space:pre"></span>/WEB-INF/applicationContext*.xml</param-value>     </context-param>     <context-param><param-name>webAppRootKey</param-name><param-value>webApp.root</param-value>     </context-param>     <!-- 设置servlet编码开始 -->      <filter>          <filter-name>CharacterEncodingFilter</filter-name>          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>          <init-param>              <param-name>encoding</param-name>              <param-value>utf-8</param-value>          </init-param>          <init-param>              <param-name>forceEncoding</param-name>              <param-value>true</param-value>          </init-param>      </filter>      <filter-mapping>          <filter-name>CharacterEncodingFilter</filter-name>          <url-pattern>/*</url-pattern>      </filter-mapping>      <!-- 设置servlet编码结束 -->        <!-- 设置spring监听 -->    <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- 设置spring监听结束 -->     <welcome-file-list>    <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

再新增一个spring配置文件:applicationContextTaskConfig.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:aop="http://www.springframework.org/schema/aop"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:jdbc="http://www.springframework.org/schema/jdbc"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"     default-autowire="byName" default-lazy-init="true">       <!-- ActionBean-->    <bean id="testAction" class="mybatis.action.TestAction" scope="prototype"/></beans>
action类:TestAction.java

package mybatis.action;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import mybatis.mapper.TestMapper;public class TestAction {@Autowiredpublic TestMapper testMapper;public String selectAllInfo(){List<String> test = testMapper.selectAllInfo();System.out.println(test.size());return "success";}}










0 0