zoukankan      html  css  js  c++  java
  • ssm配置一一道来

    resource下配置文件
    Mybatis------Spring------SpringMVC
    1:创建mybatis包

    1.1创建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>

        <setting>

        <setting name="mapUnderscoreToCamelCase" value="true"/>      

        <settting>

    <configuration>  

    ----------------------------------------------------------------------------

    1.2创建mappers包(可以有各对应mapper下的类,此处只创建一个)

    ----------------------------------------------------------------------------

    <?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="cn.xlh.travel.mapper.UserMapper">
        -----各类sql语句-----
      <select id="queryUserByUserName" resultType="User">
    select * from tab_user where username=#{username}
    </select>
    </mapper>
    ------------------------------------------------------------

    2:创建spring包
    2.1:创建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
    http://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
    http://www.springframework.org/schema/aop/spring-aop.xsd">
    //开启注解扫描
    <context:component-scan base-package=
    "cn.xlh.travael.service">
    </context:component-scan>

    //加载外部资源配置文件
    <bean id="dataSource" class="
    com.alibaba.druid.pool.DruidDataSource
    ">
      <property name="driverClassName"
    value="com.mysql.jdbc.Driver"></property>
      <property name="url" value="jdbc:mysql:
    //127.0.0.1:3306/travel11"></property>
    <property name="username" value="root"></property>
    <property name="password" value=""></property>
    </bean>
    </beans>
    --------------------------------------------------------
    2.2:创建spring连接mybatis的配置文件applicationContext-mybatis.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/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
    //装配SqlSessionFactory(会话工厂)
    <bean id="sqlSessionFactory"
    class="org.mybatis.spring.SqlSessionFactoryBean"
    >
    <property name="configLocation" value="classpath:
    mybatis/mybatis-config.xml"></property>
    <property name="dataSourcee" ref="dataSource"></property>
    <property name="typeAliasesPackage" value="
    cn.xlh.travel.pojo"></property>
    <property name="mapperLocations" value="
    classpath:mybatis/mappers/**/*.xml"></property>
    </bean>

    //装配mappper
    <bean class="
    org.mybatis.spring.mapper.MapperScannerConfigurer
    ">
    </property name="basePackage" value="
    cn.xlh.travel.mspper"></property>

    </beans>
    -----------------------------------------------------------------------------------
    2.3:装配全局的事务配置文件applicationContext-tx.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/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
    //xml配置事务的步骤
    //1.装配事务管理器
    //2.配置事务策略
    //3.配置AOP
    <bean id="transactionManager" class="
    org.springframework.jdbc.datasource.DataSourceTransactionManager
    "></bean>

    <tx:advice id="txAdvice">
      <tx:attributes>
    <tx:method name="*/">

    </tx:attributes>
    </tx:advice>

    <aop:config>
    <aop:pointcut id="pt1"
    expression="execution(* cn.itcast.travel.service.impl.*.*(..))"
    ></aop:poincut>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1">
    </aop:advisor>

    </aop:config>
    </beans>
    ------------------------------------------------------------------------------------
    2.4:配置springMVC-servlet.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: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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    //开启注解扫描
    <context:component-scan base-package="
    cn.xlh.travel.web"></context:component-scan>

    //配置注解驱动
    <mvc:annotation-driven>
    <mvc:message-converters>
    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
    <property name="defaultCharset" value="utf-8"></property>
    </bean>
    </mvc:message-converters>
    </mvc:annotation-driven>

    //放行静态资源
    <mvc:default-servlet-handler>
    </mvc:default-servlet-handler>

    </beans>
    ---------------------------------------------------
    3:main-webapp-WEB-INF-web.xml
    ---------------------------------------------------
    //---1Spring的监听器
    <listener>
      <listener-class>
    org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-parm>
    <parm-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext*.xml</param-value>
    </context-param>

    //---2配置前端控制器
    <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:spring/springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>


    //拦截
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--拦截所有请求,jsp除外-->
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    

    </web-app>
    -------------------------------------
     









  • 相关阅读:
    论.Net 2.0消失的虚拟内存空间
    检查IE版本的方法
    升级.Net 4.0遇到的两个问题
    关于ID生成算法
    探秘.Net 4.0的StringBuilder实现
    关于填充DataTable的效率问题
    [已解决]请教:时间格式“2008109公元 10:43:27,AM”,这种时间格式是该怎么恢复默认设置
    请教:c#中的窗体怎么才能像c++的那窗体一样按键后可以调出输入法?
    在access中执行SQL,SQL中包含IIF,取出来的结果集字符串被截断了,请教各位大侠,这个是为什么呀?谢谢!
    请教:PL/SQL中执行to_date(to_char(a.lastupdatedate, 'YYYYMMDD HH24:MI:SS'),'YYYYMMDD HH24:MI:SS') ,上午12点整为什么格式化出来没有时分秒?
  • 原文地址:https://www.cnblogs.com/xlhlx/p/10665499.html
Copyright © 2011-2022 走看看