码迷,mamicode.com
首页 > 其他好文 > 详细

搭建ssm开发环境配置

时间:2017-06-29 12:38:14      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:mybatis   格式   密码   poi   null   gcc   err   cti   转发   

首先创建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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.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
       ">

 

一丶配置数据库账号和密码的jdbc.properties文件

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

 

 二丶编写applicationContext.xml文件

  

    <!--开启注解扫描器  注在spring某一个版本后可以不写这段代码-->
    <context:annotation-config></context:annotation-config>
    <!--扫描某一个包下的注解-->
    <context:component-scan base-package="cn.bdqn.tangcco">
        <!--在这里不扫springmvc Controller的注解-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--加载properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源链接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置mybatis的整合-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指向数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--给实体起别名-->
        <property name="typeAliasesPackage" value="cn.bdqn.tangcco.entity"></property>
         <!--指定Mapper文件在什么路径-->
        <property name="mapperLocations" value="classpath:cn/bdqn/tangcco/mapper/*.xml"></property>
        <!--引用page分页接口-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties" >
                        <value>
                        <!--offsetAsPageNum=true-->
                        <!--rowBoundsWithCount=true-->
                        <!--pageSizeZero=truereasonable=false-->
                        <!--reasonable=true-->
                        reasonable=true
                        supporyMethodsArguments=true
                        <!--params=pageNum=pageHelperStart;pageSize=pageHelperRows;-->
                        params=count=countSql
                        antoRuntimeDialect=true
                        <!--supportMethodsArguments=false-->
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <!--配置扫描器,将mybatis接口的实现加入到IOC容器中-->
    <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描所有Dao接口的-->
        <property name="basePackage" value="cn.bdqn.tangcco.*.dao"></property>
        <!--引用工厂-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!--事务管理控制事务源-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置事务控制-->
    <!--配置事务规则 txAdvices-->
    <tx:advice id="txAdvices" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <!--切入点表达式-->
        <!--第一个 * 代表返回值-->
        <!--第二个 * 代表所有包-->
        <!--第三个 * 代表所有类-->
        <!--第四个 * 代表所有方法-->
        <!-- 最后 (..) 代表所有的参数-->
        <aop:pointcut id="txPointCut" expression="execution(* cn.*.services.impl.*.*(..))"/>
        <!--增强事务,配置事务规则-->
        <aop:advisor advice-ref="txAdvices" pointcut-ref="txPointCut"/>
    </aop:config>


三丶接下来配置srpingmvc.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--扫描指定包下所有的Spring注解生效-->                     <!-- 禁用掉默认扫描全部-->
    <context:component-scan base-package="cn.bdqn.tangcco" use-default-filters="false">
        <!--只扫描指定注解-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
    </context:component-scan>
    <!--能支持一些SpringMVC更高级的功能,比如JSR303校验,快捷的的ajax请求  等等-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--将SpringMVC不能处理的请求交给Tomcat-->
    <mvc:default-servlet-handler/>
    <!--配置拦截器 可以有多个拦截器,但每个拦截器必须实现HandlerInterceptor接口-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"></mvc:mapping>
            <bean class="cn.bdqn.tangcco.command.filter.UserInterceptor">
                <!--配置类里的excludedUrls属性把不拦截的url配置进list里-->
                <property name="excludedUrls">
                    <list>
                        <value>/login.controller</value>
                    </list>
                </property>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>
    <!--视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!--后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

五丶编写登录拦截器

public class UserInterceptor implements HandlerInterceptor {
    //存放不需要拦截的url
    private List<String> excludedUrls;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求的url
        String url = request.getRequestURI();
        //循坏无需拦截的url
        for (String s:excludedUrls){
            //如果当前请求的相等于不拦截url则不拦截
            if (s.equals(url)) {
                return true;
            }
        }
        //获取session里的用户
        Tbuser tbuser = (Tbuser) request.getSession().getAttribute("user");
        //如果session没有用户则拦截并返回登录页面传递请登录信息
        if (tbuser==null) {
            request.setAttribute("msg","请登录!");
            request.getRequestDispatcher("/index.jsp").forward(request,response);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    public List<String> getExcludedUrls() {
        return excludedUrls;
    }

    public void setExcludedUrls(List<String> excludedUrls) {
        this.excludedUrls = excludedUrls;
    }
}

六丶编写web.xml文件配置

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <!--加载初始化上下文配置文件-->
  <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>
  <!--配置SpringMVC响应编码格式-->
  <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>
    <!--转发字符编号启动-->
    <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>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <!--配置REST风格的URL,将页面的普通的post请求转为指定的put,或delete-->
  <filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <!--配置SpringMVC处理servlet的请求-->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!--指定MVC文件地址-->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--当初始化时,初始化的顺序-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!--所有.controller结尾的URL都交给SpringMVC处理-->
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.controller</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

到此全部配置已完成,只需要编写自己所需代码即可

搭建ssm开发环境配置

标签:mybatis   格式   密码   poi   null   gcc   err   cti   转发   

原文地址:http://www.cnblogs.com/asxf/p/7093723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!