关于ssm框架的全部整合(一) 2021.05.09

······今天整合了关于ssm框架的mybatis还有spring还有springmvc,关于spring因为开了弹幕,一直满屏用注解用注解自己分心了,所以第一遍的理解不太透侧,但是还好啃透了。
······关于mybatis就只用写一个包扫描,创建好跟数据库对应的类,写好数据库配置文件就OK了,对了切记把dao层的接口和实现类也要写好,这是mybatis的任务哦.
在这里插入图片描述

<?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>
    <typeAliases>
        <!--cn.com.zzn.pojo里面的所有类可以省类写包名,自动加前缀-->
        <package name="cn.com.zzn.pojo" />
    </typeAliases>

    <mappers>
        <mapper class="cn.com.zzn.dao.BookMapper"/>
    </mappers>
</configuration>

········第二步就是关于spring了,spring配置了两个xml文件来管理代码,首先第一个文件是spring-dao.xml来管理dao层的,重点来了:关于spring整合dao层的总逻辑就是 第一步先把数据库配置文件关联起来(database.propertiesdatabase.properties), 第二步连接数据库连接池,第三步创建sqlSessionFactory记住创建这个之后要把dataSource引用进来哦,还有注入mybatis,这个有点绕具体看以下代码,第四步就是配置dao要扫描的包,让它自动去注入 end

<?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">

<!--spring整合dao层总的逻辑:连接数据库池 创建sqlSession  让他自动去配置-->



<!--1.关联数据库配置文件   以前是通过mybatis来读,现在通过spring来读了就是靠这句代码-->
<context:property-placeholder location="classpath:database.properties"/>

<!--2.连接池
    dbcp:半自动化操作,不能自动连接
    c3p0:自动化的操作(自动化的加载配置文件,并且可以自动设置到对象中!)
    druid:hikari
    hikari:
-->
<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"/><!--引用上面的dataSource-->
        <!-- 绑定Mybatis的配置文件 这样spring和mybatis就整合起来了-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!--4.配置dao接口扫描的包,动态的实现了Dao接口可以注入Spring容器中!写了这一步操作之前写的实现类再也不用写了,spring通过反射帮你去做了-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入 sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 要扫描的dao包-->
        <property name="basePackage" value="cn.com.zzn.dao"/>
    </bean>
</beans>

spring还要配置一个.xml文件去管理service层,spring-service.xml去管理,简单理解,但是这句话自己纠结了半个小时还是啃透了

<bean id="BookServiceImpl" class="cn.com.zzn.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/><!--将bookMapper对象注入可以调用其任意方法-->
    </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"
       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">

<!--spring整合spring-service层-->


<!--    <import resource="classpath:spring-dao.xml"/>-->
<!--1.扫描service的包的注解-->
    <context:component-scan base-package="cn.com.zzn.service"/>
<!--2.将我们的所有业务类,注入到spring,可以通过自己配置或者注解  建议自己以后用注解方便一点-->
    <bean id="BookServiceImpl" class="cn.com.zzn.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>
<!-- 3.声明式事务配置  让增删改自动提交-->
    <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
<!--4.aop横切事务支持-->

</beans>

最后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:context="http://www.springframework.org/schema/context"
       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/mvc
       http://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.扫描包:controller-->
    <context:component-scan base-package="cn.com.zzn.controller"/>
        <!--4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

然后就是把这些全部导入applicationContext.xml来统一实现了,因为在web.xml配置的是applicationContext.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="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>

</beans>

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">
<!--DispatchServlet  所有的请求都被这个核心分发器接管了-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 乱码过滤-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Session-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>

今天结束!加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值