Spring Mybatis jUnit 单元测试初体验

来源:互联网 发布:日历 节假日 农历 js 编辑:程序博客网 时间:2024/06/04 19:11

项目还用了Maven导入Jar包,编译器为Eclipse安装了SpringIDE插件,感觉比STS好用多了。
下面直接贴图片和代码吧。
首先是整个项目的结构,主要分为三部分:
这里写图片描述

数据库

额,有点旧了。用的是NavicatForMySQL ,在命令行界面输入SQL语句创建了库 gkjm_ back , 和表 article_ table。下面还是直接放截图吧。
这里写图片描述

java类

IArticleDao.java

@Repositorypublic interface IArticleDao {    public List<Article> getAllArticle();    public int insertArticle(Article article);    public Article updateArticle(Article article);    public int deleteArticle(int id);    public Article findArticleById(int id);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

articleMapper.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="com.gkjm.server.dao.IArticleDao">    <insert id="insertArticle" parameterType="Article" >        insert into        article_table(title,headImg,textImgs,textContent)        values(#{title},#{headImg},#{textImgs},#{textContent})    </insert>    <update id="updateArticle" parameterType="Article">        update article_table        set        title=#{title},headImg=#{headImg},textImgs=#{textImgs},        textContent=#{textContent} where id=#{id}    </update>    <delete id="deleteArticle" parameterType="int">        delete from        article_table where id=#{id}    </delete>    <select id="findArticleById" parameterType="int" resultType="Article">        select title,headImg,textImgs,textContent from article_table        where        id=#{id}    </select></mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

留意namespace="com.gkjm.server.dao.IArticleDao" 这里namespcae为dao类。

Article.java

package com.gkjm.server.po;public class Article {    private String title;    private String headImg;    private String textImgs;    private String textContent;    //省略getter setter}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

IArticleService.java

public interface IArticleService {      public List<Article> getAllArticle();       public int insertArticle(Article article);      public Article updateArticle(Article article);      public int deleteArticle(int id);       public Article findArticleById(int id); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

ArticleServiceImpl

@Servicepublic class ArticleServiceImpl implements IArticleService{    @Autowired    IArticleDao iArticleDao;    public List<Article> getAllArticle() {        return iArticleDao.getAllArticle();    }    public int insertArticle(Article article) {        return iArticleDao.insertArticle(article);    }    public Article updateArticle(Article article) {        return iArticleDao.updateArticle(article);    }    public int deleteArticle(int id) {        return iArticleDao.deleteArticle(id);    }    public Article findArticleById(int id) {        return iArticleDao.findArticleById(id);    }       }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

配置文件

mybatis配置文件 mybatis-config

<?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <typeAliases>        <typeAlias alias="Article" type="com.gkjm.server.po.Article" />    </typeAliases></configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

typeAliases 别名。一开始没有写这个typeAliases还报错了。
ps.仔细一看,mapper.xml中paramterType=”Article”是下面报错的原因。

Caused by: java.lang.ClassNotFoundException: Cannot find class: Article    at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:190)    at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)    at org.apache.ibatis.io.Resources.classForName(Resources.java:256)    at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:113)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

spring配置文件 spring-db.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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"    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:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.2.xsd        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">    <!-- 1.数据源 DriveManagerDataSource -->    <bean id="mDataSource"        class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="url" value="jdbc:mysql://localhost:3306/gkjm_back"></property>        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>        <property name="username" value="root"></property>        <property name="password" value="admin"></property>    </bean>    <!-- 2.mybatis的SqlSession的工厂:SqlsessionFactoryBean 。 typeAliasesPackage         : value为实体类pojo的包名,可以本来应该全类名使用,这里可以用简单类名代替全类名 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="mDataSource"></property>        <property name="configLocation" value="classpath:mybatisConfig/mybatis-config.xml"></property>        <property name="mapperLocations" value="classpath*:com/gkjm/server/mapper/**/*.xml"></property>    </bean>    <!-- 3.mybatis自动扫描加载Sql映射文件/接口:MapperScannerConfiguer。 basePackage:指定Sql映射文件/接口所在的包(自动扫描)         sqlSessionFactory : -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.gkjm.server.dao"></property>    </bean>    <!-- 4.事务管理 DataSourceTransactionManager -->    <bean id="txManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="mDataSource"></property>    </bean>    <!-- 5.使用声明式事务 :引用上面的配置管理器 -->    <tx:annotation-driven transaction-manager="txManager" />    <context:annotation-config></context:annotation-config>    <context:component-scan base-package="com.gkjm.server" /></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

这里面折腾了超久的就是两个配置属性,sqlSessionFactory和mapperConfigScanner。

sqlSessionFactory

configLocation–value为Mybatis的配置xml
mapperLocations– 一开始我把mapper.xml放置在resource文件夹下,结果怎么都运行有Exception。看了另外一个例子是写在Java文件夹下一个包下,也就按部就班写,结果这次成功了。

MapperScannerConfigurer

自动配置扫描Mapper文件。 这里basePackage的value是dao包的路径。

annotation-config 和 component-scan

这两个属性是去扫描注解的 impl包下的@Service ,dao包下的@Repository ,MainTest类中的 @Autowired 和 @RunWith @ContextConfiguration

spring配置 applicationContext.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:context="http://www.springframework.org/schema/context"    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">    <context:annotation-config />    <context:component-scan base-package="com.gkjm.server" />    <import resource="classpath:/springConfig/spring-db.xml" /></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

这个xml配置文件实际上没有用到,在这里可以先忽略。因为MainTest测试类中直接用注解@ContextConfiguration(locations={"classpath:springConfig/spring-db.xml"}) 定位到spring-db.xml配置文件上。但实际大项目中,一般是使用applicationContext.xml配置。留意其中的<import resource="classpath:/springConfig/spring-db.xml" /> 这也是一个知识点哦。

测试类

MainTest.java

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"classpath:springConfig/spring-db.xml"})public class MainTest {    @Autowired    private IArticleService iArticleService;    @Test    public void testAdd(){        Article article=new Article();        article.setHeadImg("headImg");        article.setTextContent("textContent");        article.setTextImgs("textImgs");        article.setTitle("title");        iArticleService.insertArticle(article);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

最后

至于web.xml 则没有用到。下一步整合SpringMVC Spring会用到这个去使用配置文件。

附录pom.xml

关于pom.xml Maven的使用还有个小技巧,直接用官方的Maven仓库实在是太慢了。。这里推荐用开源中国的Maven仓库。具体如何使用,可以百度搜下开源中国官网有文章详细易懂的介绍。
为了方便,还是把pom.xml也贴上来吧,项目源码就不放了。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.gaokao</groupId>    <artifactId>AppServer</artifactId>    <packaging>war</packaging>    <version>0.0.1-SNAPSHOT</version>    <name>AppServer Maven Webapp</name>    <url>http://maven.apache.org</url>    <properties>        <spring.version>4.2.4.RELEASE</spring.version>        <mybatis.version>3.2.7</mybatis.version>    </properties>    <dependencies>        <!-- Spring start -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-expression</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jms</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aspects</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring.version}</version>        </dependency>        <!-- Spring end -->        <!-- Mybatis start -->        <!-- mybatis核心包 -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>${mybatis.version}</version>        </dependency>        <!-- mybatis/spring包 -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.2.2</version>        </dependency>        <!-- mysql驱动包 -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.31</version>        </dependency>        <!-- dbcp2连接池 -->        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-dbcp2</artifactId>            <version>2.0.1</version>        </dependency>        <!-- Mybatis end -->        <!-- Others -->        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.4</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>jstl</artifactId>            <version>1.1.2</version>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>servlet-api</artifactId>            <version>2.5</version>        </dependency>        <dependency>            <groupId>javax.servlet.jsp</groupId>            <artifactId>jsp-api</artifactId>            <version>2.1</version>        </dependency>        <dependency>            <groupId>jstl</groupId>            <artifactId>jstl</artifactId>            <version>1.1.2</version>        </dependency>        <dependency>            <groupId>taglibs</groupId>            <artifactId>standard</artifactId>            <version>1.1.2</version>        </dependency>    </dependencies>    <build>        <finalName>AppServer</finalName>    </build></project>
0 0