题目要求
实现CostType和CostLog的增删改查
CostType 包含 costTypeId
costTypeName
CostLog 包含 costLogId
costLogTime(自动生成)
costType(更加CostType选择)
costLogPrice
comment
目录结构
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Experiment0424</title>
<script type="text/javascript">
location.href="<s:url action="entry" namespace="/cost"/>";
</script>
</head>
<body>
</body>
</html>
main.jsp
<%--
Created by IntelliJ IDEA.
User: SwingHe
Date: 2017/4/24
Time: 08:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Experiment0424</title>
<script type="text/javascript">
function $(id) {
return document.getElementById(id);
}
function removeCostType(costTypeId, costTypeName){
if(confirm("您确认要删除"+costTypeName+"吗?")){
location.href="<s:url action="removeCostType" namespace="/cost"/>?costType.costTypeId="+costTypeId;
}
}
function updateCostType(costTypeId){
var button = $("TB"+costTypeId);
var item = $("TN"+costTypeId);
var str = button.innerHTML;
if (str == "修改"){
item.readOnly = false;
item.style = "border:1;";
button.innerHTML = "确定";
}
else if (str == "确定"){
var costTypeName = item.value;
location.href="<s:url action="updateCostType" namespace="/cost"/>?costType.costTypeId="+costTypeId+"&costType.costTypeName="+costTypeName;
}
}
function removeCostLog(costLogId){
if(confirm("您确认要删除"+costLogId+"吗?")){
location.href="<s:url action="removeCostLog" namespace="/cost"/>?costLog.costLogId="+costLogId;
}
}
function updateCostLog(costLogId){
var button = $("LB"+costLogId);
var item = $("LP"+costLogId);
var item1 = $("LC"+costLogId);
var str = button.innerHTML;
if (str == "修改"){
item.readOnly = false;
item1.readOnly = false;
item.style = "border:1;";
item1.style = "border:1;";
button.innerHTML = "确定";
}
else if (str == "确定"){
var costLogPrice = item.value;
var costLogComment = item1.value;
location.href="<s:url action="updateCostLog" namespace="/cost"/>?costLog.costLogId="
+costLogId+"&costLog.costLogPrice="+costLogPrice+"&costLog.comment="+costLogComment;
}
}
</script>
</head>
<body>
<div id="CostType">
<h3>消费类别</h3>
<s:form action="addCostType" namespace="/cost">
<div>
<label>消费类别名称:</label>
<s:textfield name="costType.costTypeName"></s:textfield>
</div>
<div>
<s:submit value="添加消费类别"></s:submit>
</div>
</s:form>
<table border="1" cellspacing="0" cellpadding="5px">
<tr>
<th>商品类别编号</th>
<th>商品类别名称</th>
<th>操作</th>
</tr>
<s:iterator value="costTypes">
<tr>
<td><s:property value="costTypeId"/></td>
<td><input value="<s:property value="costTypeName"/>" readonly="true" style="border:0;" id="TN<s:property value="costTypeId"/>"></td>
<td>
<button οnclick="updateCostType('<s:property value="costTypeId"/>');" id="TB<s:property value="costTypeId"/>">修改</button>
<button οnclick="removeCostType('<s:property value="costTypeId"/>','<s:property value="costTypeName"/>')">删除</button>
</td>
</tr>
</s:iterator>
</table>
</div>
<hr>
<div id = "CostLog">
<h3>消费条目</h3>
<s:form action="addCostLog" namespace="/cost">
<div>
<label>消费金额:</label>
<s:textfield name="costLog.costLogPrice"></s:textfield>
</div>
<div>
<label>消费类别:</label>
<s:select name="costTypeId" list="costTypes" listKey="costTypeId" listValue="costTypeName"></s:select>
</div>
<div>
<label>消费备注:</label>
<s:textfield name="costLog.comment"></s:textfield>
</div>
<div>
<s:submit value="添加消费条目"></s:submit>
</div>
</s:form>
<table border="1" cellspacing="0" cellpadding="5px">
<tr>
<th>商品条目编号</th>
<th>发生时间</th>
<th>消费金额</th>
<th>消费类型</th>
<th>备注说明</th>
<th>操作</th>
</tr>
<s:iterator value="costLogs">
<tr>
<td><s:property value="costLogId"/></td>
<td><s:property value="costLogTime"/></td>
<td><input value="<s:property value="costLogPrice"/>" readonly="true" style="border:0;" id="LP<s:property value="costLogId"/>"></td>
<td><s:property value="costType.costTypeName"/></td>
<td><input value="<s:property value="comment"/>" readonly="true" style="border:0;" id="LC<s:property value="costLogId"/>"></td>
<td>
<button οnclick="updateCostLog('<s:property value="costLogId"/>');" id="LB<s:property value="costLogId"/>">修改</button>
<button οnclick="removeCostLog('<s:property value="costLogId"/>','<s:property value="costLogId"/>')">删除</button>
</td>
</tr>
</s:iterator>
</table>
</div>
</body>
</html>
CostAction.java
/**
*
*/
package edu.fjnu.hsh.action;
import com.opensymphony.xwork2.ActionSupport;
import edu.fjnu.hsh.domain.CostLog;
import edu.fjnu.hsh.domain.CostType;
import edu.fjnu.hsh.service.CostLogService;
import edu.fjnu.hsh.service.CostTypeService;
import java.util.List;
public class CostAction extends ActionSupport {
private CostType costType;
private CostLog costLog;
private String costTypeId;
private List<CostType> costTypes;
private List<CostLog> costLogs;
public CostType getCostType() {
return costType;
}
public void setCostType(CostType costType) {
this.costType = costType;
}
public CostLog getCostLog() {
return costLog;
}
public void setCostLog(CostLog costLog) {
this.costLog = costLog;
}
public List<CostType> getCostTypes() {
return costTypes;
}
public void setCostTypes(List<CostType> costTypes) {
this.costTypes = costTypes;
}
public List<CostLog> getCostLogs() {
return costLogs;
}
public void setCostLogs(List<CostLog> costLogs) {
this.costLogs = costLogs;
}
public String getCostTypeId() {
return costTypeId;
}
public void setCostTypeId(String costTypeId) {
this.costTypeId = costTypeId;
}
private void config(){
costType = null;
costLog = null;
CostTypeService costTypeService = new CostTypeService();
costTypes = costTypeService.allCostType();
CostLogService costLogService = new CostLogService();
costLogs = costLogService.allCostLog();
}
public String addCostType(){
CostTypeService costTypeService = new CostTypeService();
costTypeService.addCostType(costType.getCostTypeName());
config();
return "loadAll";
}
public String addCostLog(){
CostLogService costLogService = new CostLogService();
costLogService.addCostLog(costTypeId, costLog);
costLog = null;
config();
return "loadAll";
}
public String entry(){
config();
return "SUCCESS";
}
public String removeCostType(){
CostTypeService costTypeService = new CostTypeService();
costTypeService.removeCostType(costType.getCostTypeId());
config();
return "loadAll";
}
public String removeCostLog(){
CostLogService costLogService = new CostLogService();
costLogService.removeCostLog(costLog.getCostLogId());
config();
return "loadAll";
}
public String updateCostType(){
CostTypeService costTypeService = new CostTypeService();
costTypeService.updateCostType(costType);
costType = null;
config();
return "loadAll";
}
public String updateCostLog(){
CostLogService costLogService = new CostLogService();
costLogService.updateCostLog(costLog);
config();
return "loadAll";
}
}
<!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>
<!--Required-->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--Optional-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="edu/fjnu/hsh/domain/CostType.hbm.xml"/>
<mapping resource="edu/fjnu/hsh/domain/CostLog.hbm.xml"/>
</session-factory>
</hibernate-configuration>
dao层
package edu.fjnu.hsh.dao;
import edu.fjnu.hsh.domain.CostType;
import java.util.List;
public interface CostTypeDao {
void addCostType(CostType costType);
void deleteCostType(int costTypeId);
CostType searchCostType(String costTypeId);
void updateCostType(CostType costType);
List<CostType> allCostType();
}
package edu.fjnu.hsh.dao;
import edu.fjnu.hsh.domain.CostLog;
import edu.fjnu.hsh.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
public class CostLogDaoImpl implements CostLogDao {
@Override
public void addCostLog(CostLog costLog) {
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
session.save(costLog);
transaction.commit();
}
@Override
public void updateCostLog(CostLog costLog) {
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
CostLog tmp = session.get(CostLog.class, costLog.getCostLogId());
tmp.setCostLogPrice(costLog.getCostLogPrice());
tmp.setComment(costLog.getComment());
session.saveOrUpdate(tmp);
transaction.commit();
}
@Override
public List<CostLog> allCostLog() {
String hql = "from CostLog";
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
List<CostLog> costLogs = session.createQuery(hql).list();
transaction.commit();
return costLogs;
}
@Override
public void deleteCostLog(int costLogId) {
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
CostLog costLog = session.load(CostLog.class, costLogId);
session.delete(costLog);
transaction.commit();
}
}
package edu.fjnu.hsh.dao;
import edu.fjnu.hsh.domain.CostType;
import java.util.List;
public interface CostTypeDao {
void addCostType(CostType costType);
void deleteCostType(int costTypeId);
CostType searchCostType(String costTypeId);
void updateCostType(CostType costType);
List<CostType> allCostType();
}
package edu.fjnu.hsh.dao;
import edu.fjnu.hsh.domain.CostType;
import edu.fjnu.hsh.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
public class CostTypeDaoImpl implements CostTypeDao {
@Override
public void addCostType(CostType costType) {
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
session.save(costType);
transaction.commit();
}
@Override
public void deleteCostType(int costTypeId) {
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
CostType costType = session.load(CostType.class, costTypeId);
session.delete(costType);
transaction.commit();
}
@Override
public CostType searchCostType(String costTypeId) {
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
CostType costType = session.get(CostType.class, Integer.parseInt(costTypeId));
transaction.commit();
return costType;
}
@Override
public void updateCostType(CostType costType) {
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
CostType tmp = session.get(CostType.class, costType.getCostTypeId());
tmp.setCostTypeName(costType.getCostTypeName());
session.saveOrUpdate(tmp);
transaction.commit();
}
@Override
public List<CostType> allCostType() {
String hql = "from CostType";
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
List<CostType> costTypes = session.createQuery(hql).list();
transaction.commit();
return costTypes;
}
}
CostLog.java
package edu.fjnu.hsh.domain;
public class CostLog {
private Integer costLogId;
private float costLogPrice;
private String costLogTime;
private CostType costType;
private String comment;
public Integer getCostLogId() {
return costLogId;
}
public void setCostLogId(Integer costLogId) {
this.costLogId = costLogId;
}
public float getCostLogPrice() {
return costLogPrice;
}
public void setCostLogPrice(float costLogPrice) {
this.costLogPrice = costLogPrice;
}
public String getCostLogTime() {
return costLogTime;
}
public void setCostLogTime(String costLogTime) {
this.costLogTime = costLogTime;
}
public CostType getCostType() {
return costType;
}
public void setCostType(CostType costType) {
this.costType = costType;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
@Override
public String toString() {
return "CostLog{" +
"costLogId='" + costLogId + '\'' +
", costLogName='" + costLogPrice + '\'' +
", costLogTime='" + costLogTime + '\'' +
", comment='" + comment + '\'' +
'}';
}
}
CostLog.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="edu.fjnu.hsh.domain">
<class name="CostLog" table="cost_log">
<id name="costLogId" column="cost_log_id" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="costLogPrice" column="cost_log_price" type="java.lang.Float" not-null="true"></property>
<property name="costLogTime" column="cost_log_time" type="java.lang.String" not-null="true" length="50"></property>
<property name="comment" column="cost_log_comment" type="java.lang.String" length="50"></property>
<many-to-one name="costType" class="CostType" column="cost_type_id" cascade="none"></many-to-one>
</class>
</hibernate-mapping>
CostType.java
package edu.fjnu.hsh.domain;
import java.util.Set;
public class CostType {
private Integer costTypeId;
private String costTypeName;
private Set<CostLog> costLogs;
public Integer getCostTypeId() {
return costTypeId;
}
public void setCostTypeId(Integer costTypeId) {
this.costTypeId = costTypeId;
}
public String getCostTypeName() {
return costTypeName;
}
public void setCostTypeName(String costTypeName) {
this.costTypeName = costTypeName;
}
public Set<CostLog> getCostLogs() {
return costLogs;
}
public void setCostLogs(Set<CostLog> costLogs) {
this.costLogs = costLogs;
}
@Override
public String toString() {
return "CostLog{" +
"costTypeId='" + costTypeId + '\'' +
", costTypeName='" + costTypeName + '\'' +
'}';
}
}
CostType.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="edu.fjnu.hsh.domain">
<class name="CostType" table="cost_type">
<id name="costTypeId" column="cost_type_id" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="costTypeName" column="cost_type_name" type="java.lang.String" not-null="true" length="20"></property>
<set name="costLogs" table="cost_log" cascade="all" inverse="true">
<key>
<column name="cost_type_id" not-null="true" />
</key>
<one-to-many class="CostLog" />
</set>
</class>
</hibernate-mapping>
package edu.fjnu.hsh.service;
import edu.fjnu.hsh.dao.CostLogDao;
import edu.fjnu.hsh.dao.CostLogDaoImpl;
import edu.fjnu.hsh.dao.CostTypeDao;
import edu.fjnu.hsh.dao.CostTypeDaoImpl;
import edu.fjnu.hsh.domain.CostLog;
import edu.fjnu.hsh.domain.CostType;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class CostLogService {
public List<CostLog> allCostLog() {
CostLogDao costLogDao = new CostLogDaoImpl();
return costLogDao.allCostLog();
}
public void addCostLog(String costTypeId, CostLog costLog) {
CostTypeDao costTypeDao = new CostTypeDaoImpl();
CostType costType = costTypeDao.searchCostType(costTypeId);
CostLogDao costLogDao = new CostLogDaoImpl();
costLog.setCostType(costType);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String time = df.format(new Date());// new Date()为获取当前系统时间
costLog.setCostLogTime(time);
costLogDao.addCostLog(costLog);
}
public void removeCostLog(int costLogId){
CostLogDao costLogDao = new CostLogDaoImpl();
costLogDao.deleteCostLog(costLogId);
}
public void updateCostLog(CostLog costLog){
CostLogDao costLogDao = new CostLogDaoImpl();
costLogDao.updateCostLog(costLog);
}
}
CostTypeService.java
package edu.fjnu.hsh.service;
import edu.fjnu.hsh.dao.CostTypeDao;
import edu.fjnu.hsh.dao.CostTypeDaoImpl;
import edu.fjnu.hsh.domain.CostType;
import java.util.List;
public class CostTypeService {
public void addCostType(String costTypeName){
CostTypeDao costTypeDao = new CostTypeDaoImpl();
CostType costType = new CostType();
costType.setCostTypeName(costTypeName);
costTypeDao.addCostType(costType);
}
public List<CostType> allCostType() {
CostTypeDao costTypeDao = new CostTypeDaoImpl();
return costTypeDao.allCostType();
}
public void removeCostType(int costTypeId){
CostTypeDao costTypeDao = new CostTypeDaoImpl();
costTypeDao.deleteCostType(costTypeId);
}
public void updateCostType(CostType costType){
CostTypeDao costTypeDao = new CostTypeDaoImpl();
costTypeDao.updateCostType(costType);
}
}
HibernateUtil.java
package edu.fjnu.hsh.service;
import edu.fjnu.hsh.dao.CostTypeDao;
import edu.fjnu.hsh.dao.CostTypeDaoImpl;
import edu.fjnu.hsh.domain.CostType;
import java.util.List;
public class CostTypeService {
public void addCostType(String costTypeName){
CostTypeDao costTypeDao = new CostTypeDaoImpl();
CostType costType = new CostType();
costType.setCostTypeName(costTypeName);
costTypeDao.addCostType(costType);
}
public List<CostType> allCostType() {
CostTypeDao costTypeDao = new CostTypeDaoImpl();
return costTypeDao.allCostType();
}
public void removeCostType(int costTypeId){
CostTypeDao costTypeDao = new CostTypeDaoImpl();
costTypeDao.deleteCostType(costTypeId);
}
public void updateCostType(CostType costType){
CostTypeDao costTypeDao = new CostTypeDaoImpl();
costTypeDao.updateCostType(costType);
}
}
struts.xml
package edu.fjnu.hsh.service;
import edu.fjnu.hsh.dao.CostTypeDao;
import edu.fjnu.hsh.dao.CostTypeDaoImpl;
import edu.fjnu.hsh.domain.CostType;
import java.util.List;
public class CostTypeService {
public void addCostType(String costTypeName){
CostTypeDao costTypeDao = new CostTypeDaoImpl();
CostType costType = new CostType();
costType.setCostTypeName(costTypeName);
costTypeDao.addCostType(costType);
}
public List<CostType> allCostType() {
CostTypeDao costTypeDao = new CostTypeDaoImpl();
return costTypeDao.allCostType();
}
public void removeCostType(int costTypeId){
CostTypeDao costTypeDao = new CostTypeDaoImpl();
costTypeDao.deleteCostType(costTypeId);
}
public void updateCostType(CostType costType){
CostTypeDao costTypeDao = new CostTypeDaoImpl();
costTypeDao.updateCostType(costType);
}
}
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_3_1.xsd"
version="3.1">
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern> <!-- 对任何WEB访问地址,都将执行struts2过滤 -->
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>