SSM框架中Junit的应用
程序员文章站
2022-07-08 16:01:51
...
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: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"> <context:annotation-config /> <context:component-scan base-package="cn.*" /> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${driverClassName}</value> </property> <property name="url"> <value>${url}</value> </property> <property name="username"> <value>${username}</value> </property> <property name="password"> <value>${password}</value> </property> <property name="initialSize"> <value>${initialSize}</value> </property> <property name="minIdle"> <value>${minIdle}</value> </property> <property name="maxActive"> <value>${maxActive}</value> </property> <property name="maxIdle"> <value>${maxIdle}</value> </property> </bean> <!-- 定义全局的事务控制 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启注解方式声明事务 --> <tx:annotation-driven /> <!-- 定义SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath*:com/ssm/mapping/*.xml" /> <property name="typeAliasesPackage" value="com.mybatis.model" /> </bean> <!-- 自动扫描 mapper,允许自动注入(根据类型匹配),不需要逐个配置mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 这里配置成spring中注解+自动扫描方式,相对XML文件配置简化得多。 --> <!-- @Repository:DAO层组件,@Service:业务层组件,@Controller:控制层组件 --> <context:component-scan base-package="com.ssm" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> </beans>
mybatis-config.xml
<?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> <!-- mybatis别名定义 --> <typeAliases> <typeAlias alias="User" type="com.ssm.model.User"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/lucky" /> <property name="username" value="cool" /> <property name="password" value="cool" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/ssm/mapping/UserMapper.xml" /> </mappers> </configuration>
LoginServiceImplTest.java
/** * */ package com.ssm.service.impl; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.ssm.model.UserKey; import com.ssm.service.LoginService; /** * @author guanhw * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:mybatis-config.xml"}) public class LoginServiceImplTest { @Autowired // 多个相同类型时选择注入哪一个 @Qualifier("loginServiceImpl") public LoginService loginService; /** * Test method for {@link com.ssm.service.impl.LoginServiceImpl#IsLogin(com.ssm.model.UserKey)}. */ @Test public void testIsLogin() { UserKey userKey = new UserKey(); userKey.setUsername("cool"); userKey.setPassword("cool"); boolean selResult = loginService.IsLogin(userKey); assertEquals(selResult, true); } }
TestMapperUser.java
package com.ssm.test; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import com.ssm.dao.UserMapper; import com.ssm.model.User; import com.ssm.model.UserKey; import com.ssm.utils.MyBatisUtil; public class TestMapperUser { static SqlSessionFactory sqlSessionFactory = null; static { sqlSessionFactory = MyBatisUtil.getSqlSessionFactory(); } void testQueryByUserNameAndPassword() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserKey userKey = new UserKey(); userKey.setUsername("cool"); userKey.setPassword("cool"); User user = mapper.selectByPrimaryKey(userKey); System.out.println( user.getUsername() + "," + user.getFullname()); } finally { sqlSession.close(); } } // end of testQueryByCondition }
MyBatisUtils.java
package com.ssm.utils; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { private final static SqlSessionFactory sqlSessionFactory ; static { String resouce = "mybatis-config.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resouce); } catch (IOException e) { System.out.println(e.getMessage()); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory ; } }
上一篇: git命令合集
下一篇: Get Post乱码