您现在的位置是:首页 >学无止境 >spring整合mybatis、springMVC(总结)网站首页学无止境

spring整合mybatis、springMVC(总结)

Studying_swz 2023-07-09 12:00:03
简介spring整合mybatis、springMVC(总结)

1.mybatis配置流程

  • 实体类pojo类

  • 编写Dao层(UserMapper接口以及xml文件)

  • 编写Service接口以及实现类,通过Dao层对象进行访问数据库

  • 创建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>
      
      	<!-- 原本是要去配置数据源信息的,但是和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文件中注册bean对象(没有用注解)

    <?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对象 , 由Spring创建和管理-->
    	<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层

  1. 创建数据库配置文件 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
    
  2. 创建spring整个Dao层的配置文件spring-dao.xml,主要整合数据库连接文件,连接池信息,sqlsessionFactory(需绑定Mybatis配置文件去读取配置Mapper信息),dao层动态注入到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,关联数据库连接文件-->
        <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"/>
            <!-- 关闭连接后不自动commit -->
            <property name="autoCommitOnClose" value="false"/>
            <!-- 获取连接超时时间 -->
            <property name="checkoutTimeout" value="10000"/>
            <!-- 当获取连接失败重试次数 -->
            <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层,动态实现了接口可以注入到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文件中的数据库配置信息以及工具类和dao层的bean信息。

4.spring整合Service层

创建spring整合service层的文件spring-service.xml,主要配置让spring去扫描service包的位置,配置impl中要注入的mapper信息,声明事务的提交方式,以及AOP事务的支持。

<?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业务类注入到spring-->
    <bean id="UserServiceImpl" class="com.swz.service.UserServiceImpl">
        <property name="userMapper" ref="userMapper"/>
    </bean>
    <!-- 3声明事务提交方式-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 4AOP事务支持-->
</beans>

注意:<context:component-scan base-package=“com.swz.service”/> 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean。

5.spring整合MVC层

  1. 创建spring整合mvc层的文件spring-mvc.xml,主要配置springMVC静态资源过滤,开启springMVC注解驱动

    <?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.注解驱动-->
        <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中,同一个spring文件中来,实现最终的整合。

注意:此步骤正确的方式是每配置完一个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">
    <import resource="spring-dao.xml"/>
    <import resource="spring-sevice.xml"/>
    <import resource="spring-mvc.xml"/>
</beans>

学习链接:
https://blog.csdn.net/qq_37534947/article/details/122598685
https://blog.csdn.net/qq_37534947/article/details/122569406
https://blog.csdn.net/qq_37534947/article/details/122628171
参考链接:
https://blog.csdn.net/m0_47637225/article/details/122478644

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。