SSM整合

SSM整合

1.1 mybatis

核心配置文件(可省略)

  1. 别名配置 可以配置实体类的别名

  2. 设置配置 可以对mybatis进行相关设置

映射配置文件

  1. 常规映射配置文件

1.2 spring

配置文件

spring相关
  1. 加载数据库配置文件

  2. 配置数据源 dataSource

  3. 配置 SqlSession 工厂类 【可以指定 Mybatis 核心配置文件 和映射配置文件路径、指定别名】

  4. 开启 dao 层的mapper接口扫描【配置一个扫描的类】

mybatis整合相关
  1. 开启Ioc注解扫描,扫描除控制层外的类【需要配置扫描包】
  2. 开启Aop注解扫面
  3. 开启事务注解扫描
  4. 配置 spring 平台事务管理器【需要注入数据源】

1.3 springmvc

配置文件

  1. 启用注解扫描器, 扫描被控制层的类【配置扫描的包】
  2. 加载映射注解驱动
  3. 配置处理静态资源的驱动
  4. 配置拦截器
  5. 配置文件上传解析器
  6. 配置视图解析器
  7. 配置视图控制器 <mvc: view-controller>

1.4 其它配置文件

数据库连接配置文件

  1. 数据库连接的基本配置【驱动、url、用户名、密码】

web项目配置文件

  1. 配置首页【欢迎页面】
  2. 开启 Spring 【加载spring配置文件】
    配置加载Spring文件的监听器
  3. 开启SpringMVC 【配置DispatcherServlet】
  4. 配置SpringMVC 字符编码过滤器 【要在所有过滤器的最前面】
  5. 配置请求方式过滤器 【RESTful风格使用】

实例

web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <description>a simple student management system,created by SSM framework ~</description>

    <!-- 启动Spring -->
    <context-param>
        <!-- 启动Spring: 加载Spring核心配置 -->
        <param-name>contextConfigLocation</param-name>
        <!-- 注意: 使用classpath:path(防止异常:FileNotFoundException) -->
        <param-value>classpath:spring-conf/applicationContext.xml</param-value>
    </context-param>
    


    <!-- 配置Spring MVC编码过滤器 -->
    <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>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    
	<!-- 启动Spring: 配置加载Spring文件的监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- 启动Spring MVC -->
    <servlet>
        <!-- 配置Spring MVC的前端核心控制器 -->
        <servlet-name>spring_mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 加载Spring MVC配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-conf/springmvc-config.xml</param-value>
        </init-param>
        <!-- 服务器启动后立即加载Spring MVC配置文件 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring_mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    

</web-app>

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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">

    <description>Spring configuration file</description>

    <!-- 读取c3p0.properties中的数据库配置信息 -->
    <context:property-placeholder location="classpath:database-conf/c3p0.properties"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 数据库驱动 -->
        <property name="driverClass" value="${c3p0.driverClass}"/>
        <!-- 连接数据库的url -->
        <property name="jdbcUrl" value="${c3p0.jdbcUrl}"/>
        <!-- 连接数据库的用户名 -->
        <property name="user" value="${c3p0.user}"/>
        <!-- 连接数据库的密码 -->
        <property name="password" value="${c3p0.password}"/>
        <!-- 初始化连接数 -->
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
        <!-- 最大连接数 -->
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
        <!-- 最小连接数 -->
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
        <!-- 连接的生存时间 -->
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
    </bean>

    <!-- 配置Spring事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 原理:控制数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 开启事务注解扫描 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- MyBatis与Spring整合 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 指定Mapper映射文件位置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <!-- 指定MyBatis核心配置文件位置 -->
        <property name="configLocation" value="classpath:/mybatis-conf/mybatis-config.xml"/>
        <!-- 引入插件 -->
        <property name="plugins">
            <array>
                <!-- 引入MyBaits分页插件 -->
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!-- 指定数据库类型 -->
                        <value>helperDialct=mysql</value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!-- 开启Mapper接口扫描器: 扫描Dao层 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="pers.sms.dao"/>
    </bean>

    <!-- 开启Spring IOC注解扫描器: 扫描Servie层-->
    <context:component-scan base-package="pers.sms.service"/>

    <!-- 开启Spring AOP注解扫描器 -->
    <aop:aspectj-autoproxy  />

</beans>

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: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/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <description>Spring-MVC configuration file</description>

    <!-- 启用注解扫描器: 扫描被@Controller注解的类 -->
    <context:component-scan base-package="pers.sms.controller"/>

    <!-- 加载注解驱动 -->
    <mvc:annotation-driven/>

    <!-- 处理静态资源 -->
    <mvc:default-servlet-handler/>

    <!-- 配置拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/static/h-ui/**"/>
            <mvc:exclude-mapping path="/static/easyui/**"/>
            <mvc:exclude-mapping path="/system/login"/>
            <mvc:exclude-mapping path="/system/getVerifiCodeImage"/>
            <bean class="pers.sms.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

    <!-- 配置文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 指定请求编码格式 -->
        <property name="defaultEncoding" value="UTF-8"/>
        <!-- 指定指定允许上传文件的最大值(20MB) -->
        <property name="maxUploadSize" value="20971520"/>
    </bean>

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

mybatis-conf.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>
    <!-- 为sql映射文件中的输入/输出参数设置类型别名 -->
    <typeAliases>
        <package name="pers.sms.bean"/>
    </typeAliases>
    
<!--    进行mybatis相关设置-->
    <settings>
<!--     配置日志 -->
        <!--        <setting name="logImpl" value="log4j"/>-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    
</configuration>

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="pers.sms.dao.AdminMapper">


    <!-- 根据id查询指定管理员信息 -->
    <select id="findByName" parameterType="String" resultType="pers.sms.bean.Admin">
        SELECT id,
               name,
               gender,
               password,
               email,
               telephone,
               address,
               portrait_path
        FROM tb_admin
        WHERE name = #{name}
    </select>

    <!-- 根据姓名模糊查询指定/所有管理员信息 列表 -->
    <select id="selectList" parameterType="admin" resultType="pers.sms.bean.Admin">
        SELECT id, name, gender, password, email, telephone, address, portrait_path
        FROM tb_admin
        <where>
            <!-- name为Admin中的属性名(在Controller层中已将查询的姓名封装到了Admin中的name属性中) -->
            <if test="name!=null and name!=''">
                AND name LIKE concat(concat('%',#{name}),'%')
            </if>
        </where>
    </select>

    <!-- 根据id更新指定管理员信息 -->
    <update id="update" parameterType="admin">
        UPDATE tb_admin
        <set>
            <if test="name!=null and name!=''">name=#{name},</if>
            <if test="gender!=null and gender!=''">gender=#{gender},</if>
            <if test="email!=null and email!=''">email=#{email},</if>
            <if test="telephone!=null and telephone!=''">telephone=#{telephone},</if>
            <if test="address!=null and address!=''">address=#{address},</if>
            <if test="portrait_path!=null and portrait_path!=''">portrait_path=#{portrait_path},</if>
        </set>
        WHERE id = #{id}
    </update>


    <!-- 根据id批量删除管理员信息 -->
    <delete id="deleteById">
        DELETE FROM ssm_sms.tb_admin WHERE id IN
        <foreach collection="array" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </delete>

</mapper>

jdbc.properties

# 数据库连接参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名
jdbc.username=登录名
jdbc.password=密码

# 数据库连接池参数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值