java中的接口是类吗
289
2023-05-03
详解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
一、整合原理
二、导包(41个)
1.hibernate
(1)hibernate/lib/required
(2)hibernate/lib/jpa | java persist api java的持久化规范(接口)
(3)数据库驱动
2.struts2
(1)struts-blank.war/WEB-INF/lib/*
注意:javassist-3.18.1-GA.jar包与hibernate中的重复(只保留高版本即可)
(2)struts整合spring插件包
注意:这个包一旦导入,那么struts2在启动时就会寻找spring容器.找不到将会抛出异常
3.spring
(1)基本:4+2
core | beans | context | expression | logging | log4j
(2)整合web:web包
spring-web
(3)整合aop:4个
spring-aop | spring-aspect | aop联盟 | aopweaving
(4)整合Hibernate和事务:4个
spring-jdbc | spring-tx | c3p0 | spring-orm
(5)整合junit4测试:test包
spring-test
4.标签库
standard.jar | jstl-1.2.jar
三、单独配置spring容器
1.创建applicationContext.xml,并导入约束(4个) beans | context | aop | tx
xmlns="http://springframework.org/schema/beans" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.2.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.2.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.2.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.2.xsd ">
xmlns="http://springframework.org/schema/beans"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.2.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.2.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.2.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.2.xsd ">
2.配置spring随项目启动(web.xml)
四、单独配置struts2
1.配置struts2主配置文件(struts.xml)
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
2.配置struts2核心过滤器到web.xml
五、struts2与spring整合
1.导包(已经导入)
struts2-spring-plugin-2.3.24.jar
2.配置常量
查看默认配置文件从31行开始找到要配置的变量。
### if specified, the default object factory can be overridden here
### Note: short-hand notation is supported in some cases, such as "spring"
### Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here
# struts.objectFactory = spring
### specifies the autoWiring logic when using the SpringObjectFactory.
### valid values are: name, type, auto, and constructor (name is the default)
struts.objectFactory.spring.autoWire = name
添加常量到struts.xml
3.整合方案1:struts2自己创建action,spring负责组装依赖属性(了解)
不推荐理由:最好由spring完整管理action的生命周期.spring中功能才应用到Action上.
4.整合方案2:spring负责创建action以及组装.(推荐)
applicationContext.xml:
struts.xml:
六、单独配置hibernate
1.导入实体类&orm元数据
举例:User.java
package cn.xyp.web.domain;
import java.util.HashSet;
import java.util.Set;
public class User {
private Long user_id;
private String user_code;
private String user_name;
private String user_password;
private Character user_state;
public Long getUser_id() {
return user_id;
}
public void setUser_id(Long user_id) {
this.user_id = user_id;
}
public String getUser_code() {
return user_code;
}
public void setUser_code(String user_code) {
this.user_code = user_code;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
public Character getUser_state() {
return user_state;
}
public void setUser_state(Character user_state) {
this.user_state = user_state;
}
@Override
public String toString() {
return "User [user_id=" + user_id + ", user_code=" + user_code + ", user_name=" + user_name + ", user_password="
+ user_password + "]";
}
}
User.hbm.xml:
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
2.配置主配置文件(hibernate.xml)
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
七、spring整合hibernate
1.整合原理
将sessionFactory对象交给spring容器管理
2.在spring中配置sessionFactory
(1)配置方案一:(了解)
(2)配置方案二:(推荐)
八、spring整合c3p0连接池
1.配置db.properties
jdbc.jdbcUrl=jdbc:mysql:///xyp_crm
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
2.引入连接池到spring中
3.将连接池注入给SessionFactory
九、spring整合hibernate环境操作数据库
1.Dao类创建:继承HibernateDaoSupport
注意:项目中要确保使用统一版本。
//HibernateDaoSupport 为dao注入sessionFactory
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
2.hibernate模板的操作
(1)execute
@Override
public User getByUserCode(final String usercode) {
//HQL
return getHibernateTemplate().execute(new HibernateCallback
@Override
public User doInHibernate(Session session) throws HibernateException {
String hql = "from User where user_code = ? ";
Query query = session.createQuery(hql);
query.setParameter(0, usercode);
User user = (User) query.uniqueResult();
return user;
}
});
(2)findByCriteria
//Criteria
DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.add(Restrictions.eq("user_code", usercode));
List
if(list != null && list.size()>0){
return list.get(0);
}else{
return null;
}
3.spring中配置dao
十、spring的aop事务
1.准备工作
2.xml配置aop事务
(1)配置通知
(2)配置织入
3.注解配置aop事务
(1)开启注解事务
(2)Service类中使用注解
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class UserServiceImpl implements UserService{
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void saveUser(User u) {
ud.save(u);
}
十一、扩大session作用范围
1.配置filter
为了避免使用懒加载时出现no-session问题.需要扩大session的作用范围。
十二、练习:用户登录
1.struts.xml核心配置
2.Action代码
public class UserAction extends ActionSupport implements ModelDriven
private User user = new User();
private UserService userService ;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String login() throws Exception {
//1 调用Service执行登陆逻辑
User u = userService.getUserByCodePassword(user);
//2 将返回的User对象放入session域
ActionContext.getContext().getSession().put("user", u);
//3 重定向到项目首页
return "toHome";
}
@Override
public User getModel() {
return user;
}
}
2.Service核心代码
public User getUserByCodePassword(User u) {
// 1 根据登陆名称查询登陆用户
User existU = ud.getByUserCode(u.getUser_code());
// 2 判断用户是否存在.不存在=>抛出异常,提示用户名不存在
if (existU == null) {
throw new RuntimeException("用户名不存在!");
}
// 3 判断用户密码是否正确=>不正确=>抛出异常,提示密码错误
if (!existU.getUser_password().equals(u.getUser_password())) {
throw new RuntimeException("密码错误!");
}
// 4 返回查询到的用户对象
return existU;
}
3.Dao核心代码
public User getByUserCode(final String usercode) {
//Criteria
DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.add(Restrictions.eq("user_code", usercode));
List
if(list != null && list.size()>0){
return list.get(0);
}else{
return null;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~