最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis以main方法形式調用dao層執(zhí)行代碼實例

 更新時間:2023年08月03日 10:14:09   作者:健康平安的活著  
這篇文章主要介紹了Mybatis以main方法形式調用dao層執(zhí)行代碼實例,MyBatis 是一款優(yōu)秀的持久層框架,MyBatis 免除了幾乎所有的 JDBC 代碼以及設置參數和獲取結果集的工作,需要的朋友可以參考下

一 工程概覽

1.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> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }
    //通過name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

1.2.2 pom文件

    <!-- springBoot的啟動器 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.0.1.RELEASE</version>
    </dependency>
    <!-- web啟動器 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <version>2.0.1.RELEASE</version>
    </dependency>
    <!-- Mybatis啟動器 -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.1.1</version>
    </dependency>
    <!-- mysql數據庫驅動 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.3</version>
    </dependency>
    <!-- druid數據庫連接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.9</version>
    </dependency>

1.2.3 mybatis-config配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 全局參數 -->
    <settings>
        <!-- 使全局的映射器啟用或禁用緩存 -->
        <setting name="cacheEnabled"             value="true"   />
        <!-- 允許JDBC 支持自動生成主鍵 -->
        <setting name="useGeneratedKeys"         value="true"   />
        <!-- 配置默認的執(zhí)行器.SIMPLE就是普通執(zhí)行器;REUSE執(zhí)行器會重用預處理語句(prepared statements);BATCH執(zhí)行器將重用語句并執(zhí)行批量更新 -->
        <setting name="defaultExecutorType"      value="SIMPLE" />
        <!-- 指定 MyBatis 所用日志的具體實現 -->
        <setting name="logImpl"                  value="SLF4J"  />
        <!-- 使用駝峰命名法轉換字段 -->
        <!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
    </settings>
</configuration>

1.2.4 applicatiion配置文件

代碼: 

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語句,一般用于本地開發(fā)測試
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logging:
  level:
    com.ljf.mapper: debug
  #分頁插件
  pagehelper:
    helper-dialect: mysql
    params: count=countSql
    reasonable: true    #開啟優(yōu)化,如果開啟優(yōu)化,在分頁頁碼結果沒有數據的時候,會顯示有數據的頁碼數據
    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<Order> dataList=userService.queryOrderList();
        dataList.stream().forEach((x)->{System.out.println("x;"+x.getOrderName());});
        System.out.println();
        //結束進程
       System.exit(0);
    }
}

1.2.6 執(zhí)行結果

到此這篇關于Mybatis以main方法形式調用dao層執(zhí)行代碼實例的文章就介紹到這了,更多相關Mybatis調用dao層內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

门源| 兰溪市| 马山县| 陆川县| 广饶县| 封丘县| 宜阳县| 泗阳县| 密云县| 赣榆县| 岳普湖县| 白水县| 嵊泗县| 德阳市| 叶城县| 天门市| 西吉县| 长治市| 都昌县| 安化县| 镇原县| 明星| 将乐县| 石首市| 高青县| 莱阳市| 那坡县| 锡林浩特市| 射阳县| 和顺县| 定州市| 郓城县| 徐水县| 韶山市| 汽车| 新闻| 紫金县| 伊金霍洛旗| 裕民县| 长海县| 连江县|