搭建ssm框架:Spring SpringMVC Mybatis

创建Web project 文件,取名为test

在项目名上右键--》MyEclipse-->>Add Spring Aapabilities-->>勾选Spring AOP,Spring Core,Spring Web

引jar包(jar包不知道是否已经引全,运行项目时报错了,再引所需jar包)

commons-codec-1.9.jar:主要包括核心的算法。比如 MD5。SHA1。或者常规加密解密算法:BASE64。
commons-collections-3.2.1.jar: Apache Commons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大。apache的集合框架包(如:map等),比jdk自带的功能多一点。
commons-dbcp-1.2.2.jar:
commons-fileupload-1.3.1.jar:工具包的依赖包是commons-io-2.4.jar,应用和Servlet中容易的加入强大和高性能的文件上传能力
commons-io-2.4.jar:
commons-lang.jar:与Java.lang这个包的作用类似,Commons Lang这一组API也是提供一些基础的、通用的操作和处理,如自动生成toString()的结果、自动实现hashCode()和equals()方法、数组操作、枚举、日期和时间的处理等等。
commons-logging-1.1.1.jar: Apache Commons包中的一个,包含了日志功能,必须使用的jar包。通过commons-logging-1.1.1架构来实现log4j,sl4j,jdk的log等日志管理系统的接口。内部有一个Simple logger的简单实现,但是功能很弱。所以使用common-logging,通常都是配合着log4j来使用
asm-3.3.1.jar:ASM 是一个 Java 字节码操纵框架。它可以直接以二进制形式动态地生成 stub 类或其他代理类,或者在装载时动态地修改类。ASM 提供类似于 BCEL 和 SERP 之类的工具包的功能,但是被设计得更小巧、更快速,这使它适用于实时代码插装。http://blog.csdn.net/liyangbing315/article/details/5472862
commons-pool-1.3.jar:
jstl-1.2.jar:
javassist-3.17.1-GA.jar:javassist(JAVA编程助手)使Java字节码操纵简单。这是一个编辑Java字节码的类库。
log4j-1.2.17.jar:log4j日志包
mybatis-3.2.7.jar: mybatis核心包, 实现mybatis框架的工具包。
mybatis-spring-1.2.2.jar mybatis/spring包,集合spring与mybatis框架的工具包
mysql-connector-java-5.1.7-bin.jar:mysql数据库连接包
spring-aop-3.2.0.RELEASE.jar:Spring的面向切面编程,提供AOP(面向切面编程)实现
spring-aspects-3.2.0.RELEASE.jar:
spring-beans-3.2.0.RELEASE.jar:SpringIoC(依赖注入)的基础实现
spring-context-3.2.0.RELEASE.jar:Spring提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等.
spring-context-support-3.2.0.RELEASE.jar:
spring-core-3.2.0.RELEASE.jar:Spring3.2.0的核心工具包。
spring-expression-3.2.0.RELEASE.jar:Spring表达式语言.
spring-jdbc-3.2.0.RELEASE.jar:对JDBC的简单封装
spring-orm-3.2.0.RELEASE.jar:
spring-test-3.2.0.RELEASE.jar:对Junit等测试框架的简单封装
spring-tx-3.2.0.RELEASE.jar:是spring-context-3.2.0.RELEASE.jar依赖包.
spring-web-3.2.0.RELEASE.jar:
spring-webmvc-3.2.0.RELEASE.jar

建立config资源包,配置文件

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
    <!-- 加载Spring容器配置  spring的监听器  -->
    <!-- ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。
  因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 设置Spring容器加载所有的配置文件的路径   -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/applicationContext.xml</param-value>
    </context-param>

    <!-- 配置SpringMVC核心控制器 -->
    <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*:config/spring-mvc.xml</param-value>
        </init-param>
        <!-- 启动加载一次 -->  
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--为DispatcherServlet建立映射 -->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <!-- 此处可以可以配置成*.do -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- 解决工程编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</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>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

配置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:mvc="http://www.springframework.org/schema/mvc"
    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.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 
    <!-- 配置注解映射器和适配器 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp" />
    </bean>
    
    <context:component-scan base-package="com.controller,com.biz,com.dao"></context:component-scan>
</beans>

配置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>
    <!-- 类型别名 -->
    <typeAliases>
        <package name="com.bean"/><!-- 批量定义别名 -->
        <!-- <typeAlias type="com.bean.MemberBase" alias="MemberBase"/> --><!-- 单个定义别名 -->
    </typeAliases>
</configuration>

配置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:mvc="http://www.springframework.org/schema/mvc"
    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.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    
    <!-- 配置数据库   数据源 : DriverManagerDataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- Oracle数据库 -->
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:SSM" />
        <property name="username" value="root" />
        <property name="password" value="123" />
        <!-- mySQL数据库 -->
        <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/SSM" />
        <property name="username" value="root" />
        <property name="password" value="123" /> -->
    </bean>
    
    <!--mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源
        MyBatis定义数据源,同意加载配置
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加载mybatis的配置文件 -->
        <property name="configLocation" value="classpath:mybatis.xml" />
        <!-- 把mybatis.xml文件中的mapper里的bean文件————自动扫描 -->
        <property name="mapperLocations" value="classpath*:com/dao/**/*.xml"></property>
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 声明事物管理器
    事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 定义一个通知
    使用声明式事务 transaction-manager:引用上面定义的事务管理器-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="upd*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 将sessionFactory注入dao  
    mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
        basePackage:指定sql映射文件/接口所在的包(自动扫描)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    <!-- 配置事物切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.biz..*.*(..))" id="serviceMethod"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    </aop:config>
    <!-- 表示com.biz包下的所有方法为为事务管理。第一个*代表所有的返回值类型 , 第二个*代表所有的类,  第三个*代表类所有方法 最后一个..代表所有的参数。 -->
</beans>

配置log4j.properties
  
 log4j.logger.com.sm.dao=DEBUG,stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值