2019独角兽企业重金招聘Python工程师标准>>>
spring 5.1.5版本(一)
spring 5.1.5版本(二)
spring 5.1.5版本(三)
spring 5.1.5版本(四)
spring 5.1.5版本(五)
demo地址:https://gitee.com/gwlCode/springDemo
导包
hibernate
hibernate必要包 lib/required
hibernate jpa包 lib/jpa-metamodel-generator
数据库驱动包
struts2
核心包 Essential Dependencies Only/struts-2.5.20-min-lib.zip/lib
struts2整合spring插件包 All Dependencies/struts-2.5.20-lib.zip/lib/struts2-spring-plugin-2.5.20.jar
spring
基本
整合web
整合aop
整合hibernate和事务
整合junit测试
日记包
c3p0
jstl
annotations-api.jar
整合spring到web
创建配置文件 applicationContext.xml
配置spring随项目启动 web.xml
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
启动Tomcat测试
整合struts2到web
配置struts2主配置文件 struts.xml
regex:.*
/success.jsp
配置struts2核心过滤器 web.xml
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
整合struts2到spring
配置常量 struts.xml
regex:.*
/success.jsp
applicationContext.xml
整合hibernate与spring
配置applicationContext.xml
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/mydatabase?characterEncoding=utf8
root
root123
org.hibernate.dialect.MySQL5Dialect
true
true
update
c3p0连接池
配置db.properties
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mydatabase?characterEncoding=utf8 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=root123
引入连接池到spring
将连接池注入给SessionFactory
org.hibernate.dialect.MySQL5Dialect
true
true
update
HibernateTemplate模板操作数据库
UserDaoImpl
package com.company.dao.impl;
import com.company.dao.UserDao;
import com.company.domain.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.util.List;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public User getByUserName(String name) {
//HQL
return getHibernateTemplate().execute(new HibernateCallback() {
@Override
public User doInHibernate(Session session) throws HibernateException {
String hql = "from User where name = ?0 ";
Query query = session.createQuery(hql);
query.setParameter(0, name);
User user = (User) query.uniqueResult();
return user;
}
});
/*
//Criteria
DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.add(Restrictions.eq("id",id));
List list = (List) getHibernateTemplate().findByCriteria(dc);
if (list!=null && list.size()>0) {
return list.get(0);
}
return null;
*/
}
}
spring中配置dao注入sessionFactory applicationContext.xml
aop事务
配置核心事务管理器 applicationContext.xml
xml配置aop事务
配置通知
配置将通知织入目标对象
注解配置aop事务
开启注解事务
@Transactional
package com.company.service.impl;
import com.company.dao.UserDao;
import com.company.domain.User;
import com.company.service.UserService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = true)
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Override
public User getUserByPassword(User user) {
return null;
}
@Override
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = false)
public void saveUser(User user) {
userDao.save(user);
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
扩大session作用范围
为了避免使用懒加载时出现no-session问题,需要扩大session的作用范围
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
openSessionInView
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
openSessionInView
/*
struts2
/*