最近在复习struts2,跟着视屏练习了一个客户关系管理系统的小案例,现将流程总结如下:
第一步:创建WEB工程,导入jar包
包括struts2和hibernate的jar包。
第二步:引入相关页面
该案例的主要用来讲解struts2框架的开发流程,所以前端页面就直接拿过来用了,不再做具体的讲解了。
第三步:创建项目结构(包和相关类)
第四步:在web.xml中配置核心过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>struts_crm</display-name>
<!-- 配置struts2的核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第五步:创建struts.xml配置文件
第六步:修改菜单页面的提交路径
第七步:编写Model类
package com.zju.model;
public class Customer {
/*
* 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_linkman` VARCHAR(64) 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;
*/
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
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_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
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 + "]";
}
}
第八步:编写Action类
package com.zju.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zju.model.Customer;
import com.zju.service.CustomerService;
import com.zju.service.impl.CustomerServiceImpl;
/**
* 客户管理的Action类
* @author PC
*/
public class CustomerAction extends ActionSupport{
//查询客户列表的方法
public String findAll(){
//创建业务层类的对象
CustomerService customerService = new CustomerServiceImpl();
//调用业务层的方法查询所有的用户
List<Customer> list = customerService.findAll();
//获得request对象,并且保存到request中
ServletActionContext.getRequest().setAttribute("list", list);
//findAll对应jsp中<result>中的name属性
return "findAll";
}
}
第九步:编写业务层(service)代码
package com.zju.service;
import java.util.List;
import com.zju.model.Customer;
public interface CustomerService {
List<Customer> findAll();
}
package com.zju.service.impl;
import java.util.List;
import com.zju.dao.CustomerDao;
import com.zju.dao.impl.CustomerDaoImpl;
import com.zju.model.Customer;
import com.zju.service.CustomerService;
public class CustomerServiceImpl implements CustomerService {
@Override
public List<Customer> findAll() {
//创建数据层Dao类的对象
CustomerDao customerDao = new CustomerDaoImpl();
//调用customerDao中的findAll()方法
return customerDao.findAll();
}
}
第十步:添加映射文件(Customer.hbm.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 配置表与实体对象的关系 -->
<!-- package属性:填写一个包名.在元素内部凡是需要书写完整类名的属性,可以直接写简答类名了. -->
<hibernate-mapping package="com.zju.model">
<!-- class元素: 配置实体与表的对应关系的 name: 完整类名 table:数据库表名 -->
<class name="Customer" table="cst_customer">
<!-- id元素:配置主键映射的属性 name: 填写主键对应属性名 column(可选): 填写表中的主键列名.默认值:列名会默认使用属性名
type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型. 每个类型有三种填法: java类型|hibernate类型|数据库类型
not-null(可选):配置该属性(列)是否不能为空. 默认值:false length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度 -->
<id name="cust_id">
<!-- generator:主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- property元素:除id之外的普通属性映射 name: 填写属性名 column(可选): 填写列名 type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
每个类型有三种填法: java类型|hibernate类型|数据库类型 not-null(可选):配置该属性(列)是否不能为空. 默认值:false
length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度 -->
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
第十一步:编写hibernate.cfg.xml中的配置信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 必要的配置信息:连接数据库的基本配置信息 -->
<!-- 数据库连接驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库连接url -->
<property name="hibernate.connection.url">jdbc:mysql:///struts_crm</property>
<!-- 数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="hibernate.connection.password">123456</property>
<!-- 数据库方言的配置
不同的数据库中,aql语法略有区别,指定方言可以让hibernate框架在生成sql语句时,针对数据库的方言生成
注意:MySQL在选择方言时,请选择最短的方言。
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 将hibernate生成的sql语句打印到控制台上 -->
<property name="hibernate.show_sql">true</property>
<!-- 将hibernate生成的sql语句格式化(语法缩进) -->
<property name="hibernate.format_sql">true</property>
<!--
hibernate.hbm2ddl.auto create 自动建表.每次框架运行都会创建新的表.以前表将会被覆盖,表数据会丢失.(开发环境中测试使用)
hibernate.hbm2ddl.auto create-drop 自动建表.每次框架运行结束都会将所有表删除.(开发环境中测试使用)
hibernate.hbm2ddl.auto update(推荐使用) 自动生成表.如果已经存在不会再生成.如果表有变动.自动更新表(不会删除任何数据).
hibernate.hbm2ddl.auto validate 校验.不自动生成表.每次启动会校验数据库中表是否正确.校验失败.
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 指定hibernate操作数据库时的隔离级别
#hibernate.connection.isolation 1 2 4 8
0001 1 读为提交
0010 2 读已提交
0100 4 可重复度
1000 8 串行化
-->
<property name="hibernate.connection.isolation">4</property>
<!-- 指定session与当前的线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 引入orm数据源,路径:填写src下的路径 -->
<mapping resource="com/zju/model/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
第十二步:引入工具类
package com.zju.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory sf;
static {
// 1 创建,调用空参构造
Configuration conf = new Configuration().configure();
// 2 根据配置信息,创建 SessionFactory对象
sf = conf.buildSessionFactory();
}
// 获得session => 获得全新session
public static Session openSession() {
// 3 获得session
Session session = sf.openSession();
return session;
}
// 获得session => 获得与线程绑定的session
public static Session getCurrentSession() {
// 3 获得session
Session session = sf.getCurrentSession();
return session;
}
public static void main(String[] args) {
System.out.println(HibernateUtils.openSession());
}
}
第十三步:编写数据层(Dao)代码
package com.zju.dao;
import java.util.List;
import com.zju.model.Customer;
public interface CustomerDao {
List<Customer> findAll();
}
package com.zju.dao.impl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zju.dao.CustomerDao;
import com.zju.model.Customer;
import com.zju.utils.HibernateUtils;
public class CustomerDaoImpl implements CustomerDao{
@Override
//Dao中查询所有客户的方法
public List<Customer> findAll() {
Session session = HibernateUtils.openSession();
//开启事务
Transaction tx = session.beginTransaction();
//查询所有的客户
List<Customer> list = session.createQuery("from Customer").list();
//提交事务
tx.commit();
//关闭session
session.close();
return list;
}
}
第十四步:在struts.xml中配置Action的页面跳转
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 指定struts2已开发者模式运行
1.热加载主配置,修改了配置信息,不需要重启即可生效
2.提供更多的错误信息的输出,方便开发时的调试
-->
<constant name="struts.devMode" value="true"></constant>
<package name="crm" extends="struts-default" namespace="/">
<action name="CustomerAction_*" class="com.zju.action.CustomerAction" method="{1}">
<result name="list">/jsp/customer/list.jap</result>
</action>
</package>
</struts>
第十五步:在页面中回显数据
引入jstl的jar包
<TBODY>
<TR style="FONT-WEIGHT: bold; FONT-STYLE: normal; BACKGROUND-COLOR: #eeeeee; TEXT-DECORATION: none">
<TD>客户名称</TD>
<TD>客户级别</TD>
<TD>客户来源</TD>
<TD>联系人</TD>
<TD>电话</TD>
<TD>手机</TD>
<TD>操作</TD>
</TR>
<c:forEach items="${list }" var="customer">
<TR style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
<TD>${customer.cust_name }</TD>
<TD>${customer.cust_level }</TD>
<TD>${customer.cust_source }</TD>
<TD>${customer.cust_linkman }</TD>
<TD>${customer.cust_phone }</TD>
<TD>${customer.cust_mobile }</TD>
</TR>
</c:forEach>
</TBODY>