加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ssh框架搭建:婚庆财务管理系统,AJAX自动刷新,实现增删改查四

发布时间:2020-12-16 03:30:46 所属栏目:百科 来源:网络整理
导读:以前我也写过ssh ( 《图书信息管理》 的: http://blog.csdn.net/ssh159/article/details/52439676 ) 快要考试了,顺便发个贴复习一下吧! 这个java 后端 是: 1、struts2,spring4.2,hibernate5.2, ssh框架 ),实现功能:基本的 增 删 改 查 四个, (

以前我也写过ssh

《图书信息管理》的:http://blog.csdn.net/ssh159/article/details/52439676



快要考试了,顺便发个贴复习一下吧!
这个java 后端 是:
1、struts2,spring4.2,hibernate5.2,
ssh框架),实现功能:基本的增 删 改 查四个,
(写来写去都是这四个,
oracle数据库的 下次 带着 多表并联一起发。)
2、mysql数据库,虽然用mysql,但是只需要创建数据库,建表 由 实体类 的注解搞定!

这样一运行,就创建了表,然后可以直接插入数据了!

3、AJAX自动刷新


代码列表一览:

财务管理后台
(这里 电话号码:没有写11位数的约束,因为有些手机可能要加0,有些可能是座机等,大家自行在 实体类写注解吧!)



页面:


————————————————————————————————————————————

四个功能一览:

1、添加


2、其余三个:修改,删除,查询

(之前找不到好的截图软件,画质不堪入目!现在还好吧!)

查询的时候,做了:职业 和 姓名 查询2个,

每次输入后回车即可,但是会清除你的输入,空查询是 返回显示的页面,显示全部。



————————————————————————————————————————————————————

项目的检测流程:

1、运行tomat 相当于,启动default.jsp



2、先找struts.xml ,重定向跳转action;

3、Action 找 Service

4、Service 找Dao

5、Dao中的SessionFactory 事务管理找 applicationContext

6、ac 中的 sessionfactory 找 entity 实体类

————————————————————————————————————————————————

代码公布:

一、JAVA代码
二、SSH框架的代码
三、jsp 网页代码
四、其他代码

一、JAVA代码

1、ProductAction.java

package product.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import product.entity.Product;
import product.service.ProductService;
@SuppressWarnings("serial") //这个注解是取消警告的,不用也行。
@Controller 		//控制层注解
@Scope("prototype") //规范注解
public class ProductAction extends ActionSupport  {

	@Autowired
	private ProductService productService;
	
	private List<Product> list; //list会把信息输出到jsp页面的 table列表
	private Integer[] ids;
	private Product p;
	private String like;
	private Integer update_id;
	private String status;
	
	public String getStatus() {
		return status;
	}

	public Product getP() {
		return p;
	}

	public void setP(Product p) {
		this.p = p;
	}

	public void setUpdate_id(Integer update_id) {
		this.update_id = update_id;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public List<Product> getList() {
		return list;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

//显示
	public String showAll(){
		list=productService.showAll();
		return "show";
	}
//添加	
	public String sendAdd(){
		p=null;
		status = "添加";
		return "send";
	}
	
	public String addPro(){
		productService.addPro(p);
		return "add";
	}
	
//删除	
	public String delPro(){
		productService.delPro(ids);
		return "del";
	}
	
//查询
	public String find(){
		if(like.isEmpty()){
			list=productService.showAll();
		}else{
			list=productService.find(like);
		}
		like="";
		return "find";
	}
	
//修改	
	public String updatePro(){
		productService.updatePro(p);
		return "update";
	}
	
	public String sendUpdate(){
		p=productService.find(update_id);
		p.setId(update_id);
		productService.updatePro(p);
		status = "修改";
		return "send";
	}
}

2.1、ProductDao.java

package product.dao;

import java.util.List;

import product.entity.Product;

public interface ProductDao {

	public List<Product> showAll();
	public void addPro(Product p);
	public void delPro(Integer[] ids);
	public List<Product> find(String like);
	public Product find(Integer id);
	public void updatePro(Product p);
}

2.2、ProductDaoImpl.java

package product.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import product.entity.Product;
@Repository	//数据库访问层的注解
@Scope("prototype")
public class ProductDaoImpl implements ProductDao {

//事务管理	
	@Autowired
	private SessionFactory sessionFactory;
	private Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	@Override
	public List<Product> showAll() {
		return getSession().createQuery("from Product",Product.class).getResultList();
	}
	@Override
	public void addPro(Product p) {
		p.setDate(new Date());
		getSession().save(p);
	}
	@Override
	public void delPro(Integer[] ids) {
		if(ids!=null){
			for(Integer id:ids){
				getSession().delete(getSession().find(Product.class,id));
			}
		}
	}

	public List<Product> find(String like){
		String sql="from Product where name like :name or profession like :profession ";
		return getSession().createQuery(sql,Product.class)
				.setParameter("name","%"+like+"%")
				.setParameter("profession","%"+like+"%")
				.getResultList();
	}
	@Override
	public Product find(Integer id) {
		if(id!=null){
			return getSession().find(Product.class,id);
		}else{
			return null;
		}
	}
	@Override
	public void updatePro(Product p) {
		getSession().update(p);
	}
}

3、 实体类:Product.java

package product.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity	//实体类注解
@Table(name="moneyjs")
public class Product {

	private Integer id;
	private String profession;
	private String name;
	private String work;
	private String remark;
	private String phone;
	private Integer money;
	private Date date;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	@Column(length=10)
	public String getProfession() {
		return profession;
	}
	public void setProfession(String profession) {
		this.profession = profession;
	}
	
	@Column(length=10)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(length=50)
	public String getWork() {
		return work;
	}
	public void setWork(String work) {
		this.work = work;
	}
	
	@Column(length=50)
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@Column(length=11)
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	@Column
	public Integer getMoney() {
		return money;
	}
	public void setMoney(Integer money) {
		this.money = money;
	}
	
	@Column
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	@Override
	public String toString() {
		return "Product [id=" + id + ",profession=" + profession + ",name=" + name + ",work=" + work + ",remark="
				+ remark + ",phone=" + phone + ",money=" + money + ",date=" + date + "]";
	}
	
	/*@GeneratedValue(generator = "uuid")
	@GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid",strategy = "uuid")*/
	/*@ManyToOne(cascade={CascadeType.ALL},optional=true,fetch=FetchType.EAGER)
	@JoinColumn(name="orderid")

	@OneToMany(mappedBy="order",cascade={CascadeType.ALL},fetch=FetchType.EAGER)*/
	
	
}

4.1、ProductService.java (service接口和dao接口是一样的)

package product.service;

import java.util.List;

import product.entity.Product;

public interface ProductService {

	public List<Product> showAll();
	public void addPro(Product p);
	public void delPro(Integer[] ids);
	public List<Product> find(String like);
	public Product find(Integer id);
	public void updatePro(Product p);
}

4.2、ProductServiceImpl.java (虽然是业务逻辑处理,其实是最简单了!)

package product.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import product.dao.ProductDao;
import product.entity.Product;
@Repository		//业务逻辑处理层的注解
@Scope("prototype")
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDao productDao;
	
	@Override
	public List<Product> showAll() {
		
		return productDao.showAll();
	}
	
	public void addPro(Product p){
		productDao.addPro(p);
	}

	public void delPro(Integer[] ids){
		productDao.delPro(ids);
	}
	
	public List<Product> find(String like){
		return productDao.find(like);
	}
	
	@Override
	public Product find(Integer id) {
		return productDao.find(id);
	}
	@Override
	public void updatePro(Product p) {
		productDao.updatePro(p);
	}
}

二、SSH框架层代码

1、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
		xmlns:p="http://www.springframework.org/schema/p"  
		xmlns:aop="http://www.springframework.org/schema/aop"   
		xmlns:context="http://www.springframework.org/schema/context"  
		xmlns:jee="http://www.springframework.org/schema/jee"  
		xmlns:tx="http://www.springframework.org/schema/tx"  
		xsi:schemaLocation="    
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
	<!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由spring去管理 -->
	
	<!-- (本案例不用到,只是用了一个全盘扫描,以上内容只是为了让大家了解它) -->
	
	<context:component-scan base-package="product.."/>
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->

	<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
		<!-- 每300秒检查所有连接池中的空闲连接 -->
		<property name="idleConnectionTestPeriod" value="300"/>
		<!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
		<property name="maxIdleTime" value="900"/>
		<!-- 最大连接数 -->
		<property name="maxPoolSize" value="2"/>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="myDataSource"/>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<prop key="hibernate.connection.autocommit">false</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<property name="packagesToScan">
		    <list>
		        <value>product.entity</value>
		    </list>
		</property>

	</bean>
	
	<bean id="transactionManager" 
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
	 	<!--  创建事务管理器,管理sessionFactory(因为所有的session都是从sessionFactory获取的) -->
	 	<property name="sessionFactory" ref="sessionFactory" />
	 </bean>
	<!--  配置通知,哪些方法需要切入什么类型的事务 -->	 
	<tx:advice id="advice" transaction-manager="transactionManager">
	 	<tx:attributes>
	 		<tx:method name="add*" propagation="REQUIRED"/>
	 		<tx:method name="del*" propagation="REQUIRED"/>
	 		<tx:method name="update*" propagation="REQUIRED"/>
	 		<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
	 	</tx:attributes>
	</tx:advice>
	 
	 <!-- 配置切面表达式,并且让 tx与切面表达式合二为一 -->
	 <aop:config>
	 	<!-- 表达式,定义哪个包的哪些类需要切入事务,但是此处并且没有制定类中哪些方法,需要切入什么样 事务 -->
	 	<aop:pointcut expression="execution(* product.dao.*.*(..))" id="pointcut" />
	 	<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
	 </aop:config>
</beans>
			

2、jdbc.properties (由于偷懒,只写了一套mysql的数据库连接信息,oracle的没写呢)

我的mysql 密码是空的

jdbc.driver:com.mysql.jdbc.Driver
jdbc.url:jdbc:mysql:///gfmoney
jdbc.user:root
jdbc.password:

3、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.objectFactory" value="spring" />
	<constant name="struts.devMode" value="true" />  <!-- 开发者模式,要是报错找不到,请注释这一行 -->
	<constant name="struts.ui.theme" value="simple"/>
	<package name="mypck001" extends="struts-default">
		<action name="product_*" class="productAction" method="{1}">
			<result name="show">/WEB-INF/jsp/index.jsp</result>
			<result name="find">/WEB-INF/jsp/index.jsp</result>
			<result name="del" type="redirect">product_showAll</result>
			<result name="add" type="redirect">product_showAll</result>
			<result name="update" type="redirect">product_showAll</result>
			<result name="send">/WEB-INF/jsp/add.jsp</result>
		</action>
	</package>
</struts>

4、WebContext 目录下,web.xml

struts :检测

spring:监听

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>pro3</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
<error-page>
  	<error-code>404</error-code>
  	<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
<error-page>
  	<error-code>500</error-code>
  	<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
  
  <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>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

三、jsp页面代码:

1、add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>添加/修改页面</title>
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
</head>
<body>
<center>

<s:form action="product_addPro.action" id="myform">
	职业:<s:textfield name="p.profession" value="%{p.profession}"/><br>
	姓名:<s:textfield name="p.name" value="%{p.name}"/><br>
	工作:<s:textfield name="p.work" value="%{p.work}"/><br>
	备注:<s:textfield name="p.remark" value="%{p.remark}"/><br>
	电话:<s:textfield name="p.phone" value="%{p.phone}"/><br>
	工钱:<s:textfield name="p.money" value="%{p.money}"/><br>
	时间:<s:textfield name="p.date" value="%{p.date}"/> <br>
	<s:hidden id="hidden" value="%{status}"/>
	<s:hidden name="p.id" value="%{p.id}"/>
	<s:submit value="添加" id="btn"/>
</s:form>

</center>

<script type="text/javascript">
$(function(){
	$("#btn").val($("#hidden").val());
	$("#btn").click(function(){
		if($(this).val()=="修改"){
			$("#myform").prop("action","product_updatePro.action");
		}else{
			$("#myform").prop("action","product_addPro.action");
		}
	})
})

</script>
</body>
</html>

2、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
<!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>某婚庆财务管理系统</title>
<style>	
  /* body{background: url(js/ct.jpg);width:100%} */
  body{color:#666666;}
  #table_all{text-align: center;}
  
  table{width:1000px;border:1px solid black;text-align: center; margin-top: 30px;}
  th,td{border:1px black solid;}
  #query,#delete,#sendAdd{
  text-decoration: none;
  width: 80dp;height: 35dp;font-size: 18px;}
  #delete{color:red;}
  #query2{width: 220px;height: 25px;}
</style>

</head>
<body>
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>

<center>
	
	<h2 style="color: #00a2e3">xx婚庆财务管理系统</h2>
	
<s:form action="product_find">
	
	模糊查询:<input id="query2" name="like" placeholder=" 姓名 或 职业查询"> <s:submit id="query" value="查询"/>
	
</s:form>
<s:form action="product_delPro.action">
	<table border="1">
		<tr>
			<th>选择</th>
			<th>id</th>
			<th>职业</th>
			<th>姓名</th>
			<th>工作</th>
			<th>备注</th>
			<th>电话号码</th>
			<th>工钱</th>
			<th>日期</th>
			<th>操作</th>
		</tr>
	<s:iterator value="list" status="status">
			<tr>
				<td><s:checkbox fieldValue="%{id}" name="ids" value="false"/></td>
				<td><s:property value="id"/></td>
				<td><s:property value="profession"/></td>
				<td><s:property value="name"/></td>
				<td><s:property value="work"/></td>
				<td><s:property value="remark"/></td>
				<td><s:property value="phone"/></td>
				<td><s:property value="money"/></td>
				<td><s:date name="date" format="yyyy-MM-dd,hh:mm"/></td>
				<td><s:a href="product_sendUpdate?update_id=%{id}">修改</s:a></td>
			</tr>
	</s:iterator>
	<tr style="background-color: white;">
		<td colspan="6" align=center><s:submit  style="margin: 5px;" id="delete" value="删除"/></td>
		<td colspan="4" align=center><Button  style="margin: 5px;"><s:a id="sendAdd" href="product_sendAdd">添加</s:a></Button></td>
	</tr>
			
	</table>
</s:form>

</center>

<script type="text/javascript">
	$(function(){
		$("tr:even").css("background","#d2d2d2");
		
	});
	
</script>

</body>
</html>

3、error.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>
<center>	
	<h2>页面出错了 <b style="color:red;"> --来自异世界的客户端</b></h2>
</center>	
</body>
</html>

4、default.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">
<%
	response.sendRedirect("product_showAll.action");
%>

四、其余:

WebContext 文件夹的 js-jquery1.7.js

WEB-INF 目录下的lib包一共 60个jar包 (58个ssh,一个sql,一个mysql )

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读