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

Spring整合mybatis、springMVC總結(jié)

 更新時(shí)間:2023年05月04日 11:30:55   作者:Studying_swz  
這篇文章主要為大家詳細(xì)介紹了Java整合Mybatis,SpringMVC,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1.mybatis配置流程

  • 實(shí)體類pojo類
  • 編寫Dao層(UserMapper接口以及xml文件)
  • 編寫Service接口以及實(shí)現(xiàn)類,通過Dao層對(duì)象進(jìn)行訪問數(shù)據(jù)庫
  • 創(chuàng)建mybatis的核心配置文件mybatis-config.xml,并將UserMapper綁定到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>
	<environments default="development">
		<environment id="development">
		<transactionManager type="JDBC"/>
		<dataSource type="POOLED">
			<property name="driver" value="com.mysql.jdbc.Driver"/>
			<property name="url"
			value="jdbc:mysql://localhost:3306/mybatis?
			useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
			<property name="username" value="root"/>
			<property name="password" value="123456"/>
		</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/kuang/dao/userMapper.xml"/>
	</mappers>
</configuration>

  • 引入到spring后的配置
<?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>

	<!-- 原本是要去配置數(shù)據(jù)源信息的,但是和spring整合后,此步驟將交給spring去做-->
	
	<mappers>
		<mapper resource="com/kuang/dao/userMapper.xml"/>
	</mappers>
</configuration>

  • 編寫MyBatis工具類
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
	private static SqlSessionFactory sqlSessionFactory;
	static {
		try {
			String resource = "mybatis-config.xml";
			InputStream inputStream =	Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//獲取SqlSession連接
	public static SqlSession getSession(){
		return sqlSessionFactory.openSession();
	}
}

2.spring配置流程

  • 在xml文件中注冊(cè)bean對(duì)象(沒有用注解)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!--bean就是java對(duì)象 , 由Spring創(chuàng)建和管理-->
	<bean id="hello" class="com.kuang.pojo.Hello">
		<property name="name" value="Spring"/>
	</bean>
</beans>
  • 或者在xml文件中使用掃描包(配合注解)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--指定注解掃描包-->
	<context:component-scan base-package="com.kuang.pojo"/>
</beans>

3.spring 整合Dao層

  • 創(chuàng)建數(shù)據(jù)庫配置文件 database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mytest?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
  • 創(chuàng)建spring整個(gè)Dao層的配置文件spring-dao.xml,主要整合數(shù)據(jù)庫連接文件,連接池信息,sqlsessionFactory(需綁定Mybatis配置文件去讀取配置Mapper信息),dao層動(dòng)態(tài)注入到Spring中
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

    <!--1,關(guān)聯(lián)數(shù)據(jù)庫連接文件-->
    <context:property-placeholder location="classpath:database.properties"/>
    <!--2,連接池 c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!-- c3p0連接池的私有屬性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 關(guān)閉連接后不自動(dòng)commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 獲取連接超時(shí)時(shí)間 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 當(dāng)獲取連接失敗重試次數(shù) -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!-- 3,配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 綁定mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!-- 4,配置dao層,動(dòng)態(tài)實(shí)現(xiàn)了接口可以注入到Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.kuangshen.dao"/>
    </bean>
</beans>

分析:spring-dao.xml文件整合了mybatis的mybatis-config.xml文件中的數(shù)據(jù)庫配置信息以及工具類和dao層的bean信息。

4.spring整合Service層

創(chuàng)建spring整合service層的文件spring-service.xml,主要配置讓spring去掃描service包的位置,配置impl中要注入的mapper信息,聲明事務(wù)的提交方式,以及AOP事務(wù)的支持。

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--掃描service下的包 -->
    <context:component-scan base-package="com.swz.service"/>
    <!-- 2業(yè)務(wù)類注入到spring-->
    <bean id="UserServiceImpl" class="com.swz.service.UserServiceImpl">
        <property name="userMapper" ref="userMapper"/>
    </bean>
    <!-- 3聲明事務(wù)提交方式-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 4AOP事務(wù)支持-->
</beans>

注意:<context:component-scan base-package=“com.swz.service”/> 在xml配置了這個(gè)標(biāo)簽后,spring可以自動(dòng)去掃描base-pack下面或者子包下面的Java文件,如果掃描到有@Component @Controller@Service等這些注解的類,則把這些類注冊(cè)為bean。

5.spring整合MVC層

創(chuàng)建spring整合mvc層的文件spring-mvc.xml,主要配置springMVC靜態(tài)資源過濾,開啟springMVC注解驅(qū)動(dòng)

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--1.注解驅(qū)動(dòng)-->
    <mvc:annotation-driven/>
    <!--2處理器映射器和處理器適配器-->
    <mvc:default-servlet-handler/>
    <!--3掃描包-->
    <context:component-scan base-package="com.leshangju.controller"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

6. spring整合dao-service-mvc三層

將以上配置的spring-dao.xml,spring-service.xml,spring-mvc.xml整合到applicationContext.xml中,同一個(gè)spring文件中來,實(shí)現(xiàn)最終的整合。

注意:此步驟正確的方式是每配置完一個(gè)xml都注入整合進(jìn)來一次,而不是最終才整合。

以上就是Spring整合mybatis、springMVC總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Spring整合mybatis、springMVC的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • mybatis抽取基類BaseMapper增刪改查的實(shí)現(xiàn)

    mybatis抽取基類BaseMapper增刪改查的實(shí)現(xiàn)

    目前項(xiàng)目當(dāng)中使用mapper.xml文件方式對(duì)數(shù)據(jù)庫進(jìn)行操作,但是每個(gè)里邊都有增/刪/改/查,為了方便開發(fā),把這些公共的代碼提取出來,不用當(dāng)做基類,不用每個(gè)Mapper文件都寫了,本文就詳細(xì)的介紹一下實(shí)現(xiàn)方法
    2021-09-09
  • SpringBoot配置log4j2的實(shí)現(xiàn)示例

    SpringBoot配置log4j2的實(shí)現(xiàn)示例

    SpringBoot中默認(rèn)使用Logback作為日志框架,本文主要介紹了SpringBoot配置log4j2的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • 如何通過源碼了解Java的自動(dòng)裝箱拆箱詳解

    如何通過源碼了解Java的自動(dòng)裝箱拆箱詳解

    裝箱就是把基本類型轉(zhuǎn)換成包裝類,拆箱就是把包裝類轉(zhuǎn)換成基本類型,下面這篇文章主要給大家介紹了關(guān)于如何通過源碼了解Java的自動(dòng)裝箱拆箱的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Java實(shí)現(xiàn)對(duì)字符串中的數(shù)值進(jìn)行排序操作示例

    Java實(shí)現(xiàn)對(duì)字符串中的數(shù)值進(jìn)行排序操作示例

    這篇文章主要介紹了Java實(shí)現(xiàn)對(duì)字符串中的數(shù)值進(jìn)行排序操作,涉及java字符串與數(shù)組的相互轉(zhuǎn)換以及數(shù)組排序相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05
  • 基于SpringBoot制作一個(gè)PDF切圖小工具

    基于SpringBoot制作一個(gè)PDF切圖小工具

    這篇文章主要為大家詳細(xì)介紹了如何基于SpringBoot制作一個(gè)PDF切圖小工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • SpringCloud通過MDC實(shí)現(xiàn)分布式鏈路追蹤

    SpringCloud通過MDC實(shí)現(xiàn)分布式鏈路追蹤

    在DDD領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)中,我們使用SpringCloud來去實(shí)現(xiàn),但排查錯(cuò)誤的時(shí)候,通常會(huì)想到Skywalking,但是引入一個(gè)新的服務(wù),增加了系統(tǒng)消耗和管理學(xué)習(xí)成本,對(duì)于大型項(xiàng)目比較適合,但是小的項(xiàng)目顯得太過臃腫了,所以本文介紹了SpringCloud通過MDC實(shí)現(xiàn)分布式鏈路追蹤
    2024-11-11
  • 詳解SpringBoot Mybatis如何對(duì)接多數(shù)據(jù)源

    詳解SpringBoot Mybatis如何對(duì)接多數(shù)據(jù)源

    這篇文章主要為大家介紹了SpringBoot Mybatis如何對(duì)接多數(shù)據(jù)源實(shí)現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Java 類在 Tomcat 中是如何加載的(過程分析)

    Java 類在 Tomcat 中是如何加載的(過程分析)

    這篇文章主要介紹了Java 類在 Tomcat 中是如何加載的過程分析,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java如何分析算法的時(shí)間和空間復(fù)雜度

    Java如何分析算法的時(shí)間和空間復(fù)雜度

    這篇文章主要介紹了Java如何分析算法的時(shí)間和空間復(fù)雜度,在計(jì)算機(jī)科學(xué)中,計(jì)算復(fù)雜性解釋了算法的性能。文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-06-06
  • 以實(shí)例簡介Java中線程池的工作特點(diǎn)

    以實(shí)例簡介Java中線程池的工作特點(diǎn)

    這篇文章主要介紹了以實(shí)例簡介Java中線程池的工作特點(diǎn),線程池是Java實(shí)現(xiàn)多線程編程的基礎(chǔ),需要的朋友可以參考下
    2015-09-09

最新評(píng)論

苍溪县| 淮安市| 新平| 三原县| 台南县| 昌图县| 枣阳市| 冷水江市| 安达市| 新干县| 庆城县| 安阳市| 阜新市| 中超| 许昌县| 盐城市| 兴国县| 女性| 金山区| 黑龙江省| 南雄市| 论坛| 浑源县| 隆尧县| 温州市| 伊通| 高平市| 鄯善县| 全南县| 军事| 宜州市| 娄底市| 广宁县| 乐都县| 井研县| 岳阳市| 栾城县| 松滋市| 贡嘎县| 翁牛特旗| 青铜峡市|