**
emp的sql数据库;
2、整个项目的大致文件目录如下图
2.1 首先按照如上图的文件目录添加一些特别的文件,根据自己的需求添加。
2.2 梳理一下SSH整个思想的过程,用如下图SSH中的整个过程:【整个过程用一个项目的顺序叙述结合ssh过程为主】
***~~
2.2.1
~~ *** 在一个项目启动的时候,不管项目里面是否有内容,都必须要有web.xml这个配置文件。
web.xml配置 文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--用监听器启动spring-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--加载spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext.xml</param-value>
</context-param>
<!--启动struts2-->
<filter>
<filter-name>struts2</filter-name>
<!--过滤器的全类限定名-->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<!--要拦截的目标-->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
**
2.2.2
** 用户通过web层,也就是在web项目中的网页页面,在这个项目中就是index.html 和jsp(java服务端页面)*注:整个项目在tomcat运行的时候入口是index.html页面。
index.html代码如下:
<%--
Created by IntelliJ IDEA.
User: xionghaizi
Date: 2019/6/21
Time: 14:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>页面</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/empAction!findEmpAll.action">查询全部</a>
</body>
</html>
注:${pageContext.request.contextPath}是获取上下文
${pageContext.request.contextPath}/empAction!findEmpAll.action 是通过Action中去到服务层去操作。//如果不懂可以先往下读,不会影响。
emp/list.jsp的代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>显示所有员工</title>
</head>
<body>
<%--${empList}--%>
<table border="1" align="center">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>地址</th>
<th>生日</th>
<th>操作</th>
</tr>
<c:forEach items="${empList}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.name}</td>
<td>${emp.age}</td>
<td>${emp.address}</td>
<td>${emp.birthday}</td>
<td>
<a href="${pageContext.request.contextPath}/empAction!editPage.action?id=${emp.id}">修改</a>
<a href="${pageContext.request.contextPath}/empAction!deleteEmp.action?id=${emp.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
注:如用<c:forEach></c:forEach>就必须引入<%@taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
emp/edit.jsp代码:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/6/21
Time: 11:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>修改员工信息</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/empAction!editEmp.action" method="post">
<p>编号:<input type="text" name="emp.id" value="${emp.id}"> </p>
<p>姓名:<input type="text" name="emp.name" value="${emp.name}"> </p>
<p>年龄:<input type="text" name="emp.age" value="${emp.age}"> </p>
<p>地址:<input type="text" name="emp.address" value="${emp.address}"> </p>
<p>生日:<input type="date" name="emp.birthday" value="${emp.birthday}"> </p>
<p><input type="submit" value="修改保存"> </p>
</form>
</body>
</html>
界面运行截图1:
点击查询全部之后,会显示的显示全部的内容:如截图2
之后点击修改:截图3:
保存修改之后就会重新调转到显示全部内容的页面,也就是和截图一差不多的内容,点击删除操作也是如此。
页面运行的大概功能已经描述完成。接下来会像服务层的代码进阶。
**
2.2.3
**
从index.html中点击显示全部信息之后跳转,这个操作就必须经过服务层,在这个项目中的显示,也就是如下的java操作。
这个里面包括action,service,dao,entity,这个顺序也是在点击web页面上的按钮之后在服务层会执行的顺序。
如名称所解释,不做过多的介绍,比较重要的就是action,因为这个相当于是web能转到服务层的关键,而为了安全以及很多其他方面的考虑,会对其进行检测过滤,也就是ssh中的struts(过滤器)。
配置文件struts.xml [如需要具体关于配置文件的具体解释看我前面的关于Struts的博客]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!--常亮-->
<constant name="struts.devMode" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="emp" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="empAction" class="com.whpu.k16035.action.EmpAction" >
<result name="list">/jsp/emp/list.jsp</result>
<result name="edit">/jsp/emp/edit.jsp</result>
<!--重定向-->
<result name="requert" type="redirect">empAction!findEmpAll.action</result>
</action>
</package>
</struts>
之后的关于服务层的就直接给代码:
action/Empaction的代码:
package com.whpu.k16035.action;
import com.whpu.k16035.entity.Emp;
import com.whpu.k16035.service.EmpService;
import java.util.List;
/**
* 控制器:处理请求
*/
public class EmpAction {
//服务层
private EmpService empService;
//传递数据
private List<Emp> empList;
//获取id
private Integer id;
//对象
private Emp emp;
//查询全部
public String findEmpAll(){
//调用服务层的查询全部方法
empList=empService.selectEmpAll();
return "list";
}
//删除
public String deleteEmp(){
//调用服务层的删除方法
empService.deleteEmp(id);
//重定向到查询全部
return "requert";
}
// 修改查询 editPage
public String editPage(){
//调用服务层的查询单挑的方法
emp=empService.findEmpById(id);
//跳转到修改页面
return "edit";
}
// 修改保存数据 editEmp
public String editEmp(){
//调用服务层的修改方法
empService.editEmp(emp);
//重定向到查询全部的方法去
return "requert";
}
// --------------------------------------
public List<Emp> getEmpList() {
return empList;
}
public void setEmpList(List<Emp> empList) {
this.empList = empList;
}
public EmpService getEmpService() {
return empService;
}
public void setEmpService(EmpService empService) {
this.empService = empService;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Emp getEmp() {
return emp;
}
public void setEmp(Emp emp) {
this.emp = emp;
}
}
注意:struts.xml和EmpAction.java中需要对于的几个地方:【用黑体标记的出来】
<result name=“list”>/jsp/emp/list.jsp
<result name=“edit”>/jsp/emp/edit.jsp
<result name=“requert” type=“redirect”>empAction!findEmpAll.action
dao/EmpDao代码:
package com.whpu.k16035.dao;
import com.whpu.k16035.entity.Emp;
import java.util.List;
/**
* 持久层的接口
*/
public interface EmpDao {
//查询全部
List<Emp> selectEmpAll();
//删除
void deleteEmp(Emp emp);
//查找单条数据
Emp findEmpById(Integer id);
//修改
void editEmp(Emp emp);
}
dao/impl/EmpDaoImpl代码:
package com.whpu.k16035.dao.impl;
import com.whpu.k16035.dao.EmpDao;
import com.whpu.k16035.entity.Emp;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.util.List;
/**
* 持久层 操作数据库
*
*/
public class EmpDaoImpl extends HibernateDaoSupport implements EmpDao {
//查询全部
@Override
public List<Emp> selectEmpAll() {
return (List<Emp>)this.getHibernateTemplate().find("From Emp");
}
//删除数据
@Override
public void deleteEmp(Emp emp) {
getHibernateTemplate().delete(emp);
}
//查询单条
@Override
public Emp findEmpById(Integer id) {
return getHibernateTemplate().get(Emp.class,id);
}
//修改保存
@Override
public void editEmp(Emp emp) {
getHibernateTemplate().update(emp);
}
}
service/EmpService代码:
package com.whpu.k16035.service;
import com.whpu.k16035.entity.Emp;
import java.util.List;
/**
* 服务层 处理业务逻辑的
* 事物管理
*/
public interface EmpService {
//查询全部
List<Emp> selectEmpAll();
//删除数据
void deleteEmp(Integer id);
//查询单条
Emp findEmpById(Integer id);
//修改
void editEmp(Emp emp);
}
service/impl.EmpServiceImpl代码:
package com.whpu.k16035.service.impl;
import com.whpu.k16035.dao.EmpDao;
import com.whpu.k16035.dao.impl.EmpDaoImpl;
import com.whpu.k16035.entity.Emp;
import com.whpu.k16035.service.EmpService;
import java.util.List;
/**
* 服务层的实现类
*/
public class EmpServiceImpl implements EmpService {
//创建持久层对象
//private EmpDao empDao =new EmpDaoImpl();
private EmpDao empDao;
//查询全部
@Override
public List<Emp> selectEmpAll() {
return empDao.selectEmpAll();
}
//删除
@Override
public void deleteEmp(Integer id) {
Emp emp = new Emp();
emp.setId(id);
empDao.deleteEmp(emp);
}
@Override
public Emp findEmpById(Integer id) {
return empDao.findEmpById(id);
}
@Override
public void editEmp(Emp emp) {
empDao.editEmp(emp);
}
//-------------------
public EmpDao getEmpDao() {
return empDao;
}
public void setEmpDao(EmpDao empDao) {
this.empDao = empDao;
}
}
entity/Emp代码:
package com.whpu.k16035.entity;
import java.util.Date;
public class Emp {
//创建与表对应的属性
private Integer id;
private String name;
private Integer age;
private String address;
private Date birthday;
//构造方法 一定要有无参构造
public Emp() {
}
//setter getter
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", birthday=" + birthday +
'}';
}
}
**
2.2.4
**
Hibernate映射文件,hibernate.xml文件:这个是持久层,是连接数据库最后的一层,Hibernate是可以简化对数据库的操作。
1、.创建实体类映射文件
Emp.hbm.xml
2、创建Hibernate的主配置文件
hibernate.cfg.xml
Emp.hbm.xml配置文件代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.whpu.k16035.entity">
<class name="Emp" table="emp">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="64"/>
</property>
<property name="age" type="java.lang.Integer" column="age"/>
<property name="address" type="java.lang.String" column="address"></property>
<property name="birthday" type="java.sql.Date" column="birthday"></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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--方言-->
<!--<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>-->
<!--<!–连裤四要素–>-->
<!--<property name="connection.url">jdbc:mysql://localhost:3306/test4?characterEncoding=utf-8</property>-->
<!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property>-->
<!--<property name="connection.username">root</property>-->
<!--<property name="connection.password">123</property>-->
<!--现实sql语句-->
<!--<property name="show_sql">true</property>-->
<!--<!–启动是建表的类型–>-->
<!--<property name="hbm2ddl.auto">update</property>-->
<!--<!–映射文件–>-->
<!--<mapping resource="mapper/Emp.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
注:可以有人会关于这个有疑问,SSH为什么之说了Struts和Hibernate,并且Hibernate的著配置文件中什么都没有,这就是我想说的Spring,从最开始的SSH那个思想图可以很明显的看出来,Spring是贯穿整个过程的,而在Hibernate主配置中的内容在都存在与Spring有关的配置文件中,所以可以注解掉。
**
2.2.4:Spring中的上下文ApplicationContext.xml配置文件
**
代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--1.加载连库四要素-->
<context:property-placeholder location="classpath:dbconfig/dbmysql.properties" />
<!--2.创建数据库连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--创建sessionFactroy-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载hibernate主配置文件-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<!--加载实体类映射文件-->
<property name="mappingLocations" value="classpath*:mapper/*.hbm.xml"/>
<!--hibernate的其他配置-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!--事物管理-->
<!--事物管理器-->
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--事物传播特性-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*"/>
<!--select* 方法设置只-->
<tx:method name="select*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--事物切面-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.whpu.k16035.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<!--dao层-->
<bean id="empDao" class="com.whpu.k16035.dao.impl.EmpDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--服务层-->
<bean id="empService" class="com.whpu.k16035.service.impl.EmpServiceImpl">
<property name="empDao" ref="empDao"/>
</bean>
<!--控制器-->
<bean id="empAction" class="com.whpu.k16035.action.EmpAction" scope="prototype">
<property name="empService" ref="empService"/>
</bean>
</beans>
到这里整个SSH的实现过程就已经基本完成了,只需要一些关于环境的配置以及其他需要自己完成。
希望我的整理可以对你有一点用。 ^ - ^
from by 熊xyi。