I am creating a MyBatis project with mybatis-spring, and I would like to use the Java configuration for everything except the actual SQL (for example, no @Select annotations in the mapper interfaces).
I have the following setup that works, but it uses @Select:
DataSource Beans:
@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();
}
}
Mapper Interface:
package myproject.persistence;
public interface PersonMapper {
@Select("SELECT * FROM PersonTable WHERE PersonTable.LAST_NAME = #{lastName}")
List<Person> getByLastName(@Param("lastName") String lastName);
}
Service:
@Service
public class PeopleService {
@Autowired
PersonMapper personMapper;
public List<Person> getByLastName(final String lastName) {
return personMapper.getByLastName(lastName);
}
}
I am looking for a way to move the SQL statement in the @Select annotation to an XML file (but I support Java configuration for all beans and still use @MapperScan). The missing link I'm looking for will be a method of pairing the mapper interface with the XML "mapper" that defines the SQL statements.
source
share