聊聊Mybatis集成Spring 注解方式

Posted 香农随笔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聊聊Mybatis集成Spring 注解方式相关的知识,希望对你有一定的参考价值。

导航:  

聊聊、Mybatis API  

聊聊、Mybatis XML 

聊聊、Mybatis集成Spring XML 方式

聊聊、手写Mybatis XML配置方式

聊聊、手写Mybatis 注解配置方式 

聊聊、手写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();
}
} 

以上是关于聊聊Mybatis集成Spring 注解方式的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot注解方式集成Mybatis

聊聊Mybatis集成Spring的原理

Spring Boot整合MyBatis

264.Spring Boot MyBatis集成MyBatis-Plus

263.Spring Boot MyBatis集成TkMybatis

spring boot集成mybatis+事务控制