springMVC junit
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.baomidou.kissospringmvc</groupId> <artifactId>kisso_springmvc</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>kisso_springmvc Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.3.10.RELEASE</spring.version> <aspectjweaver.version>1.8.10</aspectjweaver.version> <velocity.version>1.7</velocity.version> <velocity-tools.version>2.0</velocity-tools.version> <!-- log --> <logback-classic.version>1.2.3</logback-classic.version> <logback-ext-spring.version>0.1.4</logback-ext-spring.version> <jcl-over-slf4j.version>1.7.25</jcl-over-slf4j.version> </properties> <dependencies> <!-- spring begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectjweaver.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end --> <!-- velocity begin --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>${velocity-tools.version}</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>${velocity.version}</version> </dependency> <!-- velocity end --> <!-- log begin --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback-classic.version}</version> </dependency> <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>${logback-ext-spring.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${jcl-over-slf4j.version}</version> </dependency> <!-- log end --> <!-- kisso begin --> <dependency> <groupId>com.baomidou</groupId> <artifactId>kisso</artifactId> <version>3.7.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <!-- kisso end --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.2</version> <scope>test</scope> </dependency> <!--控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务器 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <!--解析json --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <build> <finalName>kisso_springmvc</finalName> </build> </project>
package com.ssm.spring; import com.ssm.dao.UserMapper; import com.ssm.entity.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; //目标:测试一下spring的bean的某些功能 //junit整合spring的测试 //目标:测试一下spring的bean的某些功能 //junit整合spring的测试 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:test/applicationContext-dao.xml") public class TestSpringUserMapper { @Autowired private UserMapper userMapper; @Test public void testQueryUserById(){ User user = this.userMapper.queryUserById(1); System.out.println(user); } @Test public void testQueryAllUser(){ List<User> users = this.userMapper.findAllUser(); System.out.println(users.toString()); } @Test public void testInsertUser(){ User user = new User(); user.setMoney(1000d); user.setName("马云"); this.userMapper.insertUser(user); System.out.println(user.getId()); } }
<?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" 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/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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 读取jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 创建数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="${jdbc.url}"/> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 初始化连接大小 --> <property name="initialSize" value="${jdbc.initialSize}"></property> <!-- 连接池最大数据库连接数 0 为没有限制 --> <property name="maxActive" value="${jdbc.maxActive}"></property> <!-- 连接池最大的空闲连接数,这里取值为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,随时处于待命状态 0 为没有限制 --> <property name="maxIdle" value="${jdbc.maxIdle}"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="${jdbc.minIdle}"></property> <!--最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制--> <property name="maxWait" value="${jdbc.maxWait}"></property> </bean> <!-- mybatis与Spring整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- mybatis别名包扫描 --> <property name="typeAliasesPackage" value="com.ssm.entity"/> <property name="mapperLocations" value="classpath:mappers/*.xml"/> </bean> <!-- mybatis的Mapper接口扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- mapper接口扫描包 --> <property name="basePackage" value="com.ssm.dao"/> </bean> <!-- Spring事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 开启事务注解 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 开启IOC注解 --> <context:component-scan base-package="com.ssm"/> </beans>