5

我正在使用 mybatis-spring 建立一个 MyBatis 项目,并且我想对除实际 SQL 之外的所有内容使用 Java 配置(例如,映射器接口中没有 @Select 注释)。

我有以下设置,它有效,但它使用@Select:

数据源 Bean:

@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource devDataSource() {
        ... set up data source
        return dataSource;
    }
}

MyBatis Beans:

@Configuration
@MapperScan("myproject.persistence")
public class MyBatisConfig {

    @Autowired
    DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        return sessionFactory.getObject();
    }
}

映射器接口:

package myproject.persistence;

public interface PersonMapper {

    @Select("SELECT * FROM PersonTable WHERE PersonTable.LAST_NAME = #{lastName}")
    List<Person> getByLastName(@Param("lastName") String lastName);
}

服务:

@Service
public class PeopleService {

    @Autowired
    PersonMapper personMapper;

    public List<Person> getByLastName(final String lastName) {
        return personMapper.getByLastName(lastName);
    }
}

我正在寻找一种将@Select 注释中的SQL 语句移动到XML 文件的方法(但维护所有bean 的Java 配置,并且仍然使用@MapperScan)。我正在寻找的缺失链接是将映射器接口与定义 SQL 语句的 XML“映射器”配对的方法。

4

2 回答 2

6

你可以在 myproject.persistence 包下的 PersonMapper.xml 中定义你的 sql(注意:接口应该与 xml 在同一个包中)。就像吹:

<mapper namespace="myproject.persistence.PersonMapper">
   <select id="getByLastName" parameterType="string" resultType="myproject.domain.Person">
      SELECT * FROM PersonTable WHERE PersonTable.LAST_NAME = #{lastName}
   </select>

mybatis 会自动寻找你在 xml 文件中定义的方法。

于 2013-11-12T03:03:35.993 回答
2
package cn.fruitd.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;

@Configuration
@MapperScan("cv.fruitd.dao")
public class DBConfig implements EnvironmentAware {

    @Autowired
    Environment env;


    /**
     * 配置数据源
     *
     * @return
     */
    @Bean(initMethod = "init", destroyMethod = "close")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(env.getProperty("db-url"));
        dataSource.setUsername(env.getProperty("db-username"));
        dataSource.setPassword(env.getProperty("db-password"));
        dataSource.setAsyncInit(Boolean.parseBoolean(env.getProperty("db-asyncInit", "true")));
        dataSource.setMinIdle(Integer.parseInt(env.getProperty("db-minIdle", "1")));
        dataSource.setMaxActive(Integer.parseInt(env.getProperty("db-maxActive", "20")));
        dataSource.setMaxWait(Long.parseLong(env.getProperty("db-maxWait", "60000")));
        return dataSource;
    }


    @Bean
    @Autowired
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver pathM3R = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(pathM3R.getResources("classpath*:mybatis/*.xml"));
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer =
                new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("cn.fruitd.dao");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        return mapperScannerConfigurer;
    }


    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }
}
于 2018-01-14T20:16:06.183 回答