- 实体类代码
public class User implements Serializable {
private Long custId;
private String custName;
private String custSource;
private String custInustry;
private String custLevel;
private String custAddress;
private String custPhone;
// 以下是setter/getter方法
- 从请求发起方开始写代码:
- 第一步:写JSP中的请求
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/customer/findAll">查询用户</a>
<a href="${pageContext.request.contextPath}/customer/findAll.action">查询用户</a>
</body>
</html>
- 第二步:写动作类:在实际开发中动作类都是继承
ActionSupport
类,此时如果要使用ActionSupport
类,我们必须要导包
:导struts2的相关jar包
- 第三步: 编写动作类:编写动作方法
package com.ycom1024.struts2.user.web.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 动作类
* @author Administrator
*
*/
public class CustomerAction extends ActionSupport {
/**
* 动作方法
* @return
*/
public String findAll() {
return SUCCESS;
}
}
- 第四步:接下来就需要写配置文件:struts.xml配置文件
<?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>
<!-- 打开开发者模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 建立动作名称、动作类、动作方法的对应关系 -->
<package name="p1" extends="struts-default" namespace="/customer">
<action name="findAll" class="com.ycom1024.struts2.user.web.action.CustomerAction" method="findAll">
<result name="findAll" type="dispatcher">/list.jsp</result>
</action>
</package>
</struts>
- 第五步:在web.xml文件中配置核心过滤器:核心控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 配置核心过滤器 -->
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 到这里web层的基本结束了,下面要写的就是service层了
- 第六步:service层的接口与实现类
public class CustomerServiceImpl implements ICustomerService {
// service层调用dao层服务
ICustomerDao customerDao = new CustomerDaoImpl();
@Override
public List<User> findAll() {
return customerDao.findAll();
}
}
- 第七步:有了service层,service层中是通过调用dao层的服务器来实现的:所以这一步需要实现dao层的接口与实现类
public class CustomerDaoImpl implements ICustomerDao{
@Override
public List<User> findAll() {
// TODO Auto-generated method stub
return null;
}
}
- 第八步:在dao层中使用的是hibernate,那么现在需要搭建hibernate的环境:导包、编写映射配置文件、编写核心配置文件、准备数据库
(1) 准备数据表,准备测试数据
CREATE TABLE t_customer(
cid INT PRIMARY KEY AUTO_INCREMENT COMMENT '客户id',
`name` VARCHAR(50) COMMENT '客户姓名',
source VARCHAR(50) COMMENT '客户来源',
industry VARCHAR(50) COMMENT '行业',
`level` VARCHAR(10) COMMENT '客户等级',
address VARCHAR(50) COMMENT '联系地址',
phone VARCHAR(20) COMMENT '客户电话'
)CHARSET utf8;
INSERT INTO t_customer(NAME, source, industry, LEVEL, address, phone)
VALUES
('A', 'net', '1', 'vip', 'china', '123456'),
('B', 'local', '2', 'normal', 'japan', '564789')![在这里插入图片描述](https://img-blog.csdnimg.cn/20200820091853928.png#pic_center)
(2) 导包:导hibernate的jar包
(3) 编写hibernate的映射配置文件
<?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">
<hibernate-mapping>
<class name="com.ycom1024.struts2.user.vo.User" table="t_customer">
<id name="custId" column="cid">
<generator class="native"></generator>
</id>
<property name="custName" column="name"></property>
<property name="custSource" column="source"></property>
<property name="custIndustry" column="industry"></property>
<property name="custLevel" column="level"></property>
<property name="custAddress" column="address"></property>
<property name="custPhone" column="phone"></property>
</class>
</hibernate-mapping>
(4) 编写hibernate的核心配置文件
<?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:///hibernate</property>
<!-- 访问数据库服务器的用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 访问数据库服务器的密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 第二部分:Hibernate自身的配置 -->
<!-- 让Hibernate输出SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 让Hibenate输出的SQL语句是格式化的SQL语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 让Hibernate帮助我们创建数据表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置MySQL数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 获取本地线程Session的配置 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 第三部分:加载映射配置文件 -->
<mapping resource="com/ycom1024/struts2/user/vo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(6) 添加hibernate的工具类HibernateUtils
- 第九步:编写dao操作数据库的代码
public class CustomerDaoImpl implements ICustomerDao{
@Override
public List<User> findAll() {
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try {
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
Query query = session.createQuery("from User");
List<User> list = query.list();
tx.commit();
return list;
} catch (Exception e) {
tx.rollback();
} finally {
if (session != null) session.close();
}
return null;
}
}
-
结果
-
完整的请求响应的完整链路