SSM整合

133 阅读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">
    <!--DispatcherServlet-->
    <servlet>
        <servlet-name>servlet</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>servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--编码过滤器-->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>25</session-timeout>
    </session-config>
</web-app>
  • db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456
  • mybatis-config.xml
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="db.properties">
        <property name="username" value="admin"/>
    </properties>
<!--    <settings>-->
<!--        <setting name="logImpl" value="LOG4J"/>-->
<!--    </settings>-->
    <typeAliases>
        <package name="com.ssm.pojo"/>
    </typeAliases>
<!--    <environments default="development">-->
<!--        <environment id="development">-->
<!--            <transactionManager type="JDBC"/>-->
<!--            <dataSource type="POOLED">-->
<!--            <property name="driver" value="${jdbc.driver}"/>-->
<!--            <property name="url" value="${jdbc.url}"/>-->
<!--            <property name="username" value="${jdbc.username}"/>-->
<!--            <property name="password" value="${jdbc.password}"/>-->
<!--        </dataSource>-->
<!--        </environment>-->
<!--    </environments>-->
    <mappers>
        <mapper class="com.ssm.dao.BookMapper"/>
    </mappers>
</configuration>
  • spring-dao.xml(创建sqlSession)
<?xml version="1.0" encoding="GBK"?>
<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:db.properties"/>
    <!--2.连接池
    dbcp:半自动化操作﹐不能自动连接
    c3pe:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中!)
    druid :
    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}" />
    <!-- c3p日连接池的私有属性-->
    <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接口扫描包,动态的实现了Dao接口可以注入到Spring容器中!-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!--注入 sqLSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--要扫描的dao包-->
        <property name="basePackage" value="com.ssm.dao"/>
    </bean>

</beans>
  • spring-MVC.xml(配置SpringMVC)
<?xml version="1.0" encoding="GBK"?>
<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.扫描包: controller-->
    <context:component-scan base-package="com.ssm.controller"/>
    <!--4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>
  • spring-service(实例化对象,即注入bean,一般是业务对象)
<?xml version="1.0" encoding="GBK"?>
<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.ssm.service"/>
    <!--2.将我们的所有业务类,注入到Spring,可以通过配置,或者注解实现-->
    <bean id="bookServiceImpl" class="com.ssm.service.BookServiceImpl">
         <!--ref来自MapperScannerConfigurer-->
        <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>
  • applicationContext.xml(将多个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"
       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-mvc.xml"/>
    <import resource="spring-service.xml"/>
</beans>

项目目录结构

image.png

Note

  • service层其实在调用dao层,将dao中的对象封装到service