三大框架核心知识
Hibernate:
(1)orm思想:对象关系映射
(2)hibernate核心配置文件:
如果只是单纯使用hibernate框架,核心配置文件名称hibernate.cfg.xml的位置在src下面,是固定的。
如果hibernate和spring整合的时候,hibernate核心配置文件的名称和位置没有固定的要求。
(3)hibernate映射配置文件:
实体类和数据库表映射关系—使用orm思想。
(4)hibernate操作步骤:
在spring框架对hibernate框架进行封装,使用hibernateTemplate。
Struts:
第一部分:action
(1)action的三种创建方式:
1:写一个普通类
2:写一个普通类,实现接口
3:继承ActionSupport类
(2)action配置文件
创建struts2.xml,名称和位置固定在src下面。
(3)配置action的方法:
1:使用通配符
2:method属性直接写方法名
(4)在action获取表单提交数据:
1:获取request对象(最原始方式)。
使用servletActionContext类获取
2:属性封装。
3:模型驱动。
4:表达式封装
(5)action中操作域对象:
使用ServletActionContext获取域对象。
(6)配置struts2过滤器(StrutsPrepareAndExecuteFilter)。
第二部分:值栈:
(1)向值栈中放数据:
1:set方法。
2:push方法。
3:定义变量,生成get方法。
(2)从值栈中获取数据:
1:在jsp中国使用struts2标签+ognl获取
2:<s:property>
3:<s:iterator>
第三部分:拦截器:
(1)aop和责任链模式。
(2)自定义拦截器。
1:继承MethodFilterInterceptor
2:重写类里面的方法。
3:配置拦截器和action关联。
Spring:
(1)spring核心配置文件:
名称和位置没有固定要求,在spring核心配置文件中引入schema约束。
(2)创建对象:
1:xml配置方式:<bean id=“” class=“” scope=“”/>
2:注解方式:四个注解。
(3)注入属性:
1:xml配置方式。
2:注解方式:
autoWired
resource
(4)使用ServletContext对象和监听器实现创建对象
1:在服务器启动时,加载spring配置文件,创建对象。
2:配置spring的监听器。
3:指定spring配置文件位置。
(5)jdbcTemplate
(6)spring事务配置:
1:xml方式。
2:注解方式。
SSH整合思想
struts和spring整合:把struts2的acting交给spring进行管理。
实现步骤:
1、导入jar包:
(0)导入spring基本的jar包
(1)导入struts2的jar包。
(2)导入spring整合struts2整合的jar包(struts2-spring-plugin.jar)。
(3)导入spring整合web项目的jar包(spring-web.jar):
2、创建action类,让它继承ActionSupport类。
public class UserAction extends ActionSupport {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public String execute() throws Exception {
System.out.println("action.............");
userService.add();
return NONE;
}
}
3、创建spring的配置文件(spring.xml ),引入相关约束,约束在前面文章中有一个最全约束,可以查找。
<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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
4、配置c3p0连接池和action对象。
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入属性值 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day04"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置action的对象 -->
<bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype">
<!-- 注入service -->
<property name="userService" ref="userService"></property>
</bean>
5、因为把action对象创建交给了spring,所有struts.xml中action中的class就不用谢action类的全路径,只需要写spring.xml中配置的action的id值就可以了。
<package name="demo1" extends="struts-default" namespace="/">
<!-- class属性里面不写action全路径了,因为写,action对象创建两次
写spring配置的action的bean的id值
-->
<action name="userAction" class="userAction"></action>
</package>
6、在web.xml中配置监听器,让服务器启动时就加载spring配置文件。
**<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean1.xml</param-value>
</context-param>**
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
**<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>**
spring整合Hibernate:把sessionFactory的创建交给spring进行管理
(1)导入spring整合hibernate的相关jar包以及hibernate的jar包。
(2)搭建hibernate环境,创建实体类,生成属性的get和set方法。
public class User {
private Integer uid;
private String username;
private String address;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
(3)配置实体类的映射关系。
<hibernate-mapping>
<!-- 1 配置类和表对应
class标签
name属性:实体类全路径
table属性:数据库表名称
-->
<class name="cn.itcast.entity.User" table="t_user">
<id name="uid" column="uid">
<generator class="native"></generator>
</id>
<property name="username" column="username"></property>
<property name="address" column="address"></property>
</class>
</hibernate-mapping>
(4)创建hibernate和spring核心配置文件,将hibernate核心配置文件数据库配置在spring中进行配置。
<hibernate-configuration>
<session-factory>
<!-- 第二部分: 配置hibernate信息 可选的-->
<!-- 输出底层sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 输出底层sql语句格式 -->
<property name="hibernate.format_sql">true</property>
<!-- hibernate帮创建表,需要配置之后
update: 如果已经有表,更新,如果没有,创建
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置数据库方言
在mysql里面实现分页 关键字 limit,只能使用mysql里面
在oracle数据库,实现分页rownum
让hibernate框架识别不同数据库的自己特有的语句
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 第三部分: 把映射文件放到核心配置文件中 必须的-->
<mapping resource="cn/itcast/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(5)把hibernate的sessionFactory交给spring镜像配置。
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入属性值 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day04"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- sessionFactory创建交给spring管理 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 因为在hibernate核心配置文件中,没有数据库配置,数据库配置在spring里面配置,注入dataSource -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定使用hibernate核心配置文件 -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
(6)配置web.xml,让它在服务器启动时加载spring配置文件,创建数据表。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean1.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>