1、Spring 集成 Mybatis

applicationContext.xml
| |
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> |
| <property name="driverClassName" value="com.mysql.jdbc.Driver"/> |
| <property name="url" value="jdbc:mysql://localhost:3306/suns?useSSL=false"/> |
| <property name="username" value="root"/> |
| <property name="password" value="123456"/> |
| </bean> |
| |
| |
| <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> |
| <property name="dataSource" ref="dataSource"/> |
| <property name="typeAliasesPackage" value="com.baizhiedu.entity"/> |
| <property name="mapperLocations"> |
| <list> |
| <value>classpath:com.zzw.mapper/*Mapper.xml</value> |
| </list> |
| </property> |
| </bean> |
| |
| |
| <bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> |
| <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/> |
| <property name="basePackage" value="com.baizhiedu.dao"/> |
| </bean> |
| |
| |
| |
| |
| <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
| <property name="dataSource" ref="dataSource"/> |
| </bean> |
| |
| |
| <tx:annotation-driven transaction-manager="transactionManager"/> |
2、Spring 集成 SpringMVC
| * Spring 和 SpringMVC 本身就是一家产品, 是不用整合的 |
| * 但是现在的 Spring 容器自己无法启动, 我们需要在 web 容器启动的时候, 加载 Spring 的配置文件, 启动 Spring 容器 |
| * 那么这个工作是在 spring-web 包中的一个监听器来做的, 这个包不用单独导入, 他已经在 spring-webmvc 包中了 |
| * 它会监听 WEB 容器的启动和停止, 然后就可以控制 Spring 容器的启动和停止了 |
2.1、web.xml
| <?xml version="1.0" encoding="UTF-8"?> |
| <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" |
| version="4.0"> |
| |
| |
| <context-param> |
| <param-name>contextConfigLocation</param-name> |
| <param-value>classpath:applicationContext.xml</param-value> |
| </context-param> |
| |
| |
| |
| |
| |
| |
| <listener> |
| <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
| </listener> |
| |
| |
| <filter> |
| <filter-name>encFilter</filter-name> |
| <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> |
| <init-param> |
| <param-name>encoding</param-name> |
| <param-value>utf-8</param-value> |
| </init-param> |
| </filter> |
| <filter-mapping> |
| <filter-name>encFilter</filter-name> |
| <url-pattern>/*</url-pattern> |
| </filter-mapping> |
| |
| |
| <servlet> |
| <servlet-name>dispatcherServlet</servlet-name> |
| <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
| <init-param> |
| <param-name>contextConfigLocation</param-name> |
| <param-value>classpath:springmvc.xml</param-value> |
| </init-param> |
| <load-on-startup>1</load-on-startup> |
| </servlet> |
| <servlet-mapping> |
| <servlet-name>dispatcherServlet</servlet-name> |
| <url-pattern>/</url-pattern> |
| </servlet-mapping> |
| |
| </web-app> |
2.2、spring-mvc.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" |
| xmlns:p="http://www.springframework.org/schema/p" |
| xmlns:c="http://www.springframework.org/schema/c" |
| xmlns:util="http://www.springframework.org/schema/util" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xmlns:aop="http://www.springframework.org/schema/aop" |
| xmlns:tx="http://www.springframework.org/schema/tx" |
| xmlns:mvc="http://www.springframework.org/schema/mvc" |
| xsi:schemaLocation=" |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/util |
| http://www.springframework.org/schema/util/spring-util.xsd |
| http://www.springframework.org/schema/context |
| http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/aop |
| http://www.springframework.org/schema/aop/spring-aop.xsd |
| http://www.springframework.org/schema/tx |
| http://www.springframework.org/schema/tx/spring-tx.xsd |
| http://www.springframework.org/schema/mvc |
| http://www.springframework.org/schema/mvc/spring-mvc.xsd"> |
| |
| |
| <context:component-scan base-package="com.zzw.controller" /> |
| |
| |
| <mvc:annotation-driven /> |
| |
| |
| <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > |
| |
| </bean> |
| |
| |
| <mvc:default-servlet-handler/> |
| </beans> |
2.3、applicationContext.xml
核心:把 mybatis 的配置文件和 SqlSessionFactoryBean 扔到 spring 中,让 spring 管理 mapper 层
| <?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:p="http://www.springframework.org/schema/p" |
| xmlns:c="http://www.springframework.org/schema/c" |
| xmlns:util="http://www.springframework.org/schema/util" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xmlns:aop="http://www.springframework.org/schema/aop" |
| xmlns:tx="http://www.springframework.org/schema/tx" |
| xmlns:mvc="http://www.springframework.org/schema/mvc" |
| xsi:schemaLocation=" |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/util |
| http://www.springframework.org/schema/util/spring-util.xsd |
| http://www.springframework.org/schema/context |
| http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/aop |
| http://www.springframework.org/schema/aop/spring-aop.xsd |
| http://www.springframework.org/schema/tx |
| http://www.springframework.org/schema/tx/spring-tx.xsd |
| http://www.springframework.org/schema/mvc |
| http://www.springframework.org/schema/mvc/spring-mvc.xsd"> |
| |
| |
| <context:component-scan base-package="com.zzw.service" /> |
| |
| |
| <context:property-placeholder location="classpath:jdbc.properties" /> |
| |
| |
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> |
| <property name="username" value="${jdbc.username}"/> |
| <property name="password" value="${jdbc.password}"/> |
| <property name="url" value="${jdbc.url}"/> |
| <property name="driverClassName" value="${jdbc.driver}"/> |
| </bean> |
| |
| |
| |
| <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> |
| |
| <property name="dataSource" ref="dataSource"/> |
| |
| <property name="typeAliasesPackage" value="com.zzw.pojo"/> |
| </bean> |
| |
| |
| <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> |
| |
| <property name="sqlSessionFactoryBeanName" value="factory"/> |
| |
| <property name="basePackage" value="com.zzw.mapper"/> |
| </bean> |
| |
| |
| |
| <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
| <property name="dataSource" ref="dataSource"/> |
| </bean> |
| |
| |
| <tx:annotation-driven transaction-manager="transactionManager"/> |
| </beans> |
3、Spring 的父子容器
Spring 和 SpringMVC 的容器具有父子关系,Spring 容器为父容器,SpringMVC 为子容器
子容器可以引用父容器中的 Bean,而父容器不可以引用子容器中的 Bean
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步