Spring Mybatis整合+单元测试

Spring Mybatis整合+单元测试

PO

@Data
public class User {
    private Integer id;

    private String username;

    private String password;

    private String salt;

    private String token;

    private Byte isEnabled;

    private Date createTime;

    private Date modifiedTime;
}

DAO

public interface UserMapper {
    List<User> selectAllUser();
}

Service

public interface UserService {
    List<User> selectAllUser();
}

Impl

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> selectAllUser() {
        return userMapper.selectAllUser();
    }

}

mapper.xml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.cjf.tt.dao.UserMapper">
  <resultMap id="BaseResultMap" type="cn.cjf.tt.po.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="salt" jdbcType="VARCHAR" property="salt" />
    <result column="token" jdbcType="VARCHAR" property="token" />
    <result column="is_enabled" jdbcType="TINYINT" property="isEnabled" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="modified_time" jdbcType="TIMESTAMP" property="modifiedTime" />
  resultMap>
  <sql id="Base_Column_List">
    id, username, password, salt, token, is_enabled, create_time, modified_time
  sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from sys_user
    where id = #{id,jdbcType=INTEGER}
  select>
  <select id="selectAllUser" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/> FROM sys_user;
  select>

mapper>

mybatis.xml


DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <settings>
        
        <setting name="cacheEnabled"             value="true"   />
        
        <setting name="useGeneratedKeys"         value="true"   />
        
        <setting name="defaultExecutorType"      value="SIMPLE" />
        
        <setting name="logImpl"                  value="SLF4J"  />
        
        
    settings>

configuration>

druid.properties

druid.driver =com.mysql.cj.jdbc.Driver
druid.url =  jdbc:mysql://localhost:3306/test
druid.userName = root
druid.password = 123456
#初始化连接数
druid.pool.init=3
#高峰期过后,保留连接吃的个数
druid.pool.minIdle = 5
#高峰期,最大能创建连接的个数
druid.pool.MaxActive = 20
#等待的时间
druid.pool.timeout = 30

spring.xml


<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">
    
    
    <Context:annotation-config/>
    
    <Context:component-scan base-package="cn.cjf"/>

    
    <Context:property-placeholder location="druid.properties"/>
    
    <bean id="druidDataSources" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${druid.driver}"/>
        <property name="url" value="${druid.url}"/>
        <property name="username" value="${druid.userName}"/>
        <property name="password" value="${druid.password}"/>
        <property name="initialSize" value="${druid.pool.init}"/>
        <property name="minIdle" value="${druid.pool.minIdle}"/>
        <property name="maxActive" value="${druid.pool.MaxActive}"/>
        <property name="maxWait" value="${druid.pool.timeout}"/>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="druidDataSources"/>
        
        <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml">
        property>
        
        <property name="typeAliasesPackage" value="cn.cjf.tt.po"/>
        
        <property name="configLocation" value="mybatis-config.xml"/>
    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="cn.cjf.tt.dao"/>
    bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="druidDataSources"/>
    bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="Insert*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>
            <tx:method name="Update*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>
            <tx:method name="Delete*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>
            <tx:method name="Query*" isolation="REPEATABLE_READ" propagation="SUPPORTS"/>
        tx:attributes>
    tx:advice>

    
    <aop:config>
        <aop:pointcut id="crud" expression="execution(* cn.cjf.tt.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="crud"/>
    aop:config>
beans>

单元测试

public class UserDaoTest {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

    @Test
    public void selectAllUser() {
        UserService userDao = context.getBean(UserService.class);
        List<User> users = userDao.selectAllUser();
        for (User i : users) {
            System.out.println(i);
        }
    }
}
// 使用Spring整合Junit专用的类加载器
@RunWith(SpringJUnit4ClassRunner.class)
// 加载配置文件或者配置类
//@ContextConfiguration(classes = {SpringConfiguration.class}) //加载配置类
@ContextConfiguration(locations = {"classpath:spring.xml"})//加载配置文件
public class UserTest {
    @Autowired
    private UserService userService;

    @Test
    public void selectAllUser() {
        List<User> users = userService.selectAllUser();
        for (User i : users) {
            System.out.println(i);
        }
    }
}

你可能感兴趣的:(spring,mybatis,测试,spring,mybatis,单元测试)