Spring整合mybatis、springMVC總結(jié)
1.mybatis配置流程
- 實(shí)體類pojo類
- 編寫Dao層(UserMapper接口以及xml文件)
- 編寫Service接口以及實(shí)現(xiàn)類,通過(guò)Dao層對(duì)象進(jì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&useUnicode=true&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ù)庫(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)一次,而不是最終才整合。
以上就是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)
目前項(xiàng)目當(dāng)中使用mapper.xml文件方式對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作,但是每個(gè)里邊都有增/刪/改/查,為了方便開發(fā),把這些公共的代碼提取出來(lái),不用當(dāng)做基類,不用每個(gè)Mapper文件都寫了,本文就詳細(xì)的介紹一下實(shí)現(xiàn)方法2021-09-09
SpringBoot配置log4j2的實(shí)現(xiàn)示例
SpringBoot中默認(rèn)使用Logback作為日志框架,本文主要介紹了SpringBoot配置log4j2的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
如何通過(guò)源碼了解Java的自動(dòng)裝箱拆箱詳解
裝箱就是把基本類型轉(zhuǎn)換成包裝類,拆箱就是把包裝類轉(zhuǎn)換成基本類型,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)源碼了解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ù)組的相互轉(zhuǎn)換以及數(shù)組排序相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
SpringCloud通過(guò)MDC實(shí)現(xiàn)分布式鏈路追蹤
在DDD領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)中,我們使用SpringCloud來(lái)去實(shí)現(xiàn),但排查錯(cuò)誤的時(shí)候,通常會(huì)想到Skywalking,但是引入一個(gè)新的服務(wù),增加了系統(tǒng)消耗和管理學(xué)習(xí)成本,對(duì)于大型項(xiàng)目比較適合,但是小的項(xiàng)目顯得太過(guò)臃腫了,所以本文介紹了SpringCloud通過(guò)MDC實(shí)現(xiàn)分布式鏈路追蹤2024-11-11
詳解SpringBoot Mybatis如何對(duì)接多數(shù)據(jù)源
這篇文章主要為大家介紹了SpringBoot Mybatis如何對(duì)接多數(shù)據(jù)源實(shí)現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Java 類在 Tomcat 中是如何加載的(過(guò)程分析)
這篇文章主要介紹了Java 類在 Tomcat 中是如何加載的過(guò)程分析,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
以實(shí)例簡(jiǎn)介Java中線程池的工作特點(diǎn)
這篇文章主要介紹了以實(shí)例簡(jiǎn)介Java中線程池的工作特點(diǎn),線程池是Java實(shí)現(xiàn)多線程編程的基礎(chǔ),需要的朋友可以參考下2015-09-09

