??系列專欄:Spring系列專欄
??個人主頁:個人主頁
目錄
一、Spring整合
1.Spring整合Mybatis思路分析
1.環(huán)境準(zhǔn)備
2.整合思路分析
2.Spring整合Mybatis
3.Spring整合Junit
1.環(huán)境準(zhǔn)備
2.整合Junit步驟?
二、圖書推薦
1.《元宇宙Ⅱ:圖解元技術(shù)區(qū)塊鏈、元資產(chǎn)與Web3.0、元人與理想國(全三冊)》
?2.《從零開始讀懂量子力學(xué)(精裝加強(qiáng)版)》
一、Spring整合
1.Spring整合Mybatis思路分析
1.環(huán)境準(zhǔn)備
在準(zhǔn)備環(huán)境的過程中,我們也來回顧下Mybatis開發(fā)的相關(guān)內(nèi)容:
create database spring_db character set utf8;
use spring_db;
create table tbl_account(
id int primary key auto_increment,
name varchar(35),
money double
);
<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>
</dependencies>
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
//setter...getter...toString...方法略
}
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);
}
public interface AccountService {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
}
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private 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();
}
}
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false
jdbc.username=root
jdbc.password=root
<?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>
<!--讀取外部properties配置文件-->
<properties resource="jdbc.properties"></properties>
<!--別名掃描的包路徑-->
<typeAliases>
<package name="com.itheima.domain"/>
</typeAliases>
<!--數(shù)據(jù)源-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}">
</property>
<property name="password" value="${jdbc.password}">
</property>
</dataSource>
</environment>
</environments>
<!--映射文件掃描包路徑-->
<mappers>
<package name="com.itheima.dao"></package>
</mappers>
</configuration>
public class App {
public static void main(String[] args) throws IOException {
// 1. 創(chuàng)建SqlSessionFactoryBuilder對象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 2. 加載SqlMapConfig.xml配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 3. 創(chuàng)建SqlSessionFactory對象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
// 4. 獲取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5. 執(zhí)行SqlSession對象執(zhí)行查詢,獲取結(jié)果User
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
Account ac = accountDao.findById(1);
System.out.println(ac);
// 6. 釋放資源
sqlSession.close();
}
}

2.整合思路分析
- Mybatis程序核心對象分析
?從圖中可以獲取到,真正需要交給Spring管理的是SqlSessionFactory
- 整合Mybatis,就是將Mybatis用到的內(nèi)容交給Spring管理,分析下配置文件
- 第一行讀取外部properties配置文件,Spring有提供具體的解決方案@PropertySource ,需要交給Spring
- 第二行起別名包掃描,為SqlSessionFactory服務(wù)的,需要交給Spring
- 第三行主要用于做連接池,Spring之前我們已經(jīng)整合了Druid連接池,這塊也需要交給Spring
- 前面三行一起都是為了創(chuàng)建SqlSession對象用的,那么用Spring管理SqlSession對象嗎?
- 回憶下SqlSession是由SqlSessionFactory創(chuàng)建出來的,所以只需要將SqlSessionFactory交給Spring管理即可。
- 第四行是Mapper接口和映射文件[如果使用注解就沒有該映射文件],這個是在獲取SqlSession以后執(zhí)行具體操作的時候用,所以它和SqlSessionFactory創(chuàng)建的時機(jī)都不在同一個時間,可能需要單獨(dú)管理。
2.Spring整合Mybatis
<dependency>
<!--Spring操作數(shù)據(jù)庫需要該jar包-->
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<!--
Spring與Mybatis整合的jar包
這個jar包mybatis在前面,是Mybatis提供的
-->
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
//配置類注解
@Configuration
//包掃描,主要掃描的是項(xiàng)目中的AccountServiceImpl類
@ComponentScan("com.itheima")
public class SpringConfig {
}
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;
@Bean
public DataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
}
}
@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import(JdbcConfig.class)
public class SpringConfig {
}
public class MybatisConfig {
//定義bean,SqlSessionFactoryBean,用于產(chǎn)生SqlSessionFactory對象
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
//設(shè)置模型類的別名掃描
ssfb.setTypeAliasesPackage("com.itheima.domain");
//設(shè)置數(shù)據(jù)源
ssfb.setDataSource(dataSource);
return ssfb;
}
//定義bean,返回MapperScannerConfigurer對象
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.itheima.dao");
return msc;
}
}
- 使用SqlSessionFactoryBean封裝SqlSessionFactory需要的環(huán)境信息
- 使用MapperScannerConfigurer加載Dao接口,創(chuàng)建代理對象保存到IOC容器中
@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}
public class App2 {
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);
}
}
- SqlSessionFactoryBean
- MapperScannerConfigurer
3.Spring整合Junit
1.環(huán)境準(zhǔn)備

2.整合Junit步驟?
<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>
//設(shè)置類運(yùn)行器
@RunWith(SpringJUnit4ClassRunner.class)
//設(shè)置Spring環(huán)境對應(yīng)的配置類
@ContextConfiguration(classes = SpringConfig.class)
public class AccountServiceTest {
//支持自動裝配注入bean
@Autowired
private AccountService accountService;
@Test
public void testFindById(){
System.out.println(accountService.findById(1));
}
@Test
public void testFindAll(){
System.out.println(accountService.findAll());
}
}
- 單元測試,如果測試的是注解配置類,則使用@ContextConfiguration(classes = 配置類.class)
- 單元測試,如果測試的是配置文件,則使用@ContextConfiguration(locations={配置文件 名,...})
- Junit運(yùn)行后是基于Spring環(huán)境運(yùn)行的,所以Spring提供了一個專用的類運(yùn)行器,這個務(wù)必要設(shè) 置,這個類運(yùn)行器就在Spring的測試專用包中提供的,導(dǎo)入的坐標(biāo)就是這個東西 SpringJUnit4ClassRunner
- 上面兩個配置都是固定格式,當(dāng)需要測試哪個bean時,使用自動裝配加載對應(yīng)的對象,下面的工作就和以前做Junit單元測試完全一樣了
?知識點(diǎn)2:@ContextConfiguration
筆記來自:黑馬程序員SSM框架教程
二、圖書推薦
1.《元宇宙Ⅱ:圖解元技術(shù)區(qū)塊鏈、元資產(chǎn)與Web3.0、元人與理想國(全三冊)》
看半小時漫畫,通元宇宙未來100年,300幅手繪插圖輕松讀懂虛實(shí)共生的未來世界。剖析元宇宙三大定律、大統(tǒng)一方程、熵增定律、Web3.0、萬億元資產(chǎn)、元人與區(qū)塊鏈文明,構(gòu)建元宇宙大樓。講透元技術(shù)區(qū)塊鏈、元宇宙基石Web3.0到穿越未來的技術(shù)大革命。厘清8大產(chǎn)業(yè)規(guī)律和11大投資方向,從元宇宙經(jīng)濟(jì)學(xué)到財(cái)富自由2.0,構(gòu)建NO.1無限∞世界的數(shù)字空間,從元人到理想國。
?內(nèi)容簡介
這是一個全新的時代:Web3.0構(gòu)建的經(jīng)濟(jì)體系,DID身份的跨平臺操作,數(shù)字NFT的原子級鏡像,以及DeFi的無摩擦元資產(chǎn)再分配......2022年,奇點(diǎn)出現(xiàn):元人即將誕生;元資產(chǎn)即將分配;元宇宙正在成形。本套書通過元宇宙三大定律、大統(tǒng)一方程、熵增定律、Web3.0、萬億元資產(chǎn)、元人與區(qū)塊鏈文明構(gòu)建了元宇宙第一大樓。第1-80層:數(shù)字人展位、電子寵物、數(shù)字藏品、3D沉侵式旅游、DeFi。第81-160層:AI、VR、AR、MR、DAO、Web3.0、邊緣計(jì)算。第161-214+層:多場景閱讀、4K空間、跨鏈許可、維度轉(zhuǎn)換、無限∞世界。
?
迫不及待的小伙伴也可以訪問下面的鏈接了解詳情:
《元宇宙Ⅱ:圖解元技術(shù)區(qū)塊鏈、元資產(chǎn)與Web3.0、元人與理想國(全三冊)》
?2.《從零開始讀懂量子力學(xué)(精裝加強(qiáng)版)》
量子力學(xué)全新升級版,每章增加了背景知識和相關(guān)的理論實(shí)驗(yàn)介紹, 新增了現(xiàn)代量子科技的前沿話題。增加21幅手繪物理插畫和作者本人創(chuàng)作的科技詩詞。從微小的原子到浩瀚的宇宙,從每一滴水到閃亮的鉆石,從劃破夜空的激光到你身邊的手機(jī),所有事物的背后都有量子力學(xué)在主宰!讓我們從零開始,一起走進(jìn)量子力學(xué)的世界!
?
?量子力學(xué)是現(xiàn)代物理學(xué)的基石,推動了科學(xué)技術(shù)的快速發(fā)展。在今天,量子依然是新聞熱點(diǎn)。
本書將為廣大科技愛好者系統(tǒng)、嚴(yán)謹(jǐn)?shù)亟榻B量子力學(xué)的基本原理和應(yīng)用。讀者需要熟悉高中物理和數(shù)學(xué)的相關(guān)內(nèi)容,愿意學(xué)習(xí)科學(xué)的思維方式。雖然量子力學(xué)是一門有著神秘面紗、打破生活常識、顛覆人類認(rèn)知的現(xiàn)代科學(xué),但是讀者只要愿意隨著本書一起思考,就一定能夠清楚地了解量子力學(xué)理論的基本概念,最終全面認(rèn)識它在科學(xué)體系中的作用和對現(xiàn)代技術(shù)的貢獻(xiàn)。
本書的敘述方式是一邊講解科學(xué)理論,一邊介紹重要的實(shí)驗(yàn)現(xiàn)象和科學(xué)原理的應(yīng)用。本書在第一篇中依次講解了狀態(tài)疊加、波粒二象性、不確定性原理等基本概念;在第二篇中介紹了量子力學(xué)在凝聚態(tài)物理和基本粒子物理領(lǐng)域中的應(yīng)用。同時,對由量子力學(xué)催生的現(xiàn)代電子技術(shù),也著重做了介紹。
?迫不及待的小伙伴也可以訪問下面的鏈接了解詳情:
《從零開始讀懂量子力學(xué)(精裝加強(qiáng)版) 解密諾貝爾獎聚焦話題量子糾纏的奧秘 零基礎(chǔ)學(xué)量子力學(xué)》
??本次送 3?本書 ,評論區(qū)抽3位小伙伴送書??
書籍名稱:《元宇宙Ⅱ:圖解元技術(shù)區(qū)塊鏈、元資產(chǎn)與Web3.0、元人與理想國(全三冊)》
活動時間:截止到 2023-05-08?20:00:00
抽獎方式:利用程序進(jìn)行抽獎。
參與方式:關(guān)注博主、點(diǎn)贊、收藏,評論區(qū)評論 "夏日炎炎,碼不停息!"
???獲獎名單??
在下小吉.
勾欄聽曲_0
幾分醉意.文章來源:http://www.zghlxwxcb.cn/news/detail-439480.html
名單公布時間: 2023-05-08?20:00:00文章來源地址http://www.zghlxwxcb.cn/news/detail-439480.html
到了這里,關(guān)于【Spring篇】Spring整合的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!