zoukankan      html  css  js  c++  java
  • 聊聊、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();
    }
    } 

  • 相关阅读:
    JAVA实现接口监控报警系统
    批量插入数据、自定义分页器
    django与Ajax
    ORM优化查询、choices参数
    django之查询操作及开启事务
    django之ORM字段及参数
    数据库设计
    django之模型层
    django之模板层
    django之视图层
  • 原文地址:https://www.cnblogs.com/xums/p/12642139.html
Copyright © 2011-2022 走看看