SSH框架学习总结及注意事项(常宝)

最近在学习SSH框架,发现总会出现各种问题,进行以下记录,方便查找
一、 Hibernate主要是为了封装JDBC和建立ORM框架,使用注意事项:
    1、Session会话的获取三种方式
    1)SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
      Session session=sessionFactory.openSession();//从hibernate配置文件中创建session
   
    2)Session session=HibernateSessionFactory.getSession();//从SessionFactory类得到一个session
 
    3)BeanFactory beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml");
      SessionFactory sessionFactory=(SessionFactory) beanFactory.getBean("sessionFactory");
      Session session=sessionFactory.openSession();  //通过Spring非注解式开发取得,不推荐使用

    4)推荐使用:通过Spring注解注入式取得Session
      配置扫描包,然后进行注解注入式引用applicationContext.xml里的sessionFactory
      @Repository
      @Transactional
      public class CommonDAOImpl(){
           private SessionFactory factory;

        @Resource
        public void setFactory(SessionFactory factory) {
            this.factory = factory;
        }
    
        public Session getSession(){
            return factory.getCurrentSession();
        }

      }
 
    2、通过session实现增删改查
                //开启会话事务
        session.beginTransaction();
        //提交会话事务
        session.getTransaction().commit();
        //关闭会话
        session.close();
                
                session.save(emp);//增

                session.delete(Emp.id);//删

                session.update(Emp);//改

        Query query=session.createQuery("From Emp");   //查:Emp表示Emp.java文件的类,非数据库表名



二、Struts2 主要是为了取代Servlet,MVC思想
    1、struts.xml基本配置
           <struts>
        <package name="default" namespace="/" extends="struts-default">     <!-包名为default,URL命名空间/,继承默认struts-->
            <action name="hello" class="org.cb.hello">                  <!--name表示访问url路径, class表示映射的对应的类a-->
                <result name="success">/WEB-INF/jsp/index.jsp</result>  <!--当返回结果为success则跳转index.jsp页面-->
            </action>
        </package>
    </struts>  
    对应的hello.java文件中
    public class hello extends ActionSupport{
    
        @Override
        public String execute() throws Exception {
        
            System.out.println("hello类已访问");
            System.out.println(uname);
            return "success";  //返回结果,让struts.xml中的result调用
        }
        private String uname;//用来获取或设置表单的值
        public String getUname() {
            return uname;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
    
    }
    2、struts.xml扩展配置(推荐使用,spring整合struts)
    <struts>
         <!--    让Sturts通过Spring扫描包 -->
        <constant name="struts.objectFactory" value="spring" />
        <constant name="struts.objectFactory.spring.autoWire" value="name" />
        <!-- URL地址栏加*.do用此配置 -->
        <constant name="struts.action.extension" value="do" />
        <!-- struts标签取消tr,td标签 -->
        <constant name="struts.ui.theme" value="simple" />
        <package name="default" namespace="/" extends="struts-default">
            <action name="*_*" class="{1}Action" method="{2}">
                <result name="addpre">/WEB-INF/jsp/{1}/reg.jsp</result>
                <result name="add">/WEB-INF/jsp/success.jsp</result>
                <result name="input">/WEB-INF/jsp/error.jsp</result>
                <result name="login" type="redirect">/user/index.do</result>
                <result name="list">/WEB-INF/jsp/goodsinfo/list.jsp</result>
            </action>
        
        
        </package>
        <package name="user"  namespace="/user" extends="struts-default">
                <!-- 直接访问/user/login.jsp页面,不经过任何类和方法 -->
            <action name="login">
                <result>/WEB-INF/jsp/users/login.jsp</result>
            </action>
            <action name="index">
                <result>/WEB-INF/jsp/users/index.jsp</result>
            </action>
        </package>
    </struts>  

三、Spring框架:主要通过applicationContext.xml文件,在类中直接通过set注入方式实现对象的创建,不需要在类中去new对象,这样可以实现解耦并方便类的模块化管理,常

用来整合各种框架
    1、核心技术:IOC(控制反转)和面向切面编程(AOP)
       IOC(Inversion of Control):一个对象依赖其他对象通过被动的方式传递进来,而不是自己主动创建对象或查找依赖对象
       AOP(Aspect Oriented Programming):横切多个功能模块的关注点(业务逻辑),这些业务逻辑可能是:事务处理、日志管理、权限控制等,它们之间虽无关系,但各个功

能模块都要调用,将这些业务逻辑通过AOP框架进行模块化的管理
       AOP优点:减少重复代码,降低模块间耦合度,并有利于未来的可操作性和可维护性;缺点:依赖AOP框架
    2、三种注入方式:值注入,构造注入,set注入(常用),以下是三种注入方式例子:
    1)值注入  
    <bean id="stu" class="Student">
        <property name="name" value="常宝"></property>
    </bean>

    例子:
    ----------1)新建Student.java文件
     /*
     *2015年8月27日
     *实现功能:通过spring调用这个类
     */
    public class Student {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
        public void say(){
            System.out.println("值注入模式");
        }

    }  

    ----------2)在applicationContext.xml文件中加入bean
    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    <bean id="stu" class="Student">
        <property name="name" value="常宝"></property>    设置对应的类的属性的值

    </bean>

    </beans>

    ----------3)新建test.java文件调用spring测试
    /*
     *2015年8月27日
     *实现功能:测试Spring值注入方式
     */
    public class test {
        public static void main(String[] args) {
        
            BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
            Student stu=(Student) factory.getBean("stu");//调用xml文件中的bean id="stu"所对应的类
            stu.say();
        }
    }




    2)setter注入    <property name="name" ref="引用的beanid"></property>
    例子:新建DAO层、Service层、Aciton层   原用接口回调调用实现类,现改为spring调用
    将接口回调改成接口成员变量,并定义set属性,如下:
    //定义service接口成员set属性
           private IStudentService stuservice;  
           public void setStuservice(IStudentService stuservice) {
            this.stuservice = stuservice;
        }
    在applicationContext.xml配置文件如下:
    <!-- 调用StudentAction类-->
    <bean id="stuaction" class="org.action.StudentAction">
        <!--action引用service层 -->
        <!-- name属性为stuservice接口的成员变量  ref注入接口所引用的实现类对应的beanid   -->
        <property name="stuservice" ref="stuservice"></property>
    </bean>

    <!--service引有dao层  -->
    <bean id="stuservice" class="org.service.StudentServiceImpl">
        <property name="studao" ref="studao"></property>
    </bean>

    <!-- dao层 -->
    <bean id="studao"  class="org.dao.StudentDAOImpl"></bean>



    3)构造函数注入
    将接口回调改成通过构造函数
        private IStudentService stuservice;
       
        public StudentAction(IStudentService stuservice){
            this.stuservice = stuservice;
        }

    在applicationContext.xml配置文件如下:
    <constructor-arg  name="stuservice" ref="stuservice"></constructor-arg>
    构造注入和设值注入区别:
    推荐使用设值注入,因为性能高


-------------------------------------------注解开发-------------------------------------------------------
     *3、通过注解式开发优化set注入
     Spring四种常用注解标记可以通用,但推荐以下使用方式:
                       @Controller            [action 层使用]
                       @Service               [Service层使用]
                       @Repository            [Dao    层使用]
                       @Component             [通用]
    
1)配置扫描包提示
选择applicationContext.xml文件,选项卡选择namespaces,勾选context
2)扫描包如下:
<!-- 扫描包 -->
<context:component-scan base-package="org.action,org.service,org.dao"></context:component-scan>


3)对应的引用资源加入注解,如下:
***********************************************************************************
main函数调用action类
        //调用类名首字母改小写
        StudentAction stuaction=(StudentAction) factory.getBean("studentAction");

***********************************************************************************
action调用service层
package org.action;

import javax.annotation.Resource;

import org.iservice.IStudentService;
import org.springframework.stereotype.Repository;
import org.vo.Student;

/*
 *2015年8月27日
 *实现功能:调用service层
 */
//注册成bean类
@Controller
public class StudentAction {
    // 通过service接口回调service类
    // IStudentService stuservice=new StudentServiceImpl();
    // 定义service接口成员set属性
    private IStudentService stuservice;

    @Resource
    public void setStuservice(IStudentService stuservice) {
        this.stuservice = stuservice;
    }

    public String addstu() {
        Student s = new Student();
        stuservice.addstu(s);
        return "success";
    }
}

*****************************************************************************************
service层调用dao层
//注册bean类
@Service
public class StudentServiceImpl implements IStudentService {
    //IStudentDAO studao=new StudentDAOImpl();//通过接口回调调用StudentDAOImpl类
    
    //定义接口成员set属性,spring使用
    private IStudentDAO studao;    
    //引用的资源
    @Resource
    public void setStudao(IStudentDAO studao) {
        this.studao = studao;
    }



    @Override
    public void addstu(Student s) {        
        studao.addstu(s);//调用studao的方法    
    }

}
***************************************************************************************
dao层
@Repository
//@Component
public class StudentDAOImpl implements IStudentDAO {

    @Override
    public void addstu(Student s) {
        System.out.println("注解开发--->成功");
    }

}


四、SSH框架使用注意事项

1、Spring整合hibernate注意:   
                                <!--默认用myeclipse安装SSH后,没有驱动配置,需手工添加-->
                                  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
                           
                                <!-- 在控制台打印sql语句 -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 打印sql语句格式漂亮 -->
                <prop key="hibernate.format_sql">true</prop>
2、Struts常用配置:
              <!--    让Sturts通过Spring扫描包 -->
        <constant name="struts.objectFactory" value="spring" />
        <constant name="struts.objectFactory.spring.autoWire" value="name" />
        <!-- URL地址栏加*.do用此配置 -->
        <constant name="struts.action.extension" value="do" />
        <!-- struts标签取消tr,td标签 -->
        <constant name="struts.ui.theme" value="simple" />
3、HQL语句不能执行:因为strutes  antlr-2.7.2.jar和hibernate 的jar包冲突,删除该jar包,重新导入
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值