美文网首页
项目配置与单元测试

项目配置与单元测试

作者: Haward_ | 来源:发表于2019-05-04 15:15 被阅读0次

准备工作

主配置

server.port=8080
server.context-path=/addressbook
#DataSource
#数据库驱动
jdbc.driver = com.mysql.jdbc.Driver
#数据库链接
jdbc.url=jdbc:mysql://localhost:3306/addressbook?useUnicode=true&characterEncoding=utf8&useSSL=false
#数据库用户名
jdbc.username=xxx
#数据库密码
jdbc.password=xxx
#Mybatis
mybatis_config_file=mybatis-config.xml
mapper_path=/mapper/**.xml
entity_package=com.haward.addressbook.entity

数据连接:
DataSourceConfiguration.java

package com.haward.addressbook.config.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.beans.PropertyVetoException;

@Configuration  //找到 @Bean
@MapperScan("com.haward.addressbook.dao")  //扫描该包下面的所有dao接口(与mapper文件对应)
public class DataSourceConfiguration {
    @Value("${jdbc.driver") //在applicaton.properties中可以找到该值
    private String jdbcDriver;
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.username}")
    private String jdbcUsername;
    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Bean(name="dataSource")
    public ComboPooledDataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //驱动
        dataSource.setDriverClass(jdbcDriver);
        //数据库连接url
        dataSource.setJdbcUrl(jdbcUrl);
        //
        dataSource.setUser(jdbcUsername);
        //
        dataSource.setPassword(jdbcPassword);
        // 配置c3p0连接池的私有属性
        // 连接池最大线程数
//        dataSource.setMaxPoolSize(30);
//        // 连接池最小线程数
//        dataSource.setMinPoolSize(10);
//        //关闭连接后不自动提交,便于事务管理
//        dataSource.setAutoCommitOnClose(false);
//        // 连接超时时间
//        dataSource.setCheckoutTimeout(10000);
//        // 连接失败重试次数
//        dataSource.setAcquireRetryAttempts(2);
        return dataSource;
    }
}

SessionFactoryConfiguration.java(注意,"${jdbc.url}"不要漏写{})

package com.haward.addressbook.config.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.beans.PropertyVetoException;

@Configuration  //找到 @Bean
@MapperScan("com.haward.addressbook.dao")  //扫描该包下面的所有dao接口(与mapper文件对应)
public class DataSourceConfiguration {
    @Value("${jdbc.driver") //在applicaton.properties中可以找到该值
    private String jdbcDriver;
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.username}")
    private String jdbcUsername;
    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Bean(name="dataSource")
    public ComboPooledDataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //驱动
        dataSource.setDriverClass(jdbcDriver);
        //数据库连接url
        dataSource.setJdbcUrl(jdbcUrl);
        //
        dataSource.setUser(jdbcUsername);
        //
        dataSource.setPassword(jdbcPassword);
        // 配置c3p0连接池的私有属性
        // 连接池最大线程数
//        dataSource.setMaxPoolSize(30);
//        // 连接池最小线程数
//        dataSource.setMinPoolSize(10);
//        //关闭连接后不自动提交,便于事务管理
//        dataSource.setAutoCommitOnClose(false);
//        // 连接超时时间
//        dataSource.setCheckoutTimeout(10000);
//        // 连接失败重试次数
//        dataSource.setAcquireRetryAttempts(2);
        return dataSource;
    }
}

1.mybatis的组成

由两部分组成:1)mybatis-config.xml主配置文件 2)mapper文件(用来实现dao层的相关操作配置)
mybatis-config.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatics.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--全局配置属性-->
    <settings>
        <!--使用jdbc的useGeneratedKeys获取数据库自增主键值,
        value="true"表示打开这个功能:如果传入的是实体类,为实体类id去赋值-->
        <setting name="useGeneratedKeys" value="true"/>
        <!--使得列支持别名-->
        <setting name="useColumnLabel" value="true"/>
        <!--开始驼峰命名转化:表中create_time -> 实体类中createTime-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

AddressbookDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatics.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--对应的增删改查接口中方法-->
<mapper namespace="com.haward.addressbook.dao.AddressbookDao">
    <select id="queryAddressbook" resultType="com.haward.addressbook.entity.Addressbook">
        select * from tb_addressbook
    </select>
    <select id="queryAddressbookById" resultType="com.haward.addressbook.entity.Addressbook">
        select * from tb_addressbook
        where addressbook_id=#{addressbookId}
    </select>
    <insert id="insertAddressbook" useGeneratedKeys="true" parameterType="com.haward.addressbook.entity.Addressbook">
        insert into
        tb_addressbook(addressbook_name,addressbook_numbers,create_time,last_edit_time)
        values
        (#{addressbookName},#{addressbookNumbers},#{createTime},#{lastEditTime})
    </insert>
    <update id="updateAddressbook" parameterType="com.haward.addressbook.entity.Addressbook">
        update tb_addressbook
        <set>
            <if test="addressbookName!=null">addressbook_name=#{addressbookName},</if>
            <if test="addressbookNumbers!=null">addressbook_numbers=#{addressbookNumbers},</if>
            <if test="lastEditTime!=null">last_edit_time=#{lastEditTime},</if>
        </set>
        where addressbook_id=#{addressbookId}
    </update>
    <delete id="deleteAddressbook">
        delete from
        tb_addressbook
        where
        addressbook_id=#{addressbookId}
    </delete>
</mapper>

2.单元测试

在dao中:Alt + Enter 选择 create test,将bean提示降为提醒


调整bean.png
package com.haward.addressbook.dao;

import com.haward.addressbook.entity.Addressbook;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.List;

import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AddressbookDaoTest {

    @Autowired  //dao的mybatis实现类给注入
    private AddressbookDao addressbookDao;

    @Test
    @Ignore
    public void queryAddressbook() {
        List<Addressbook> addressbookList = addressbookDao.queryAddressbook();
        assertEquals(2,addressbookList.size());
    }

    @Test
    @Ignore
    public void queryAddressbookById() {
        Addressbook addressbook = addressbookDao.queryAddressbookById(1);
        assertEquals("张三",addressbook.getAddressbookName());
    }

    @Test
    @Ignore
    public void insertAddressbook() {
        Addressbook addressbook = new Addressbook();
        addressbook.setAddressbookName("王五");
        addressbook.setAddressbookNumbers("18813672949");
        int effectedNum = addressbookDao.insertAddressbook(addressbook);
        assertEquals(1,effectedNum);
    }

    @Test
    @Ignore
    public void updateAddressbook() {
        Addressbook addressbook = new Addressbook();
        addressbook.setAddressbookName("张三new");
        addressbook.setAddressbookId(1);
        addressbook.setLastEditTime(new Date());
        int effectedNum = addressbookDao.updateAddressbook(addressbook);
        assertEquals(1,effectedNum);
    }

    @Test
    public void deleteAddressbook() {
        int effectedNum = addressbookDao.deleteAddressbook(3);
        assertEquals(1,effectedNum);
    }
}

相关文章

网友评论

      本文标题:项目配置与单元测试

      本文链接:https://www.haomeiwen.com/subject/hmionqtx.html