springboot mybatis 以main方法形式调用dao层进行执行

一 工程概览

1.1 工程概览

springboot mybatis 以main方法形式调用dao层进行执行_第1张图片

 1.2 核心内容配置

1.2.1 springutil工具类

package com.ljf.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @ClassName: SpringUtil
 * @Description: TODO
 * @Author: admin
 * @Date: 2023/04/08 19:58:05 
 * @Version: V1.0
 **/

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtil.applicationContext == null){
            SpringUtil.applicationContext  = applicationContext;
        }
    }


    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    //通过class获取Bean.
    public static  T getBean(Class clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static  T getBean(String name,Class clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}

1.2.2 pom文件

    
    
      org.springframework.boot
      spring-boot-starter-web
      2.0.1.RELEASE
    
    
    
      org.springframework.boot
      spring-boot-starter-thymeleaf
      2.0.1.RELEASE
    
    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.1.1
    
    
    
      mysql
      mysql-connector-java
      5.1.3
    
    
    
      com.alibaba
      druid
      1.0.9
    

1.2.3 mybatis-config配置文件




    
    
        
        
        
        
        
        
        
        
        
        
    

1.2.4 applicatiion配置文件

springboot mybatis 以main方法形式调用dao层进行执行_第2张图片

代码: 

server:
  port: 8084
spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test_db
    username: root
    password: cloudiip
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.ljf.model
    # 加载全局的配置文件
  configuration-properties: classpath:mybatis/mybatis-config.xml
  configuration:
    #增加打印sql语句,一般用于本地开发测试
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

logging:
  level:
    com.ljf.mapper: debug
  #分页插件
  pagehelper:
    helper-dialect: mysql
    params: count=countSql
    reasonable: true    #开启优化,如果开启优化,在分页页码结果没有数据的时候,会显示有数据的页码数据
    support-methods-arguments: true #是否支持接口参数来传递分页参数,默认false

1.2.5 调用类

@SpringBootApplication
@MapperScan("com.ljf.mapper")
//@ComponentScan("com.ljf")
public class MainAppExe {
    public static void main(String[] args) {
        SpringApplication.run(MainAppExe.class, args);
        //调用Sercice层
        ApplicationContext context = SpringUtil.getApplicationContext();
        OrderService userService = context.getBean(OrderService.class);// 注意是Service,不是ServiceImpl
        List dataList=userService.queryOrderList();
        dataList.stream().forEach((x)->{System.out.println("x;"+x.getOrderName());});
        System.out.println();


        //结束进程
       System.exit(0);
    }

}

1.2.6 执行结果

springboot mybatis 以main方法形式调用dao层进行执行_第3张图片

 

你可能感兴趣的:(springboot,mybaits,mybatis,spring,boot,java)