聊聊、Mybatis集成Spring 注解方式
导航:
聊聊、手写Mybatis SpringBoot Starter
依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.5</version>
</dependency>
Config配置类
package org.rockcode.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
@ComponentScan({"org.rockcode.services"})
@MapperScan("org.rockcode.mappers")
public class AppConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
@Bean
public DataSource dataSource(){
DruidDataSource driverManagerDataSource = new DruidDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test?useSSL=false");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("root");
return driverManagerDataSource;
}
}
这里有个非常重要的注解 @MapperScan("org.rockcode.mappers"),相当于《聊聊、Mybatis集成Spring XML方式》里面的 <mybatis:scan base-package="org.rockcode.mappers" />或者
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.rockcode.mappers" />
</bean>
AccountService.java
package org.rockcode.services;
import org.rockcode.mappers.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class AccountService {
@Autowired
private AccountMapper accountMapper;
public void queryAll() {
List<Map<String, Object>> list = accountMapper.queryAll();
System.out.println(list);
}
}
AccountMapper.java
package org.rockcode.mappers;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
public interface AccountMapper {
@Select("select * from account")
public List<Map<String,Object>> queryAll();
}
Main方法
package org.rockcode.config;
import org.rockcode.services.AccountService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
AccountService accountService = ac.getBean(AccountService.class);
accountService.queryAll();
}
}