• 整合ssm框架


    一、Dao层:整合mybatisspring

    需要的jar包:

    1、mybatisjar

    2、Mysql数据库驱动

    3、数据库连接池

    4、Mybatisspring的整合包。

    5、Springjar

    配置文件:

    1、mybatis的配置文件:SqlMapConfig.xml

    2、Spring的配置文件:applicationContext-dao.xml

    1、数据源

    2、数据库连接池

    3、配置SqlSessionFactorymybatisspring整合包中的)

    4、配置mapper文件的扫描器。

     

    二、Service层:

    使用的jar包:springjar包。

    配置文件:applicationContext-service.xml

    配置一个包扫描器,扫描所有带@Service注解的类。

    事务配置:

    配置文件:applicationContext-trans.xml

    1、配置一个事务管理器

    2、配置tx

    3、配置切面

     

    三、表现层

    使用springmvc,需要使用springmvcspringjar包。

    配置文件:springmvc.xml

    1、配置注解驱动

    2、配置一个视图解析器。

    3、包扫描器,@Controller注解

     

    Web.xml

    1、配置springmvc的前端控制器

    2、Spring容器初始化的listener

     

    1.1 框架整合dao

    1.1.1 SqlMapConfig.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>

    </configuration>

    1.1.2 applicationContext-dao.xml

    db.properties

    jdbc.driver=com.mysql.jdbc.Driver

    jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8

    jdbc.username=root

    jdbc.password=root

    applicationContext-dao.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    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-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 数据库连接池 -->

    <!-- 加载配置文件 -->

    <context:property-placeholder location="classpath:properties/*.properties" />

    <!-- 数据库连接池 -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

    destroy-method="close">

    <property name="url" value="${jdbc.url}" />

    <property name="username" value="${jdbc.username}" />

    <property name="password" value="${jdbc.password}" />

    <property name="driverClassName" value="${jdbc.driver}" />

    <property name="maxActive" value="10" />

    <property name="minIdle" value="5" />

    </bean>

    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

    <!-- 数据库连接池 -->

    <property name="dataSource" ref="dataSource" />

    <!-- 加载mybatis的全局配置文件 -->

    <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />

    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <property name="basePackage" value="com.xx.mapper" />

    </bean>

    </beans>

    Service层   

    applicationContext-service.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    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-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 包扫描器,扫描带@Service注解的类 -->

    <context:component-scan base-package="com.xx.service"></context:component-scan>

    </beans>

    applicationContext-trans.xm

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    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-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 事务管理器 -->

    <bean id="transactionManager"

    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    <!-- 数据源 -->

    <property name="dataSource" ref="dataSource" />

    </bean>

    <!-- 通知 -->

    <tx:advice id="txAdvice" transaction-manager="transactionManager">

    <tx:attributes>

    <!-- 传播行为 -->

    <tx:method name="save*" propagation="REQUIRED" />

    <tx:method name="insert*" propagation="REQUIRED" />

    <tx:method name="add*" propagation="REQUIRED" />

    <tx:method name="create*" propagation="REQUIRED" />

    <tx:method name="delete*" propagation="REQUIRED" />

    <tx:method name="update*" propagation="REQUIRED" />

    <tx:method name="find*" propagation="SUPPORTS" read-only="true" />

    <tx:method name="select*" propagation="SUPPORTS" read-only="true" />

    <tx:method name="get*" propagation="SUPPORTS" read-only="true" />

    </tx:attributes>

    </tx:advice>

    <!-- 切面 -->

    <aop:config>

    <aop:advisor advice-ref="txAdvice"

    pointcut="execution(* com.xx.service.*.*(..))" />

    </aop:config>

    </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: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.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.xsd">

        

        <!-- 配置包扫描器 -->    

        <context:component-scan base-package="com.xx.controller"></context:component-scan>

        <!-- 配置注解驱动 -->

        <mvc:annotation-driven/>

        <!-- 视图解析器 -->

        <bean

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="/WEB-INF/jsp/" />

    <property name="suffix" value=".jsp" />

    </bean>

    </beans>        

    1.1.1 Web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    id="xx" version="2.5">

    <display-name>xx-manager</display-name>

    <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

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

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

    </welcome-file-list>

    <!-- 初始化spring容器 -->

    <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:spring/applicationContext-*.xml</param-value>

    </context-param>

    <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <!-- 解决post乱码 -->

    <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>xx-manager</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->

    <init-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:spring/springmvc.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

    <servlet-name>xx-manager</servlet-name>

    <url-pattern>/</url-pattern>

    </servlet-mapping>

    </web-app>

     

  • 相关阅读:
    王艳 201771010127《面向对象程序设计(java)》第十七周学习总结
    王艳 201771010127《面向对象程序设计(java)》第十六周学习总结
    王艳 《面向对象程序设计》第十五周学习总结
    王艳 201771010127《面向对象程序设计(java)》第十四周学习总结
    王艳 201771010127《面向对象程序设计(java)》第十三周学习总结
    201771030117-祁甜 实验四 软件项目案例分析
    201771030117-祁甜 实验三 结对项目—《西北师范大学疫情防控信息系统》项目报告
    201771030117-祁甜 实验二 个人项目—《西北师范大学学生疫情上报系统》项目报告
    201771030117-祁甜 实验一 软件工程准备—<阅读《现代软件工程——构建之法》提出的三个问题>
    201771010115-刘志梅-实验四 软件项目案例分析
  • 原文地址:https://www.cnblogs.com/gangzi4321/p/11497341.html
Copyright © 2020-2023  润新知