推薦一種加密方式
阿新 • • 發佈:2021-01-13
技術標籤:Spring學習
Myabatis基礎搭建
步驟:
- 匯入相關jar包
① junit
②mybatis
③spring
④mysql
⑤aop
⑥mybatis-spring
⑦lombok
⑧資源過濾
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version >
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId >org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId >
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
- 編寫配置檔案
- mybatis-config
- mybatisUtils
- 實體類
- 介面
- 介面實現類
- 測試
Myabatis整合Spring
步驟:
- 編寫資料配置源
- sqlSessionFactory
- sqlSessionTemplate
- 給介面增加實現類
- 將實現類注入到spring中
- 測試
對比:
- 可以用applicationContext.xml配置,替換掉mybatis中的mybatisUtils.class和mybatis-config.xml檔案
- 增加的實現類可以通過spring注入,拿到SqlSession並把建立的權力交給spring,不用手動建立SqlSession物件了
- 在實現類裡操作mybatis裡測試時需要拿到的SqlSession物件及getMapper方法
- 總的來說,就三個地方的修改:
- 用資料來源替代mybatisUtils.class和mybatis-config.xml檔案
- 在spring中拿到sqlSessionFactory
- 在spring中拿到sqlSession