溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring整合mybatis、springMVC的方法是什么

發(fā)布時(shí)間:2023-05-04 14:43:48 來(lái)源:億速云 閱讀:82 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Spring整合mybatis、springMVC的方法是什么的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Spring整合mybatis、springMVC的方法是什么文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

1.mybatis配置流程

  • 實(shí)體類pojo類

  • 編寫Dao層(UserMapper接口以及xml文件)

  • 編寫Service接口以及實(shí)現(xiàn)類,通過(guò)Dao層對(duì)象進(jìn)行訪問(wèn)數(shù)據(jù)庫(kù)

  • 創(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ì)象(沒(méi)有用注解)

<?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ù)庫(kù)配置文件 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ù)庫(kù)連接文件,連接池信息,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ù)庫(kù)連接文件-->
    <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ù)庫(kù)配置信息以及工具類和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)資源過(guò)濾,開啟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文件中來(lái),實(shí)現(xiàn)最終的整合。

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

關(guān)于“Spring整合mybatis、springMVC的方法是什么”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Spring整合mybatis、springMVC的方法是什么”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI