最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring集成Mybatis過程詳細講解

 更新時間:2023年03月16日 10:48:44   作者:假正經(jīng)的小柴  
mybatis-plus是一個Mybatis的增強工具,在Mybatis的基礎上只做增強不做改變,為簡化開發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關于SpringBoot整合Mybatis-plus案例及用法實例的相關資料,需要的朋友可以參考下

為啥學習集成Mybatis ORM框架

雖然Spring中提供了JDBCTemplate模塊,已經(jīng)很大程度了解決了JDBC代碼的復雜度,但它仍然是和Java代碼寫在一起的。反觀 Mybatis 將 Sql 語句寫在配置文件中,使得SQL語句和程序?qū)崿F(xiàn)了松耦合。而且提供了些許標簽,使得SQL可以是動態(tài)的。在ORM基礎上想要更好的用Spring的DI、AOP、事務處理、Junit支持等實現(xiàn)成果,學會使用 Spring 框架集成 Mybatis 是大勢所趨。

實現(xiàn)步驟

第一步:準備數(shù)據(jù)庫表

第二步:IDEA中創(chuàng)建一個模塊,并引入以下依賴

  • spring-context
  • spring-jdbc
  • mysql驅(qū)動
  • mybatis
  • mybatis-spring:mybatis提供的與spring框架集成的依賴
  • druid:德魯伊連接池
  • junit
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.0.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.11</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

第三步:基于三層架構實現(xiàn),所以提前創(chuàng)建好所有的包

  • XXXX.mapper
  • XXXX.service
  • XXXX.service.impl
  • XXXX.bean

第四步:編寫bean

Account,按bean規(guī)范創(chuàng)建

第五步:編寫Mapper接口

AccountMapper接口,定義與數(shù)據(jù)庫交互的方法

package com.ncpowernode.spring.mappers;
import com.ncpowernode.spring.bean.Account;
import java.util.List;
public interface AccountMapper {
    List<Account> selectAll();
    Account selectByActno(String actno);
    int insert(Account account);
    int deleteByActno(String actno);
    int update(Account account);
}

第六步:編寫mapper配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ncpowernode.spring.mappers.AccountMapper">
    <select id="selectAll" resultType="account">
        select * from t_act;
    </select>
    <insert id="insert">
        insert into t_act values(#{actno},#{balance})
    </insert>
    <delete id="deleteByActno">
        delete from t_act where actno = #{actno};
    </delete>
    <select id="selectByActno" resultType="account">
        select * from t_act where actno = #{actno};
    </select>
    <update id="update">
        update t_act set balance = #{balance} where actno = #{actno};
    </update>
</mapper>

第七步:編寫service接口和service接口的實現(xiàn)類

service接口

package com.ncpowernode.spring.service;
import com.ncpowernode.spring.bean.Account;
import java.util.List;
public interface AccountService {
    int save(Account act);
    int deleteByActno(String actno);
    List<Account> getAll();
    void transfer(String fromActno,String toActno,double money);
}

service實現(xiàn)

package com.ncpowernode.spring.service.impl;
import com.ncpowernode.spring.bean.Account;
import com.ncpowernode.spring.mappers.AccountMapper;
import com.ncpowernode.spring.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {
    @Autowired
    @Qualifier("accountMapper")
    private AccountMapper accountMapper;
    @Override
    public int save(Account act) {
        return accountMapper.insert(act);
    }
    @Override
    public int deleteByActno(String actno) {
        return accountMapper.deleteByActno(actno);
    }
    @Override
    public List<Account> getAll() {
        return accountMapper.selectAll();
    }
    @Override
    public void transfer(String fromActno, String toActno, double money) {
        // 判斷余額
        Account fromAccount = accountMapper.selectByActno(fromActno);
        if (fromAccount.getBalance()<money) throw new RuntimeException("余額不足");
        // 更新數(shù)據(jù)庫
        Account toAccount = accountMapper.selectByActno(toActno);
        fromAccount.setBalance(fromAccount.getBalance()-money);
        toAccount.setBalance(toAccount.getBalance()+money);
        int cnt = accountMapper.update(fromAccount);
        cnt += accountMapper.update(toAccount);
        // 判斷業(yè)務是否實現(xiàn)成功
        if(cnt!=2) throw new RuntimeException("轉(zhuǎn)賬失敗");
    }
}

第八步:編寫jdbc.properties配置文件

數(shù)據(jù)庫連接池相關信息

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/???
jdbc.username=???
jdbc.password=???

第九步:編寫mybatis-config.xml核心配置文件

<?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>
    <settings>
        <!--駝峰命名自動映射,默認false-->
        <setting name="mapUnderscoreToCamelCase" value="false"/>
        <!--設置懶加載-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--配置日志,可以幫助我們查看sql執(zhí)行信息-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
</configuration>

第十步:編寫spring.xml配置文件

組件掃描

引入外部的屬性文件

數(shù)據(jù)源

SqlSessionFactoryBean配置(注入mybatis核心配置文件路徑,指明別名包,注入數(shù)據(jù)源)

Mapper掃描配置器(指定掃描的包)

事務管理器(DataSourceTransactionManager,注入數(shù)據(jù)源)

啟動事務注解(注入事務管理器)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--組件掃描-->
    <context:component-scan base-package="com.ncpowernode.spring.service"/>
    <!--引入外部的屬性文件-->
    <context:property-placeholder location="jdbc.properties"/>
    <!--數(shù)據(jù)源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置SqlSessionFactoryBean-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入數(shù)據(jù)源-->
        <property name="dataSource" ref="dataSource"/>
        <!--指定別名包-->
        <property name="typeAliasesPackage" value="com.ncpowernode.spring.bean"/>
        <!--指定mybatis核心配置文件-->
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>
    <!--配置Mapper掃描配置器-->
    <!--主要掃描mapper接口,生成代理類-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ncpowernode.spring.mappers"/>
    </bean>
    <!--事務管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--啟動事務注解-->
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

第十一步:編寫測試程序,并添加事務,進行測試。

	@Test
    public void testSM(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountServiceImpl.class);
        accountService.getAll().forEach(System.out::println);
        accountService.transfer("act-001","act-002",10000.0);
    }

測試結果

到此這篇關于Spring集成Mybatis過程詳細講解的文章就介紹到這了,更多相關Spring集成Mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java異常的幾個謎題_動力節(jié)點Java學院整理

    Java異常的幾個謎題_動力節(jié)點Java學院整理

    本文給大家收藏整理java異常的幾個謎題,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-06-06
  • SpringBoot+MyBatis+AOP實現(xiàn)讀寫分離的示例代碼

    SpringBoot+MyBatis+AOP實現(xiàn)讀寫分離的示例代碼

    高并發(fā)這個階段,肯定是需要做MySQL讀寫分離的。本文主要介紹了SpringBoot+MyBatis+AOP實現(xiàn)讀寫分離的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Spring?Boot中的JdbcClient與JdbcTemplate使用對比分析

    Spring?Boot中的JdbcClient與JdbcTemplate使用對比分析

    這篇文章主要介紹了Spring Boot中的JdbcClient與JdbcTemplate使用對比分析,一起看看Spring Boot 中 JdbcClient 和 JdbcTemplate 之間的差異
    2024-01-01
  • 基于mybatis中test條件中單引號雙引號的問題

    基于mybatis中test條件中單引號雙引號的問題

    這篇文章主要介紹了基于mybatis中test條件中單引號雙引號的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java基礎之查找文本特定內(nèi)容后進行修改

    Java基礎之查找文本特定內(nèi)容后進行修改

    這篇文章主要介紹了Java基礎之查找文本特定內(nèi)容后進行修改,文中有非常詳細的代碼示例,對正在學習java基礎的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Spring mvc如何實現(xiàn)數(shù)據(jù)處理

    Spring mvc如何實現(xiàn)數(shù)據(jù)處理

    這篇文章主要介紹了Spring mvc如何實現(xiàn)數(shù)據(jù)處理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • java javax.annotation.Resource注解的詳解

    java javax.annotation.Resource注解的詳解

    這篇文章主要介紹了javax.annotation.Resource注解的詳解的相關資料,需要的朋友可以參考下
    2016-10-10
  • java之minio文件服務器的日常操作

    java之minio文件服務器的日常操作

    本文介紹如何在Java項目中配置Minio服務,通過創(chuàng)建minioConfig和minioDto來管理連接信息,并通過minioUtils工具類實現(xiàn)文件的上傳、下載和刪除功能,詳細說明了如何從application.yml文件中讀取配置,并強調(diào)了避免static情況下minioDto為null的問題,另外,提到刪除操作是延遲的
    2024-10-10
  • 使用Java根據(jù)文件路徑下載zip文件到本地代碼示例

    使用Java根據(jù)文件路徑下載zip文件到本地代碼示例

    在開發(fā)過程中我們會遇到需要對文件進行壓縮并下載的功能需求,這篇文章主要給大家介紹了關于如何使用Java根據(jù)文件路徑下載zip文件到本地的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-03-03
  • spring aop注解配置代碼實例

    spring aop注解配置代碼實例

    這篇文章主要介紹了spring aop注解配置代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04

最新評論

平远县| 兴国县| 加查县| 台北县| 若羌县| 镇原县| 蒲城县| 南澳县| 澄江县| 凤翔县| 隆子县| 巫溪县| 阜宁县| 越西县| 镇远县| 山阳县| 巫山县| 通江县| 景宁| 双桥区| 桐城市| 盐池县| 屏山县| 祁东县| 申扎县| 沁水县| 方城县| 仁布县| 富蕴县| 营口市| 广元市| 福泉市| 溧阳市| 西畴县| 昌图县| 仙游县| 南江县| 南开区| 云霄县| 齐河县| 墨脱县|