How to use HSQLDB and mybatis to construct unit tests

Source: Internet
Author: User

Projects are projects that use Spring + mybatis

Below is the configuration how to write:

This is written in spring:

<Jdbc:embedded-databaseID= "DataSource">          <Jdbc:script Location= "Classpath:/test/vmdb.script"/>      </Jdbc:embedded-database>--The key is this sentence<BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">        <!--when instantiating sqlsessionfactory, you need to use the above configured data sources and SQL mapping files. -        < Propertyname= "DataSource"ref= "DataSource" />                < Propertyname= "Mapperlocations"value= "Classpath:com/suning/rdrs/admin/mapping/*.xml" />    </Bean>    <!--Configuring the Scanner -    <Beanclass= "Org.mybatis.spring.mapper.MapperScannerConfigurer">        <!--Scan Com.suning.rdrs.admin.dao All mapped interface classes under this package and its sub-packages -        < Propertyname= "Basepackage"value= "Com.suning.rdrs.admin.dao" />        < Propertyname= "Sqlsessionfactorybeanname"value= "Sqlsessionfactory" />    </Bean>

How to construct the memory database for unit tests

PrivateConnection Conn; @Before Public voidbefore () {//Create a spring context using the two configuration files "Spring.xml" and "Spring-mybatis.xml"ApplicationContext AC =NewClasspathxmlapplicationcontext (Newstring[]{"/test/spring.xml", "/test/conf.xml"}); //Remove the UserService object we want to use from the spring container based on the Bean's IDUserService = (itaskservice) ac.getbean ("Taskservice"); Try{conn= Drivermanager.getconnection ("Jdbc:hsqldb:mem:rdrs", "sa", "" "); Startup memory Database RDRs, user name sa password empty}Catch(SQLException e) {}}

Also posted script statements, although known as compatible, but not so good compatibility

Incompatible with INDEX Index_taskname (taskname,username)

Column_pair text Character set UTF8, text specified character set incompatible

SET DATABASE SQL SYNTAX MYS TRUE; --compatible with mysqlcreate TABLE TASK (  ID INT (TEN) NOT NULL auto_increment,  taskname varchar () is not NULL,  USERNAME var char () not NULL,  STATUS varchar (+) NOT NULL,  task_type varchar (+) NOT NULL,  gmt_create varchar (+) not N ULL,  gmt_start varchar (n) not NULL,  PRIMARY KEY  (ID),);

The rest is simply MyBatis, the unit test is very good to write

How to use HSQLDB and mybatis to construct unit tests

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.