mybatis與spring整合ssm01

qq_50957390 發表於 2020-10-20

mybatis與spring整合

其實就是將SqlSessionFactory的建立交給spring管理

1. 新增相關依賴

<?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.zking</groupId>
    <artifactId>ssm1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>ssm1 Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <spring.version>5.0.2.RELEASE</spring.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- ********************** spring依賴 ********************** -->
        <!--1)spring核心依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--spring ioc依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--spring 擴充套件依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--2)spring dao層依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}
            </version>
        </dependency>
        
        <!--3)aop相關依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--4)spring web相關依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--5) spring test相關依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--6)springMVC框架-->
        <dependency>
	        <groupId>org.springframework</groupId>
	        <artifactId>spring- webmvc</artifactId>
	        <version>${spring.version}</version>
        </dependency>


        <!-- ********************** junit 單元測試依賴 ********************** -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- ********************** Java Servlet API  ********************** -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- ********************** Mybatis依賴 ********************** -->
        <!--1) mybatis核心 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        
        <!--2) Mybatis分頁外掛-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        
        <!--3) spring整合mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- ********************** Mysql JDBC驅動 ********************** -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>

        <!--dbcp2連線池依賴-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.3</version>
        </dependency>

        <!-- **********************  日誌 配置  ********************** -->
        <!--記得修改mybatis.cfg.xml新增如下內容-->
        <!--<setting name="logImpl" value="LOG4J2"/>-->
        
        <!--核心log4j2jar包-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.9.1</version>
        </dependency>
        
        <!--web工程需要包含log4j-web,非 web工程不需要-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.9.1</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>ssm1</finalName>
        <resources>
            <!--解決mybatis-generator-maven-plugin執行時沒有將XxxMapper.xml檔案放入target資料夾的問題-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!--解決mybatis-generator-maven-plugin執行時沒有將jdbc.properites檔案放入target資料夾的問題-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>jdbc.properties</include>
                    <include>*.xml</include>
                </includes>
            </resource>
        </resources>

        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-
                        generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <dependencies>
                        <!--使用Mybatis-generator外掛不能使用太高版本的mysql驅動-->
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.44</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
1.2 spring整合mybatis
     <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis-spring</artifactId>
           <version>1.3.1</version>
     </dependency>

1.3 新增dbcp2連線池依賴
    <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-dbcp2</artifactId>
           <version>2.1.1</version>
     </dependency>
     <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-pool2</artifactId>
           <version>2.4.3</version>
     </dependency>

2. 建立spring配置檔案spring.xml,並進行相關配置

  1. 註解式開發
  2. 引入外接jdbc配置檔案
  3. dbcp2資料庫連線池配置
  4. spring和MyBatis整合
  5. 註解式事務配置開始 <tx:annotation-driven transaction-manager=“transactionManager”/>

3. 註解式開發
@Repository 倉庫一般存在於dao層
@Service 服務的意思
@Constroller 控制層

@Component 萬能註解,如果你再類上面加了這個註解,它表示這一個主鍵它交給spring管理
@Scope 範圍的意思,就是表示範圍多列模式和單列模式

@Autowired
@Resource
注1:各個註解的作用請參考資料“註解式開發”

@Transactional
注2:個人感覺註解式事務比以前的宣告式事務更加麻煩,要寫的東西更多

4. Spring Test+JUnit完美組合
4.1 在工程的pom檔案中增加spring-test的依賴

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-test</artifactId>
     <version>${spring.version}</version>
</dependency>

4.2 建立BaseTestCase,並在該類上加上兩個註解:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={“classpath:spring.xml”})

4.3 使用idea的junit外掛(JUnitGenerator V2.0)生成測試用例,
注1:還需要修改相關配置,詳情參考資料:“IDEA Junit4配置.mht”
注2:快捷鍵,Ctrl + Shift + T

4.4 在執行單元測試時,為了避免產生髒資料,可將測試單元設定成事務回滾
@Rollback(value = true|false)//true回滾事務,false提交事務
@Transactional(transactionManager = “transactionManager”)

5. 修改分頁程式碼:
使用AOP程式設計解決分頁程式碼重複的問題
注1:AOP註解式開發詳情見資料“Spring如何實現AOP環繞通知.mht”
注2:AOP分頁實現“PageBeanAsp“

MAVEN指令:mybatis-generator:generate -e

實體類類的ToString&&set&&get的註解方法外掛

<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.0</version>
      <scope>provided</scope>
</dependency>