MyEclipse10+Struts2.1+Spring3.3+Hibernate3.3全注解框架搭建.docx_第1页
MyEclipse10+Struts2.1+Spring3.3+Hibernate3.3全注解框架搭建.docx_第2页
MyEclipse10+Struts2.1+Spring3.3+Hibernate3.3全注解框架搭建.docx_第3页
MyEclipse10+Struts2.1+Spring3.3+Hibernate3.3全注解框架搭建.docx_第4页
MyEclipse10+Struts2.1+Spring3.3+Hibernate3.3全注解框架搭建.docx_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、 新建web项目1. FileNewweb project2. 工程名称:king-back点选Java EE 6.0标准二、 添加ssh2支持1. 添加struts2工程上点右键myeclipseadd struts capabilites点选struts2.1url模式点选 /*添加struts2 core lib和struts2 spring lib新建LoginAction类package com.frank.action;import com.opensymphony.xwork2.ActionSupport;/* * author steve frank * */public class LoginAction extends ActionSupport private String name;private String password;public String getName() return name;public void setName(String name) = name;public String getPassword() return password;public void setPassword(String password) this.password = password;public String execute() if (name.equals(steve) & password.equals(123)return SUCCESS;elsereturn INPUT;修改struts.xml/success.jsp/fail.jsp 修改index.jspMy JSP index.jsp starting page 添加success.jspMy JSP success.jsp starting page$password添加fail.jspMy JSP fail.jsp starting page登录失败,用户名或密码错误.当前web.xml如下,直接启动会报spring监听错误,因为刚才已经添加了spring支持struts的lib包。添加spring监听,重新启动会提示找不到spring配置文件。2. 添加spring右键myeclipseadd spring capabilities勾选spring 3.0 aop lib,spring 3.0 core lib,spring 3.0 persistence core lib,spring 3.0 persistence jdbc lib,spring 3.0 web lib;点选copy checked library contents to project folder当前web.xml如下,重新启动tomcat,成功。打开浏览器,输入http:/localhost:8088/king-back,(tomcat端口未修改过的应该为8080),出现如下界面。直接点击submit,成功跳转到success.jsp,界面如下。如果修改用户名为frank,如下点击submit后跳转到fail.jsp如下3. 添加hibernate先配置好数据源驱动,如下右键myeclipseadd hibernate capabilities点选hibernate 3.3,勾选enable hibernate annotations support;勾选hibernate 3.3 annotations&entity manager,hibernate 3.3 core lib;点选copy checked library jars to project folder and add to build-path点选spring configuration file,交给spring配置数据源勾选existing spring configuration file选择配置好的数据源驱动 去掉create seesionfactory class的勾。然后点击完成。三、 数据反向生成1. 新建文件夹com.frank.model,准备放置生成的实体类和dao类2. 在项目中新建data.sql文件,内容如下,连接mysql的test库,新建表studentDROP TABLE IF EXISTS student;CREATE TABLE student ( id int(11) NOT NULL, name varchar(20) default NULL, role_id int(11) default NULL, PRIMARY KEY (id) ENGINE=InnoDB DEFAULT CHARSET=utf8;- - Records of student- -INSERT INTO student VALUES (1, 张三, 1);INSERT INTO student VALUES (2, 李四, 2);INSERT INTO student VALUES (3, 王五, 1);SELECT * FROM student;3. 生成Dao和实体类过程如下打开Test库,选择student表,右键hibernate reverse engineering出现如下视图,通过browse选择king-back项目和com.frank.model文件夹结果如下,勾选create pojo db table mapping information点选add hibernate mapping annotations to pojo保留update hibernate configuration with mapping resource location;去掉create abstract class勾;勾选java data access object(dao) 勾选generate precise findby methods点选spring daoId generator 选择native结果如下四、 编写测试1. 新建test类package test;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.frank.model.Student;import com.frank.model.StudentDAO;public class Test public static void main(String args) ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml);StudentDAO dao = (StudentDAO) context.getBean(StudentDAO);List list = dao.findAll();int n = list.size();for (int i = 0; i n; i+) Student c = list.get(i);String name = c.getName();System.out.println(name);运行结果如下,说明项目基本上是ok的。2. 修改action类public String execute() getData();if (name.equals(steve) & password.equals(123)return SUCCESS;elsereturn INPUT;private void getData() ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml);StudentDAO dao = (StudentDAO) context.getBean(StudentDAO);List list = dao.findAll();int n = list.size();for (int i = 0; i n; i+) Student c = list.get(i);String name = c.getName();System.out.println(name);刷新浏览器,查看控制台打印结果。五、 添加注解配置1. 修改struts.xml2. 修改LoginAction,在execute方法上添加Action(value = login, results = Result(name = INPUT, location = /index.jsp),Result(name = SUCCESS, location = /WEB-INF/content/success.jsp) )3. action注解完成,可以测试通过,接下来添加service注解等4. 修改applicationContext.xmlorg.hibernate.dialect.MySQLDialectcom.frank.model.Student5. Service、dao 的接口和实现类StudentService.javapublic interface StudentService /* * 根据主键查询Student * param id * return */public Student findById(Integer id);StudentServiceImpl.javaServicepublic class StudentServiceImpl implements StudentService Resourceprivate StudentDAO studentDAO;Overridepublic Student findById(Integer id) / TODO Auto-generated method stubreturn studentDAO.findById(id);StudentDAO.javapublic interface StudentDAOpublic abstract void save(Student transientInstance);public abstract void delete(Student persistentInstance);public abstract Student findById(java.lang.Integer id);public abstract List findByExample(Student instance);public abstract List findByProperty(String propertyName, Object value);public abstract List findByName(Object name);public abstract List findAll();public abstract Student merge(Student detachedInstance);public abstract void attachDirty(Student instance);public abstract void attachClean(Student instance);StudentDAOImpl.javaRepositorypublic class StudentDAOImpl extends HibernateDaoSupport implements StudentDAO private static final Logger log = LoggerFactory.getLogger(StudentDAOImpl.class);/ property constantspublic static final String NAME = name;private SessionFactory sessionFacotry;Resourcepublic void setSessionFacotry(SessionFactory sessionFacotry) super.setSessionFactory(sessionFacotry);protected void initDao() / do nothing/* * (non-Javadoc) * * see com.frank.dao.impl.S#save(com.frank.model.Student) */Overridepublic void save(Student transientInstance) log.debug(saving Student instance);try getHibernateTemplate().save(transientInstance);log.debug(save successful); catch (RuntimeException re) log.error(save failed, re);throw re;/* * (non-Javadoc) * * see com.frank.dao.impl.S#delete(com.frank.model.Student) */Overridepublic void delete(Student persistentInstance) log.debug(deleting Student instance);try getHibernateTemplate().delete(persistentInstance);log.debug(delete successful); catch (RuntimeException re) log.error(delete failed, re);throw re;/* * (non-Javadoc) * * see com.frank.dao.impl.S#findById(java.lang.Integer) */Overridepublic Student findById(java.lang.Integer id) log.debug(getting Student instance with id: + id);try Student instance = (Student) getHibernateTemplate().get(com.frank.model.Student, id);return instance; catch (RuntimeException re) log.error(get failed, re);throw re;/* * (non-Javadoc) * * see com.frank.dao.impl.S#findByExample(com.frank.model.Student) */Overridepublic List findByExample(Student instance) log.debug(finding Student instance by example);try List results = (List) getHibernateTemplate().findByExample(instance);log.debug(find by example successful, result size: + results.size();return results; catch (RuntimeException re) log.error(find by example failed, re);throw re;/* * (non-Javadoc) * * see com.frank.dao.impl.S#findByProperty(java.lang.String, * java.lang.Object) */Overridepublic List findByProperty(String propertyName, Object value) log.debug(finding Student instance with property: + propertyName+ , value: + value);try String queryString = from Student as model where model.+ propertyName + = ?;return getHibernateTemplate().find(queryString, value); catch (RuntimeException re) log.error(find by property name failed, re);throw re;/* * (non-Javadoc) * * see com.frank.dao.impl.S#findByName(java.lang.Object) */Overridepublic List findByName(Object name) return findByProperty(NAME, name);/* * (non-Javadoc) * * see com.frank.dao.impl.S#findAll() */Overridepublic List findAll() log.debug(finding all Student instances);try String queryString = from Student;return getHibernateTemplate().find(queryString); catch (RuntimeException re) log.error(find all failed, re);throw re;/* * (non-Javadoc) * * see com.frank.dao.impl.S#merge(com.frank.model.Student) */Overridepublic Student merge(Student detachedInstance) log.debug(merging Student instance);try Student result = (Student) getHibernateTemplate().merge(detachedInstance);log.debug(merge successful);return result; catch (RuntimeException re) log.error(merge failed, re);throw re;/* * (non-Javadoc) * * see com.frank.dao.impl.S#attachDirty(com.frank.model.Student) */Overridepublic void attachDirty(Student instance) log.debug(attaching dirty Student instance);try getHibernateTemplate().saveOrUpdate(instance);log.debug(attach successful); catch (RuntimeException re) log.error(attach failed, re);throw re;/* * (non-Javadoc) * * see com.frank.dao.impl.S#attachClean(com.frank.model.Student) */Overridepublic void attachClean(Student instance) log.deb

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论