springmvc(三)ssm的整合

spring整合springMvC

在web.xml中通过spring提供的一个监听器,加载applicationContext.xml
在项目启动时候,加载web.xml;然后在web.xml中要加载applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

	<!--
	服务器创建web3大组件顺序:
			1.监听器
			2.过滤器
			3.Servlet
	-->

	<!--配置监听器加载解析applicationContext.xml配置文件创建IOC容器对象-->
	<!--	配置全局参数指向applicationContext.xml-->
	<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>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!--springmvc中央控制器-->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--加载springmvc.xml配置文件-->
		<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>
------------------------------------------------------------------------------

修改controller,注入service

----------------------------------------------------------------------------
加载顺序  (listener    >     filter    >    servlet)


spring整合Mybatis

配置:applicationContext.xml, 把SqlSessionFactory的创建,交给SpringIOC容器完成。
Spring整合Mybatis最核心的对象就是在IOC容器中创建SqlSessionFactory对象,有了这个对象就可以获取SqlSession对象进行持久层的操作

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>


    <!--给封装数据的实体类起别名-->
    <typeAliases>
        <package name="com.itheima.entity"/>
    </typeAliases>

</configuration>

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

    <!--1.IOC注解扫描业务层:com.itheima.service
        为什么不扫描别的包,因为一会会用springmvc扫描controller,职责清晰
    -->
    <context:component-scan base-package="com.itheima.service"></context:component-scan>

    <!--2.spirng整合Mybatis-->
    <!--2.1 加载外部的jdbc.properties配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--2.2 创建DruidDataSource对象加入IOC容器-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--2.3 创建spring整合mybatis的sqlSession的工厂类,接管mybatis的工厂-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
       <!-- <property name="typeAliasesPackage" value="com.itheima.entity"></property>  mybatis-config.xml中有,所以这里注释-->
    </bean>

    <!--2.4 扫描mybatis的映射接口与映射配置文件等
            将所有的Dao接口进行增强生成代理对象加入IOC容器,所以不需要使用@Repository
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"></property>
    </bean>

    <!--2.5 配置spring的声明式事务-->
    <!--2.5.1 配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--2.5.2 配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--查询方法不使用事务 propagation="SUPPORTS"
               增删改方法使用事务
            -->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="search*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*"  /> <!--默认propagation="REQUIRED" read-only="false"-->
        </tx:attributes>
    </tx:advice>
    <!--2.5.3 aop配置,将通知(声明式事务)给到切入点方法-->
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* com.itheima.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
    </aop:config>
</beans>

标签库

<%--jstl里面提供了格式化标签库,可以格式化日期
                    <fmt:formatDate> 日期格式化标签
                       value="${user.birthday}" 需要格式化的值
                       pattern="yyyy-MM-dd" 显示数据的格式设置
            --%>

注意:这个 <fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd"></fmt:formatDate>中间不能有空格
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值