SSM随笔

Posted dch0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM随笔相关的知识,希望对你有一定的参考价值。

1.SSM框架的简单配置

1.1 引入jar

c3p0-0.9.1.2.jar    #连接池
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
jackson-annotations-2.9.8.jar   #json支持
jackson-core-2.9.8.jar          #json支持
jackson-databind-2.9.8.jar      #json支持
mybatis-3.4.1.jar
mybatis-spring-1.3.0.jar
mysql-connector-java-5.1.7-bin.jar  #mysql驱动
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

1.2 web.xml配置文件

1.2.1在工程的classpath下创建两个配置文件
#在src目录下创建
applicationContext.xml
springMvc.xml
1.2.2web.xml的配置
<!-- 配置spring -->
<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>

<!--POST过滤器 -->
<filter>
    <filter-name>encoding</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>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- springMVC -->
<servlet>
    <servlet-name>springDispatcherServlet</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>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
  • 注意:/ 和 /*的区别
其中/和/*的区别:
< url-pattern > / </ url-pattern >   会匹配到/login这样的路径型url,不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet类 。
< url-pattern > /* </ url-pattern > 会匹配*.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。

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

    <!-- 自动扫描包,排除springmvc中的Controller -->
    <context:component-scan base-package="com.pdsu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- property引入文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="user" value="$jdbc.user"></property>
        <property name="password" value="$jdbc.password"></property>
        <property name="jdbcUrl" value="$jdbc.jdbcUrl"></property>
        <property name="driverClass" value="$jdbc.driverClass"></property>
    </bean>
    
    <!-- 配置数据源事务管理器 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
     </bean>
     
     <!-- 开启基于注解的事物 -->
     <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!-- 配置mybatis -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis.xml"/>
    </bean> 
     
     <!-- 配置扫描Mapper包 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.pdsu.dao" />
    </bean>
    
</beans>

1.4 springMvc.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        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-4.0.xsd">

    <!-- 自动扫描包,只扫描Controller -->
    <context:component-scan base-package="com.pdsu" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/WEB-INF/views/"></property>
    </bean>
    
    <!-- json转换器 -->
    <bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </bean>
    
    <!-- 拦截静态资源 -->
    <mvc:default-servlet-handler/>
    
    <!-- 注解驱动 -->
    <mvc:annotation-driven/>
    
</beans>

1.5 mybatis.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>
    <settings>
        <!-- 开启mybatis的懒加载机制 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
        
        <!-- 转驼峰命名 -->
        
        <!-- 打印sql -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
</configuration>

1.6 jdbc.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring4
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

2. Mybatis相关配置

以上是关于SSM随笔的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb SSM 《精品毕设》个人博客系统 前台 + 后台(源码 + 论文)主要实现的登录注册主页博客随笔文章管理消息管理评论管理用户管理角色管理主题管理栏目管理等功能

JavaWeb SSM 《精品毕设》个人博客系统 前台 + 后台(完整源码+论文)主要实现的登录注册主页博客随笔文章管理消息管理评论管理用户管理角色管理主题管理栏目管理等功能

随随笔随笔新新

随笔标题

随笔测试

笔新新随随笔 新随