SSM框架整合 有图


1. 导包

 


 

2. web.xml文件中配置springspring的配置信息

<!--配置spring mvc的配置信息-->

<!--因为spring mvc 是基于servlet的所以要配置-->
    <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:SpringMVC.xml</param-value><!--文件位置路径-->
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <!--
           /* 能拦截所有的资源 包括jsp html js css
           *.action *.do 后缀名配置(可以用)
           /  指定目录拦截  会放行静态资源(可以使用)
        -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <!--spring IOC 容器 配置-->
    <listener><!--配置spring监听-->
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param><!--环境配置的参数-->
        <param-name>contextConfigLocation</param-name><!--环境配置的位置-->
        <param-value>classpath:applicationContext.xml</param-value><!--文件位置路径-->
    </context-param>

 

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

<--以上是spring的约束,下面才是配置信息-->

    <!--扫描配置文件-->
    <context:property-placeholder location="classpath:db.properties">

</context:property-placeholder>

<--下面这个是扫描文件中的信息 通过 键值名= 键值 的方式书写  -->

jdbc.driver =com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql://localhost:3306/mybatis01
jdbc.username=root
jdbc.password=root

<-- 以上信息 如果有扫描引用 可通过表达式获得对应的值-->


    <!--配置c3p0数据库连接池-->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<--以下四条都是通过ognl表达式拿到上面扫描文件中的值,即连接数据库的信息-->
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--获得sqlSessionFactory工厂 得到sqlSessionFactory-->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
    </bean>
    <!--扫描映射接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.hd.mapper"></property>
    </bean>
    <!--扫描指定包中的注解-->
    <context:component-scan base-package="cn.hd"></context:component-scan>
    <!--配置事务  事务需要数据库连接池的支持  所有要配置-->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>

<--配置要在哪些方法加入事务操作 add*表示以add开头的方法都可加上操作-->
            <tx:method name="add*" read-only="false" propagation="REQUIRED" isolation="DEFAULT"/>

<-- 设置只读 默认的是不开启的 查询操作可开启 不需事务-->
            <tx:method name="delete*" read-only="false" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="update*" read-only="false" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="get*" read-only="true" propagation="REQUIRED" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <!--将通知织入切入点-->
    <aop:config>

<-- 配置在执行哪些方法前执行事务操作-->
        <aop:pointcut id="txPt" expression="execution(* cn.hd.service.impl.*ServiceImpl.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPt"></aop:advisor>
    </aop:config>
</beans>

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><!--扫描指定包 返回值类型可以是该包下的类-->
            <package name="cn.hd.entity"></package>
            <package name="cn.hd.queryVo"></package>
        </typeAliases>

<-- 配置环境 可配置多个-->
        <environments default="development">
            <environment id="development">

                <transactionManager type="JDBC"></transactionManager>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"></property>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis01"></property>
                    <property name="username" value="root"></property>
                    <property name="password" value="root"></property>
                </dataSource>
            </environment>
        </environments>

<!-- 配置映射文件 -->
        <mappers>

<!-- 配置映射文件的路径 也可扫描指定包的所有映射文件-->
            <mapper resource="cn/hd/mapper/UserMapper.xml"></mapper>
        </mappers>
</configuration>

 

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:p="http://www.springframework.org/schema/p"
       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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <-- 扫描指定包下的注解-->
    <context:component-scan base-package="cn.hd.controller"></context:component-scan>
        <!--配置json的转换器-->
        <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
            </mvc:message-converters>
        </mvc:annotation-driven>
</beans>

 项目的结构目录

 

   

 

测试:

 

@Controller
public class UserController {
    @Resource(name = "userServiceImpl") 
    private UserService userService;
    @RequestMapping(value = "login.action")
    public ModelAndView login(){
        User user = userService.getUserById(10);
        System.out.println(user);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/index.jsp");
        return mav;
    }
}

 

 

 


 

 

猜你喜欢

转载自blog.csdn.net/zyz0225/article/details/81055724
今日推荐