本文共 7913 字,大约阅读时间需要 26 分钟。
三大框架(Hibernate、Struts2、Spring)学完之后,我们会立马做一个CRM系统(客户关系管理系统)的项目,CRM系统中有一个显示客户列表的功能,效果如下图:
现在,我们虽说只是学完了Hibernate框架,Struts2框架也只是入门了,但是仅凭现在的知识也是可以实现这个显示客户列表的功能的,只不过肯定和最终版的没法比。搭建开发环境
创建web项目,引入jar包
创建一个web项目,例如crm_struts2,并引入相关的jar包,那引入哪些jar包呢?由于我们在dao层使用的Hibernate框架,所以得导入与Hibernate框架开发相关的依赖jar包,其次还得导入Struts2基本的开发包。
引入相关的配置文件
首先,在项目中引入Struts2的核心配置文件(struts.xml),它里面的内容一开始是空的哟!然后在项目的web.xml文件中配置Struts2的核心过滤器。
接着,我们还要在项目中引入Hibernate的核心配置文件(hibernate.cfg.xml),其内容如下:com.mysql.jdbc.Driver jdbc:mysql:///struts2_crm root liayun org.hibernate.dialect.MySQLDialect true true update org.hibernate.connection.C3P0ConnectionProvider 5 20 120 3000 4 thread
因为现在我们还没有编写实体类及其相应的映射配置文件,所以hibernate.cfg.xml文件中还没有引入映射配置文件。这些事情,待会就会做,先就这样配啊!
最后,还得在项目中引入日志记录文件(log4j.properties),其内容如下:### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c\:mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' #### error warn info debug tracelog4j.rootLogger= info, stdout
创建数据库和表
创建一个数据库,并在该数据库下新建一张客户表,这里笔者使用的数据库是MySQL。
create database struts2_crm;use struts2_crm;CREATE TABLE `cst_customer` ( `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)', `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)', `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源', `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业', `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别', `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话', `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话', PRIMARY KEY (`cust_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
创建包结构
引入相应的页面
CRM系统中用到的所有页面可以点击我给的百度网盘链接行下载。
- 百度网盘链接地址:,提取码:ct3t。
这里,除了要将CRM系统中用到的所有页面导入到我们的项目之外,还记得将所有的htm静态页面改为jsp页面。咋改?不用我教吧!改完之后,发布我们的项目,试着访问一下项目的首页,看能不能出现下面的效果。
至此,开发环境就搭建起来了,接下来就是编写代码实现需求的事情了。案例代码实现
编写Customer实体类及其相对应的映射配置文件
在com.meimeixia.domain包下创建一个Customer实体类及其相对应的映射配置文件。
-
Customer实体类
package com.meimeixia.domain;public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_phone; private String cust_mobile; public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source + ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile + "]"; } }
-
Customer.hbm.xml
记得在Hibernate的核心配置文件(hibernate.cfg.xml)中引入以上映射配置文件哟!
在菜单页面(menu.jsp)中修改提交路径
编写web层
在com.meimeixia.web.action包下创建一个CustomerAction类,并在里面写一个查询客户列表的方法。
package com.meimeixia.web.action;import java.util.List;import org.apache.struts2.ServletActionContext;import com.meimeixia.domain.Customer;import com.meimeixia.service.CustomerService;import com.meimeixia.service.impl.CustomerSeviceImpl;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;/** * 客户管理的Action * @author liayun * */public class CustomerAction extends ActionSupport { //查询客户列表的方法 public String find() { //调用业务层 CustomerService customerService = new CustomerSeviceImpl(); Listlist = customerService.find(); //需要带到页面上,即页面跳转 ServletActionContext.getRequest().setAttribute("list", list); return "findSuccess"; } }
编写业务逻辑层
我们在com.meimeixia.service包下创建一个CustomerService接口,该接口的具体代码如下:
package com.meimeixia.service;import java.util.List;import com.meimeixia.domain.Customer;public interface CustomerService { public Listfind();}
接着在com.meimeixia.service.impl包下编写CustomerService接口的一个实现类——CustomerSeviceImpl.java。
package com.meimeixia.service.impl;import java.util.List;import com.meimeixia.dao.CustomerDao;import com.meimeixia.dao.impl.CustomerDaoImpl;import com.meimeixia.domain.Customer;import com.meimeixia.service.CustomerService;public class CustomerSeviceImpl implements CustomerService { @Override public Listfind() { //调用Dao CustomerDao customerDao = new CustomerDaoImpl(); return customerDao.find(); }}
编写dao层
首先,在com.meimeixia.utils包下创建一个工具类,该工具类专门用于获取Session对象,利用它与数据库打交道。
package com.meimeixia.utils;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtils { public static final Configuration cfg; public static final SessionFactory sf;//一个项目只会创建一个SessionFactory对象 static { cfg = new Configuration().configure(); sf = cfg.buildSessionFactory(); } public static Session openSession() { return sf.openSession(); } public static Session getCurrentSession() { return sf.getCurrentSession(); } }
然后,在com.meimeixia.dao包下创建一个CustomerDao接口,该接口的具体代码如下:
package com.meimeixia.dao;import java.util.List;import com.meimeixia.domain.Customer;public interface CustomerDao { Listfind();}
接着在com.meimeixia.dao.impl包下编写CustomerDao接口的一个实现类——CustomerDaoImpl.java。
package com.meimeixia.dao.impl;import java.util.List;import org.hibernate.Session;import org.hibernate.Transaction;import com.meimeixia.dao.CustomerDao;import com.meimeixia.domain.Customer;import com.meimeixia.utils.HibernateUtils;public class CustomerDaoImpl implements CustomerDao { //查询客户的方法 @Override public Listfind() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); List list = session.createQuery("from Customer").list(); tx.commit(); return list; }}
配置Action
在struts.xml文件中配置上自己编写的CustomerAction类,就像下面这样:
/jsp/customer/list.jsp
在页面中显示相应的数据
由于要在jsp页面中显示一批客户的信息,那么这个时候就要用到jstl标签了,所以你还得在咱的项目中导入jstl的jar包。但是,这里我们就不用导了,因为在搭建开发环境的时候,咱已经导入了。接下来,在WebContent/jsp/customer/list.jsp页面中编写显示客户列表的代码:
测试程序
发布我们的项目到Tomcat服务器并启动,然后访问该项目的首页,点击客户列表超链接,就能显示一系列客户的信息了。
转载地址:https://liayun.blog.csdn.net/article/details/99199707 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!