1、电力系统总结笔记一:项目第一天(项目框架)1:项目介绍参考:项目分析笔记和【文档】中的国家电力系统技术点汇总(简历使用).doc2:项目框架(SSH)第一步:创建数据库(格式:UTF-8)创建表:第二步:创建项目(格式:UTF-8)导入jar包(SSH)第三步:持久层(1)在cn.itcast.elec.domain中创建ElecText.javapublic class ElecText implements java.io.Serializable private String textID;/主键IDprivate String textName; /测试名称private Date t
2、extDate;/测试日期private String textRemark;/测试备注public String getTextID() return textID;public void setTextID(String textID) this.textID = textID;public String getTextName() return textName;public void setTextName(String textName) this.textName = textName;public Date getTextDate() return textDate;public
3、 void setTextDate(Date textDate) this.textDate = textDate;public String getTextRemark() return textRemark;public void setTextRemark(String textRemark) this.textRemark = textRemark;(2)在cn.itcast.elec.domain目录下,创建ElecText.java对应的映射文件(ElecText.hbm.xml)!DOCTYPE hibernate-mapping PUBLIC -/Hibernate/Hiber
4、nate Mapping DTD 3.0/EN (3)在src下创建hibernate.cfg.xml的配置文件!DOCTYPE hibernate-configuration PUBLIC-/Hibernate/Hibernate Configuration DTD 3.0/ENcom.mysql.jdbc.Driverjdbc:mysql:/localhost:3306/itcastElec?useUnicode=true&characterEncoding=utf8rootrootorg.hibernate.dialect.MySQL5Dialectupdatetrue(4)使用
5、junit在test包中进行测试(并且导入log4j的配置文件)public class TestHibernate /*测试保存*/Testpublic void save()Configuration configuration = new Configuration();configuration.configure();/加载类路径hibernate.cfg.xml和映射文件SessionFactory sf = configuration.buildSessionFactory();Session s = sf.openSession();Transaction tr = s.beg
6、inTransaction();/测试操作对象的过程,就是操作数据库表ElecText elecText = new ElecText();elecText.setTextName(测试Hibernate名称);elecText.setTextDate(new Date();elecText.setTextRemark(测试Hibernate备注);s.save(elecText);mit();s.close();第四步:DAO层(1)在cn.itcast.elec.dao中创建2个接口(公用接口和业务接口)l 公用接口:public interface ICommonDao void sav
7、e(T entity);l 业务接口(需要继承公共接口,并且指定泛型T所对应的对象:public interface IElecTextDao extends ICommonDao public static final String SERVICE_NAME = cn.itcast.elec.dao.impl.ElecTextDaoImpl;(2)在cn.itcast.elec.dao.impl中创建2个接口的实现类l 公用类(需要继承HibernateDaoSupport,这样可以方便使用HibernateTemplate对象):public class CommonDaoImpl ext
8、ends HibernateDaoSupport implements ICommonDao /*使用Resource注入SessionFactory*/Resource(name=sessionFactory)public final void setSessionFactoryDi(SessionFactory sessionFactory) this.setSessionFactory(sessionFactory);/*保存*/public void save(T entity) this.getHibernateTemplate().save(entity);l 业务类(需要继承公用
9、类,这样可以使用公用类中的定义的方法)Repository(IElecTextDao.SERVICE_NAME)public class ElecTextDaoImpl extends CommonDaoImpl implements IElecTextDao (3)在src创建spring的配置文件(beans.xml) classpath:hibernate.cfg.xml!- 也可以使用spring的配置文件的方式管理事务 -(4)使用junit完成测试public class TestDao /*测试保存*/Testpublic void save() /加载类路径下的spring容器
10、ApplicationContext ac = new ClassPathXmlApplicationContext(beans.xml); /调用接口IElecTextDao elecTextDao = (IElecTextDao) ac.getBean(IElecTextDao.SERVICE_NAME);/操作对象ElecText elecText = new ElecText();elecText.setTextName(测试Dao名称);elecText.setTextDate(new Date();elecText.setTextRemark(测试Dao备注);elecTextDa
11、o.save(elecText);注意:如果数据没有保存,需要设置事务自动提交:在hibernate.cfg.xml中添加:true第五步:Service层(1) 在cn.itcast.elec.service中创建接口:public interface IElecTextService public static final String SERVICE_NAME = cn.itcast.elec.service.impl.ElecTextServiceImpl;void saveElecText(ElecText elecText);(2) 在cn.itcast.elec.service.
12、impl中创建接口的实现类:Service(IElecTextService.SERVICE_NAME)Transactional(readOnly=true)public class ElecTextServiceImpl implements IElecTextService Resource(name=IElecTextDao.SERVICE_NAME)private IElecTextDao elecTextDao;Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=fa
13、lse)public void saveElecText(ElecText elecText) elecTextDao.save(elecText);(3) 使用junit测试public class TestService /*测试保存*/Testpublic void save()ApplicationContext ac = new ClassPathXmlApplicationContext(beans.xml);IElecTextService elecTextService = (IElecTextService) ac.getBean(IElecTextService.SERVI
14、CE_NAME);/操作对象ElecText elecText = new ElecText();elecText.setTextName(测试Service名称);elecText.setTextDate(new Date();elecText.setTextRemark(测试Service备注);elecTextService.saveElecText(elecText);由于spring提供的声明式事务处理,进行事务的控制,在业务层的类和方法上定义Transactional(),同时去掉hibernate.cfg.xml中的配置,由spring统一控制。去掉的代码是:true第六步:控制
15、层()(1)在cn.itcast.elec.web.action中创建Action(业务Action)类和BaseAction(公用Action)l Action类:(注意:这里要设置成多例,即Scope(value=prototype),因为struts2的Action是多实例,多线程)Controller(elecTextAction)Scope(value=prototype)public class ElecTextAction extends BaseActionElecText elecText = this.getModel();Resource(name=IElecTextSe
16、rvice.SERVICE_NAME)private IElecTextService elecTextService;/*执行保存*/public String save()/保存elecTextService.saveElecText(elecText);return save;l BaseAction类(封装模型驱动对象,HttpServletRequest和HttpServletResponse对象):public class BaseAction extends ActionSupport implements ModelDriven,ServletRequestAware,Serv
17、letResponseAware protected HttpServletRequest request;protected HttpServletResponse response;T entity;public BaseAction()/*泛型转换成真实类型(范类转换)*/Class entityClass = TUtils.getTClass(this.getClass();try entity = (T) entityClass.newInstance(); catch (Exception e) e.printStackTrace();public T getModel() ret
18、urn entity;public void setServletRequest(HttpServletRequest req) this.request = req;public void setServletResponse(HttpServletResponse res) this.response = res;(2)在cn.itcast.elec.util包下创建公用类(泛型转换)。泛型转换的目的子类传递真实对象类型,在父类中使用泛型转换成真实对象类型。以后util包下封装的就是公用类。public class TUtils /*泛型转换成真实类型(范类转换)*/public stat
19、ic Class getTClass(Class entity) ParameterizedType parameterizedType = (ParameterizedType) entity.getGenericSuperclass();Class entityClass = (Class) parameterizedType.getActualTypeArguments()0;return entityClass;(3)在src下创建struts2的配置文件struts.xml/system/textAdd.jsp(4)在web.xml中添加配置:web-app version=2.5
20、xmlns= xmlns:xsi=http:/www.w3.org/2001/XMLSchema-instance xsi:schemaLocation= struts2org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterstruts2/*contextConfigLocationclasspath:beans.xmlorg.springframework.web.context.ContextLoaderListenerindex.jsp(5)导入css,script,jsp,images进行测试使用sys
21、tem/textAdd.jsp进行测试:页面如图点击【保存】完成。测试保存ElecText对象。SSH整体架构图:3:底层方法封装(CommonDaoImpl类)public class CommonDaoImpl extends HibernateDaoSupport implements ICommonDao /*泛型转换,获取真实对象实体*/Class entityClass = TUtils.getTClass(this.getClass();/*使用Resource注入SessionFactory*/Resource(name=sessionFactory)public final
22、void setSessionFactoryDi(SessionFactory sessionFactory) this.setSessionFactory(sessionFactory);/*保存*/public void save(T entity) this.getHibernateTemplate().save(entity);/*更新*/public void update(T entity) this.getHibernateTemplate().update(entity);/*使用主键ID,查询对象*/public T findObjectByID(Serializable i
23、d) return (T) this.getHibernateTemplate().get(entityClass, id);/*使用主键ID,删除对象(删除一个或者多个对象)*/public void deleteObjectByIDs(Serializable. ids) if(ids!=null & ids.length0)for(Serializable id:ids)Object entity = this.findObjectByID(id);this.getHibernateTemplate().delete(entity);/*使用封装对象的集合,批量删除对象*/public
24、void deleteObjectByCollection(List list) this.getHibernateTemplate().deleteAll(list);/*测试指定查询条件,查询结果集(不分页)*/*这里1=1的目的是方便在Service层拼装sql或者hql语句,连接统一使用and * SELECT o FROM ElecText o WHERE 1=1 #Dao层填写AND o.textName LIKE %张% #Service拼装AND o.textRemark LIKE %张% #Service拼装ORDER BY o.textDate ASC,o.textName
25、 desc #Service拼装 */public List findCollectionByConditionNoPage(String condition,final Object params, Map orderby) String hql = SELECT o FROM +entityClass.getSimpleName()+ o WHERE 1=1;String orderByHql = this.initOrderByHql(orderby);final String finalHql = hql + condition + orderByHql;/执行hql语句/*方式一:直
26、接使用HibernateTemplate的find()方法,find方法支持执行hql语句*/List list = this.getHibernateTemplate().find(finalHql, params);/*方式二:获取SessionFactory,在获取Session*/SessionFactory sf = this.getHibernateTemplate().getSessionFactory();/Session s = sf.getCurrentSession();/Query query = s.createQuery(finalHql);/query.setPa
27、rameter(0, params0);/query.setParameter(1, params1);/List list = query.list();/*方式三:使用hibernateTemplate调用回调函数*/List list = this.getHibernateTemplate().execute(new HibernateCallback() public Object doInHibernate(Session session)throws HibernateException, SQLException Query query = session.createQuery(finalHql);if(params!=null & params.length0)for(int i=0;iparams.length;i+)query.setParameter(i, paramsi);return query.list(););return list;/*组织排序语句,将Map集合转换成String类型*/private String initOrderByHql(Map orderby) StringBuffer buffer = new Stri