Write test classes Mybatis-Spring project

Because want to look at the code mybatis-Spring project, all downloaded from the source on github down, I looked at, it may be too much of a test class, there is no fine to find the entrance class, so he wrote a main entrance class, hereby record (local I have installed Mysql database)

First, there is no database-driven project, so add about

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.34</version>
    </dependency>

Then add mapper interface class

package org.mybatis.spring.demo.mapper;

public interface DemoMapper {

   String getById();
}

Add xml file corresponding to the next resource / mapper / directory

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.spring.demo.mapper.DemoMapper">


  <select id="getById" resultType="String">
    SELECT stu_id FROM record WHERE id = 1
  </select>

</mapper>

Finally, add a configuration class

package org.mybatis.spring.demo;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;


@Configuration
@MapperScan("org.mybatis.spring.demo.mapper")
public class DemoConfig {

  ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();

  @Bean
  public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource) throws Exception{
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    Resource[] resources = resourceResolver.getResources("mapper/*.xml");
    bean.setMapperLocations(resources);
    return bean;
  }

  @Bean
  public DataSource dataSource(){
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/test");
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    return dataSource;

  }

}

Adding a master class to do some tests

package org.mybatis.spring;

import org.mybatis.spring.demo.DemoConfig;
import org.mybatis.spring.demo.mapper.DemoMapper;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class Main {

  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
    DemoMapper bean = context.getBean(DemoMapper.class);
    System.out.println(bean.getById());
    System.out.println(bean.toString());



  }
}

Add a whole directory structure is such that

 

 

End.

 

Guess you like

Origin www.cnblogs.com/nihaofenghao/p/12619794.html