SSH框架学习总结及注意事项
标签(空格分隔): SSH框架 Spring hibernate sturts2 orm
在此输入正文
一. Hibernate主要是为了封装JDBC和建立ORM框架,使用注意事项:
- Session会话的获取三种方式
-
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory(); Session session=sessionFactory.openSession();//从hibernate配置文件中创建session
-
Session session=HibernateSessionFactory.getSession();//从SessionFactory类得到一个session
-
BeanFactory beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml"); SessionFactory sessionFactory=(SessionFactory) beanFactory.getBean("sessionFactory"); Session session=sessionFactory.openSession(); //通过Spring非注解式开发取得,不推荐使用
-
推荐使用:通过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>
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注入(常用),以下是三种注入方式例子: ```java 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("值注入模式");
}
}
```
在applicationContext.xml文件中加入bean
```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"
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>
```
调用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();
}
}
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>
构造函数注入 ```java //将接口回调改成通过构造函数 private IStudentService stuservice;
public StudentAction(IStudentService stuservice){
this.stuservice = stuservice;
}
```
在applicationContext.xml配置文件如下:
<constructor-arg name="stuservice" ref="stuservice"></constructor-arg>
构造注入和设值注入区别:
推荐使用设值注入,因为性能高
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包,重新导入