Mybatis以main方法形式調(diào)用dao層執(zhí)行代碼實(shí)例
一 工程概覽
1.1 工程概覽
1.2 核心內(nèi)容配置
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; } //通過(guò)name獲取 Bean. public static Object getBean(String name){ return getApplicationContext().getBean(name); } //通過(guò)class獲取Bean. public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } //通過(guò)name,以及Clazz返回指定的Bean public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
1.2.2 pom文件
<!-- springBoot的啟動(dòng)器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.1.RELEASE</version> </dependency> <!-- web啟動(dòng)器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.0.1.RELEASE</version> </dependency> <!-- Mybatis啟動(dòng)器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mysql數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.3</version> </dependency> <!-- druid數(shù)據(jù)庫(kù)連接池 --> <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> <!-- 全局參數(shù) --> <settings> <!-- 使全局的映射器啟用或禁用緩存 --> <setting name="cacheEnabled" value="true" /> <!-- 允許JDBC 支持自動(dòng)生成主鍵 --> <setting name="useGeneratedKeys" value="true" /> <!-- 配置默認(rèn)的執(zhí)行器.SIMPLE就是普通執(zhí)行器;REUSE執(zhí)行器會(huì)重用預(yù)處理語(yǔ)句(prepared statements);BATCH執(zhí)行器將重用語(yǔ)句并執(zhí)行批量更新 --> <setting name="defaultExecutorType" value="SIMPLE" /> <!-- 指定 MyBatis 所用日志的具體實(shí)現(xiàn) --> <setting name="logImpl" value="SLF4J" /> <!-- 使用駝峰命名法轉(zhuǎn)換字段 --> <!-- <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語(yǔ)句,一般用于本地開(kāi)發(fā)測(cè)試 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl logging: level: com.ljf.mapper: debug #分頁(yè)插件 pagehelper: helper-dialect: mysql params: count=countSql reasonable: true #開(kāi)啟優(yōu)化,如果開(kāi)啟優(yōu)化,在分頁(yè)頁(yè)碼結(jié)果沒(méi)有數(shù)據(jù)的時(shí)候,會(huì)顯示有數(shù)據(jù)的頁(yè)碼數(shù)據(jù) support-methods-arguments: true #是否支持接口參數(shù)來(lái)傳遞分頁(yè)參數(shù),默認(rèn)false
1.2.5 調(diào)用類
@SpringBootApplication @MapperScan("com.ljf.mapper") //@ComponentScan("com.ljf") public class MainAppExe { public static void main(String[] args) { SpringApplication.run(MainAppExe.class, args); //調(diào)用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(); //結(jié)束進(jìn)程 System.exit(0); } }
1.2.6 執(zhí)行結(jié)果
到此這篇關(guān)于Mybatis以main方法形式調(diào)用dao層執(zhí)行代碼實(shí)例的文章就介紹到這了,更多相關(guān)Mybatis調(diào)用dao層內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Elasticsearch索引結(jié)構(gòu)與算法解析
?作為搜索引擎的一部分,ES自然具有速度快、結(jié)果準(zhǔn)確、結(jié)果豐富等特點(diǎn),那么ES是如何達(dá)到“搜索引擎”級(jí)別的查詢效率呢?首先是索引,其次是壓縮算法,接下來(lái)我們就一起了解下ES的索引結(jié)構(gòu)和壓縮算法2023-04-04關(guān)于在IDEA熱部署插件JRebel使用問(wèn)題詳解
這篇文章主要介紹了關(guān)于在IDEA熱部署插件JRebel使用問(wèn)題詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Java對(duì)象轉(zhuǎn)json JsonFormat注解
這篇文章主要介紹了Java對(duì)象轉(zhuǎn)json JsonFormat注解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Java并發(fā)工具之CyclicBarrier使用詳解
這篇文章主要介紹了Java并發(fā)工具之CyclicBarrier使用詳解,CyclicBarrier是一個(gè)同步器,允許一組線程相互之間等待,直到到達(dá)某個(gè)公共屏障點(diǎn)(common barrier point),再繼續(xù)執(zhí)行,需要的朋友可以參考下2023-12-12Springboot基礎(chǔ)學(xué)習(xí)之初識(shí)SpringBoot
今天帶大家學(xué)習(xí)Springboot基礎(chǔ)知識(shí),文中有非常詳細(xì)的圖文解說(shuō)及代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05spring boot activiti工作流的搭建與簡(jiǎn)單使用
這篇文章主要給大家介紹了關(guān)于spring boot activiti工作流的搭建與簡(jiǎn)單使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08java實(shí)現(xiàn)解析二進(jìn)制文件的方法(字符串、圖片)
本篇文章主要介紹了java實(shí)現(xiàn)解析二進(jìn)制文件的方法(字符串、圖片),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02java String類功能、原理與應(yīng)用案例【統(tǒng)計(jì)、判斷、轉(zhuǎn)換等】
這篇文章主要介紹了java String類功能、原理與應(yīng)用案例,結(jié)合實(shí)例形式詳細(xì)分析了java String類的基本功能、構(gòu)造方法,以及使用String類實(shí)現(xiàn)統(tǒng)計(jì)、判斷、轉(zhuǎn)換等功能相關(guān)操作技巧,需要的朋友可以參考下2019-03-03