ssm整合(Struts、spring、mybatis)

                                                                                     框架之struts2——spring——mybatis整合
    今天学了struts2、spring、hibernate的整合,心血来潮就对struts、spring、mybatis也进行了整合。其中的历程可谓是曲折,折磨了我好几天。决定把心得记录下来。话不多说。
1.首先列一下所需jar包
     1.1. springd的jar包
        aopalliance.jar,aspectjrt.jar,aspectjweaver.jar,spring-aop-3.1.1.RELEASE.jar(前面4个主要用于spring的切面编程,即spring事物用代理,dao接口层也用代理,用到切面编程),spring-beans-3.1.1.RELEASE.jar,spring-context-3.1.1.RELEASE.jar,spring-core-3.1.1.RELEASE.jar,spring-expression-3.1.1.RELEASE.jar(spring核心包,即必备包),spring-jdbc-3.1.1.RELEASE.ja,rspring-tx-3.1.1.RELEASE.jar(spring事物包),spring-web-3.2.5.RELEASE.jar
spring-asm-3.1.1.RELEASE.jar
     1.2 struts2的jar包
        commons-fileupload-1.2.2.jar,commons-io-2.0.1.jar,commons-lang3-3.1.jar,commons-logging-1.1.1.jar,
freemarker-2.3.19.jar,javassist-3.11.0.GA.jar,xwork-core-2.3.4.1.jar,struts2-core-2.3.4.1.jar(spring必备jar包)
struts2-spring-plugin-2.3.4.1.jar(spring与struts2整合的jar)
     1.3. mybatis的jar包
        mybatis-3.1.1.jar,mybatis-spring-1.3.1.jar(spring与mybatis整合所需jar)
     1.4. 数据库驱动包(mysql)
           mysql-connector-java-5.1.2-beta-bin.jar,c3p0-0.9.1.2.jar
     下载地址:http://download.csdn.net/detail/mstzhang/9855600
2.接下来就是配置文件
    2.1 web.xml配置
          <filter>
            <filter-name>struts</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
         </filter>
        <filter-mapping>
            <filter-name>struts</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
       </context-param>
       <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
    2.2. spring的配置文件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:p="http://www.springframework.org/schema/p"
                  xmlns:context="http://www.springframework.org/schema/context"
                  xsi:schemaLocation="
                  http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                  http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context.xsd">
                 <!--bean默认是单例singleton只要IOC容器中已经创建了bean则直接取,适合于service,工具类-->
                 <!--prototype多例每次都创建一个实例 ,适合于action  -->
                <!--配置扫描包  -->

                <context:component-scan base-package="com.zhangyisheng.*"></context:component-scan>
                <!--配置数据源,连接池  -->
               <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                   <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
                   <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test1"></property>
                   <property name="user" value="root"></property>
                   <property name="password" value="zhangyisheng"></property>
                   <property name="initialPoolSize" value="3"></property>
                   <property name="maxPoolSize" value="10"></property>
                   <property name="maxStatements" value="100"></property>
                   <property name="acquireIncrement" value="2"></property>
              </bean>
             <!--配置sqlSessionFactory工厂  -->
            <bean id="sqlSessisonFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <property name="dataSource" ref="dataSource"></property>
                 <!--spring与myatis完美整合用于扫描mapper映射文件-->
               <property name="mapperLocations" value="classpath:com/zhangyisheng/mapper/*.xml"></property>
           </bean>
        <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="basePackage" value="com.zhangyisheng.dao"></property>
           <property name="sqlSessionFactory" ref="sqlSessisonFactory"></property>
        </bean>
        <!--配置事物管理器  -->
        <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
       <!--开启事物-->
        <tx:annotation-driven transaction-manager="tx"/>
    </beans>
   2.3 struts配置文件
        struts.xml
       <?xml version="1.0" encoding="UTF-8" ?>
       <!DOCTYPE struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
       "http://struts.apache.org/dtds/struts-2.3.dtd">
       <struts>
           <include file="com/zhangyisheng/action/studentAction.xml"></include>
       </struts>

       studentAction.xml
       <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

       <struts>
            <package name="default" namespace="/" extends="struts-default">
               <!--自定义拦截器,用于拦截登录请求,进行权限判断-->
               <interceptors>
                    <interceptor name="loginInterceptor" class="com.zhangyisheng.interceptor.LoginInterceptor">
                    </interceptor>
                   <interceptor-stack name="myStack">
                          <interceptor-ref name="defaultStack"></interceptor-ref>
                          <interceptor-ref name="loginInterceptor"></interceptor-ref>
                   </interceptor-stack>
                <!--全局变量,错误处理页面-->
               </interceptors>
              <global-results>
                 <result name="error">/error.jsp</result>
              </global-results>
        
             <action name="student_*" class="studentAction" method="{1}">
                 <interceptor-ref name="myStack"></interceptor-ref>
                 <result name="list" type="redirectAction">student_listView</result>
                 <result name="login">/WEB-INF/views/login.jsp</result>
                 <result name="listView">/WEB-INF/views/list.jsp</result>
             </action>
          </package>
       </struts>
   2.4 映射文件配置
      Student.xml
       <?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="com.zhangyisheng.dao.StudentDao">
       <!--根据id查询学生  -->
           <select id="findById" parameterType="int" resultMap="studentfind">
                select * from student where id=#{id}
           </select>
           <!--查询全部  -->
           <select id="select" parameterType="com.zhangyisheng.entity.Student" resultMap="studentfind">
                select * from student
           </select>
          <resultMap id="studentfind" type="com.zhangyisheng.entity.Student">
              <result column="id" property="s_id"/>
              <result column="name" property="s_name"/>
              <result column="age" property="age"/>
              <result column="score" property="score"/>
         </resultMap>
         <!--添加  -->
         <insert id="add" parameterType="com.zhangyisheng.entity.Student">
              insert into student(name,age,score) values('${s_name}',#{age},#{score})
         </insert>
        <!--更新 -->
        <update id="update" parameterType="com.zhangyisheng.entity.Student">
             update student set name = '${s_name}',age= #{age},score = #{score}
        </update>
        <!--删除  -->
        <delete id="deleteById" parameterType="int">
            delete from student where id = #{id}
       </delete>
   </mapper>

    User.xml
     <?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="com.zhangyisheng.dao.UserDao">
         <!--根据id查询学生  -->
         <select id="findByUser" parameterType="com.zhangyisheng.entity.User" resultMap="findUser">
             select * from User where user_name='${user_name}' and password='${password}'
         </select>
        <resultMap id="findUser" type="com.zhangyisheng.entity.User">
            <result column="id" property="id"/>
            <result column="user_name" property="user_name"/>
            <result column="password" property="password"/>
        </resultMap>
</mapper>
3.接下来看看项目的目录结构
   
4.StudentDao接口
      //用于扫描接口,为接口生成代理类
     @MapperScan
    public interface StudentDao {
        //根据id查询
        public Student findById(int id);
    
        //查询全部
    
        public List<Student> select();
    
        //添加
        public void add(Student stu);
    
        //更新
        public void update(Student stu);
    
       //删除
       public void deleteById(int id);
 }

5.StudentService接口中的方法与StudentDao中的方法一致就不重复了
6.StudentServiceImpl类
     package com.zhangyisheng.serviceImpl;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.zhangyisheng.dao.StudentDao;
    import com.zhangyisheng.entity.Student;
    import com.zhangyisheng.service.StudentService;
    @Service("studentService")
    public class StudentServiceImpl implements StudentService{
        @Autowired
        private StudentDao studentDao;
        public void setStudentDao(StudentDao studentDao) {
            this.studentDao = studentDao;
         }

       @Override
       public Student findById(int id) {
          // TODO Auto-generated method stub
         return studentDao.findById(id);
      }

      @Override
       public List<Student> select() {
          // TODO Auto-generated method stub
           return studentDao.select();
     }

      @Override
      public void add(Student stu) {
         // TODO Auto-generated method stub
          studentDao.add(stu);
      }

      @Override
      public void update(Student stu) {
         // TODO Auto-generated method stub
         studentDao.update(stu);
     }

      @Override
      public void deleteById(int id) {
        // TODO Auto-generated method stub
        studentDao.deleteById(id);
      }

}
到此基本上结束了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值