mybatis
, and mybatis-spring
):<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1-SNAPSHOT</version>
</dependency>
A SqlSessionFactory
is needed to create mybatis sessions. Spring allows one to be quickly configured using SqlSessionFactoryBean
. Simply define a SqlSessionFactoryBean
bean, and give the reference to the DataSource
bean:
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
JDBC
queries. If a default Spring Boot HSQLDB
database is being used, this following query can be created. (There are tables involved; it simply returns a string built using a user-provided parameter).public interface HelloWorldMapper {
@Select("VALUES ('Hello ' || #{origin})")
String getString(@Param("origin") String origin);
}
@MapperScan
to the spring java configuration with the name of the "root" package containing the mapper interfaces. This annotation will auto-register the interfaces as spring beans, which can be easily injected anywhere in the application. @Autowired
private HelloWorldMapper helloWorldMapper;
// invoking the mapper method
helloWorldMapper.getString("World");
Here's the complete Spring configuration class, with a test invocation of the query:
package com.example;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.lang.annotation.*;
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
@MapperScan("com.example")
public class DemoApplication {
@Autowired
private HelloWorldMapper helloWorldMapper;
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return (SqlSessionFactory) sqlSessionFactoryBean.getObject();
}
@PostConstruct
public void init() {
System.out.println(helloWorldMapper.getString("World"));
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}