SSM學習4:spring整合mybatis、spring整合Junit

spring整合mybatis

之前的內容是有service層(業務實現層)、dao層(操作數據庫),現在新添加一個domain(與業務相關的實體類)

依賴配置
pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.itheima</groupId><artifactId>spring_15_spring_mybatis</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.16</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency></dependencies>
</project>

注解配置
resource/jdbc.properties
mysql數據庫相關的配置信息

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false
jdbc.username=root
jdbc.password=

config/JdbcConfig.class

package com.itheima.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;import javax.sql.DataSource;public class JdbcConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String userName;@Value("${jdbc.password}")private String password;@Beanpublic DataSource dataSource(){DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;}
}

config/MybatisConfig.class

public class MybatisConfig {//定義bean,SqlSessionFactoryBean,用于產生SqlSessionFactory對象// DataSource dataSource,@Bean定義的方法,形參會根據類型自動注入,也就是上面的JdbcConfig中的DataSource @Beanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();// 掃描所有的實體類ssfb.setTypeAliasesPackage("com.itheima.domain");ssfb.setDataSource(dataSource);return ssfb;}//定義bean,返回MapperScannerConfigurer對象@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc = new MapperScannerConfigurer();// 掃描與數據庫操作的類msc.setBasePackage("com.itheima.dao");return msc;}
}

config/SpringConfig.class

@Configuration
@ComponentScan("com.itheima")
//@PropertySource:加載類路徑jdbc.properties文件
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}

業務類
AccountDao
操作數據庫,應該是通過MyBatis操作數據庫

public interface AccountDao {@Insert("insert into tbl_account(name,money)values(#{name},#{money})")void save(Account account);@Delete("delete from tbl_account where id = #{id} ")void delete(Integer id);@Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ")void update(Account account);@Select("select * from tbl_account")List<Account> findAll();@Select("select * from tbl_account where id = #{id} ")Account findById(Integer id);
}

AccountServiceImpl

@Service
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;public void save(Account account) {accountDao.save(account);}public void update(Account account){accountDao.update(account);}public void delete(Integer id) {accountDao.delete(id);}public Account findById(Integer id) {return accountDao.findById(id);}public List<Account> findAll() {return accountDao.findAll();}
}

App

public class App {public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);AccountService accountService = ctx.getBean(AccountService.class);Account ac = accountService.findById(1);System.out.println(ac);}
}

在這里插入圖片描述

spring整合Junit

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.itheima</groupId><artifactId>spring_16_spring_junit</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.2.10.RELEASE</version></dependency></dependencies>
</project>

測試類
test/java/com.itheima.service/AccountServiceTest

//設置類運行器
@RunWith(SpringJUnit4ClassRunner.class)
//設置Spring環境對應的配置類
@ContextConfiguration(classes = SpringConfig.class)
public class AccountServiceTest {//支持自動裝配注入bean@Autowiredprivate AccountService accountService;@Testpublic void testFindById() {System.out.println(accountService.findById(1));}@Testpublic void testFindAll() {System.out.println(accountService.findAll());}
}

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/39101.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/39101.shtml
英文地址,請注明出處:http://en.pswp.cn/web/39101.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

解決ScaleBox來實現大屏自適應時,頁面的餅圖會變形的問題

封裝一個公用組件pieChartAdaptation.vue 代碼如下&#xff1a; <template><div :style"styleObject" class"pie-chart-adaptation"><slot></slot></div> </template><script setup lang"ts"> impo…

2.2.3 C#中顯示控件BDPictureBox 的實現----控件實現

2.2.3 C#中顯示控件BDPictureBox 的實現----控件實現 1 界面控件布局 2圖片內存Mat類說明 原始圖片&#xff1a;m_raw_mat ,Display_Mat()調用時更新或者InitDisplay_Mat時更新局部放大顯示圖片&#xff1a;m_extract_zoom_mat&#xff0c;更新scale和scroll信息后更新overla…

2024年精選100道軟件測試面試題(內含文檔)

測試技術面試題 1、我現在有個程序&#xff0c;發現在 Windows 上運行得很慢&#xff0c;怎么判別是程序存在問題還是軟硬件系統存在問題&#xff1f; 2、什么是兼容性測試&#xff1f;兼容性測試側重哪些方面&#xff1f; 3、測試的策略有哪些&#xff1f; 4、正交表測試用…

Eureka與Spring Cloud Bus的協同:打造智能服務發現新篇章

Eureka與Spring Cloud Bus的協同&#xff1a;打造智能服務發現新篇章 在微服務架構中&#xff0c;服務發現是實現服務間通信的關鍵機制。Eureka作為Netflix開源的服務發現框架&#xff0c;與Spring Cloud Bus的集成&#xff0c;提供了一種動態、響應式的服務治理解決方案。本文…

市場規模5萬億,護理員缺口550萬,商業護理企業如何解決服務供給難題?

干貨搶先看 1. 據統計&#xff0c;我國失能、半失能老人數量約4400萬&#xff0c;商業護理服務市場規模達5萬億。然而&#xff0c;當前養老護理員缺口巨大&#xff0c;人員的供需不匹配是很多養老服務企業需要克服的難題。 2. 當前居家護理服務的主要市場參與者分為兩類&…

利用GPT 將 matlab 內置 bwlookup 函數轉C

最近業務需要將 matlab中bwlookup 的轉C 這個函數沒有現成的m文件參考&#xff0c;內置已經打成庫了&#xff0c;所以沒有參考源代碼 但是它的解釋還是很清楚的&#xff0c;可以根據這個來寫 Nonlinear filtering using lookup tables - MATLAB bwlookup - MathWorks 中國 A…

python請求報錯::requests.exceptions.ProxyError: HTTPSConnectionPool

在發送網頁請求時&#xff0c;發現很久未響應&#xff0c;最后報錯&#xff1a; requests.exceptions.ProxyError: HTTPSConnectionPool(hostsvr-6-9009.share.51env.net, port443): Max retries exceeded with url: /prod-api/getInfo (Caused by ProxyError(Unable to conne…

秒懂設計模式--學習筆記(5)【創建篇-抽象工廠】

目錄 4、抽象工廠4.1 介紹4.2 品牌與系列&#xff08;針對工廠泛濫&#xff09;(**分類**)4.3 產品規劃&#xff08;**數據模型**&#xff09;4.4 生產線規劃&#xff08;**工廠類**&#xff09;4.5 分而治之4.6 抽象工廠模式的各角色定義如下4.7 基于此抽象工廠模式以品牌與系…

vue啟動時的錯誤

解決辦法一&#xff1a;在vue.config.js中直接添加一行代碼 lintOnSave:false 關閉該項目重新運行就可啟動 解決辦法二&#xff1a; 修改組件名稱

Python容器 之 通用功能

1.切片 1.格式&#xff1a; 數據[起始索引:結束索引:步長 2.適用類型&#xff1a; 字符串(str)、列表(list)、元組(tuple) 3.說明&#xff1a; 通過切片操作, 可以獲取數據中指定部分的內容 4.注意 : 結束索引對應的數據不會被截取到 支持正向索引和逆向索引 步長用于設置截取…

配音軟件有哪些?分享五款超級好用的配音軟件

隨著嫦娥六號的壯麗回歸&#xff0c;舉國上下都沉浸在這份自豪與激動之中。 在這樣一個歷史性的時刻&#xff0c;我們何不用聲音記錄下這份情感&#xff0c;讓這份記憶以聲音的形式流傳&#xff1f; 無論是制作視頻分享這份喜悅&#xff0c;還是創作音頻講述探月故事&#xff…

Oracle數據庫中RETURNING子句

RETURNING子句允許您檢索插入、刪除或更新所修改的列&#xff08;以及基于列的表達式&#xff09;的值。如果不使用RETURNING&#xff0c;則必須在DML語句完成后運行SELECT語句&#xff0c;才能獲得更改列的值。因此&#xff0c;RETURNING有助于避免再次往返數據庫&#xff0c;…

Plotly:原理、使用與數據可視化的未來

文章目錄 引言Plotly的原理Plotly的基本使用安裝Plotly創建基本圖表定制圖表樣式 Plotly的高級特性交互式圖表圖表動畫圖表集成 結論 引言 在當今的數據驅動世界中&#xff0c;數據可視化已經成為了一個至關重要的工具。它允許我們直觀地理解數據&#xff0c;發現數據中的模式…

CXL-GPU: 全球首款實現百ns以內的低延遲CXL解決方案

數據中心在追求更高性能和更低總擁有成本&#xff08;TCO&#xff09;的過程中面臨三大主要內存挑戰。首先&#xff0c;當前服務器內存層次結構存在局限性。直接連接的DRAM與固態硬盤&#xff08;SSD&#xff09;存儲之間存在三個數量級的延遲差異。當處理器直接連接的內存容量…

VideoPrism——探索視頻分析領域模型的算法與應用

概述 論文地址:https://arxiv.org/pdf/2402.13217.pdf 視頻是我們觀察世界的生動窗口&#xff0c;記錄了從日常瞬間到科學探索的各種體驗。在這個數字時代&#xff0c;視頻基礎模型&#xff08;ViFM&#xff09;有可能分析如此海量的信息并提取新的見解。迄今為止&#xff0c;…

【vuejs】vue-router 路由跳轉參數傳遞詳解和應用場景及技巧

1. Vue2 Router 路由基礎 1.1 路由定義 路由定義是Vue Router中實現頁面路由跳轉的基礎。在Vue2中&#xff0c;路由的定義通常在應用的入口文件或路由配置文件中進行。路由定義涉及到路徑模式&#xff08;path&#xff09;、視圖組件&#xff08;component&#xff09;以及一…

【數據分析思維--史上最全最牛逼】

前言&#xff1a; &#x1f49e;&#x1f49e;大家好&#xff0c;我是書生?&#xff0c;主要和大家分享一下數據分析的思維&#xff01;怎么提好我們對于業務的判斷是非常重要的&#xff01;&#xff01;&#xff01;希望對大家有所幫助。 &#x1f49e;&#x1f49e;代碼是你…

采煤機作業3D虛擬仿真教學線上展示增強應急培訓效果

在化工行業的生產現場&#xff0c;安全永遠是首要之務。為了加強從業人員的應急響應能力和危機管理能力&#xff0c;紛紛引入化工行業工藝VR模擬培訓&#xff0c;讓應急演練更加生動、高效。 化工行業工藝VR模擬培訓軟件基于真實的廠區環境&#xff0c;精確還原了各類事件場景和…

醫療器械FDA | 醫療器械軟件如何做源代碼審計?

醫療器械網絡安全測試https://link.zhihu.com/?targethttps%3A//www.wanyun.cn/Support%3Fshare%3D24315_ea8a0e47-b38d-4cd6-8ed1-9e7711a8ad5e 醫療器械源代碼審計是一個確保醫療器械軟件安全性和可靠性的重要過程。以下是醫療器械源代碼審計的主要步驟和要點&#xff0c;以…

Vue3 sortablejs 表格拖拽后,表格無法更新的問題處理

實用sortablejs在vue項目中實現表格行拖拽排序 你可能會發現&#xff0c;表格排序是可以實現&#xff0c;但是我們基于數據驅動的vue中關聯的數據并沒有發生變化&#xff0c; 如果你的表格帶有列固定(固定列實際上在dom中有兩個表格&#xff0c;其中固定的列在一個表格中&…