spring之ssh拓展


本篇在 spring之ssh整合的基础上进行拓展,为接下来我们学习vue有关。

Vue课程知识的后台

代码演示
spring-conext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<!-- spring与hibernate整合的配置文件 -->
	<import resource="spring-hibernate.xml" />
	<!-- 分模块开发的书籍模块 book模块 -->
	<import resource="spring-book.xml" />
	<!-- 分模块开发的模块 user模块 -->
	<import resource="spring-user.xml" />
	<!-- 分模块开发的模块 treenode模块 -->
	<import resource="spring-treeNode.xml" />
	<!-- 分模块开发的模块 article模块 -->
	<import resource="spring-articles.xml"/>

</beans>		

spring-hibernate.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<!-- hibernate.cfg.xml -->
	<!-- 1.注册数据库连接信息文件db.properties -->
	<context:property-placeholder
		location="classpath:db.properties" />
	<!-- 2.配置数据库连接池(数据源) c3p0,dbcp2,druid -->
	<!-- 配置c3p0连接池 -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${db.username}"></property>
		<property name="password" value="${db.password}"></property>
		<property name="driverClass" value="${db.driverClass}"></property>
		<property name="jdbcUrl" value="${db.jdbcUrl}"></property>

		<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize"
			value="${db.initialPoolSize}"></property>
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="${db.maxPoolSize}"></property>
		<!--连接池中保留的最小连接数。 -->
		<property name="minPoolSize" value="${db.minPoolSize}" />
		<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="${db.maxIdleTime}" />

		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement"
			value="${db.acquireIncrement}" />

		<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。 
			所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 
			0 -->
		<property name="maxStatements" value="${db.maxStatements}" />

		<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
		<property name="idleConnectionTestPeriod"
			value="${db.idleConnectionTestPeriod}" />

		<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
		<property name="acquireRetryAttempts"
			value="${db.acquireRetryAttempts}" />

		<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。 
			如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
		<property name="breakAfterAcquireFailure"
			value="${db.breakAfterAcquireFailure}" />

		<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod 
			或automaticTestTable 等方法来提升连接测试的性能。Default: false -->
		<property name="testConnectionOnCheckout"
			value="${db.breakAfterAcquireFailure}" />
	</bean>

	<!-- 3.通过数据源然后去创建sessionfactory -->
	<!-- 3、配置sessionfactory相关信息 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<!-- hibernate相关属性 -->
		<property name="hibernateProperties">
			<props>
				<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!--spring与Hibernate集成无法显示sql语句问题,请见集成后hibernate无法显示sql语句.txt -->
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
		<!-- 实体映射文件 -->
		<property name="mappingResources">
			<list>
				<value>com/myy/book/entity/Book.hbm.xml</value>
				<value>com/myy/user/entity/User.hbm.xml</value>
				<value>com/myy/tree/entity/TreeNode.hbm.xml</value>
				<value>com/myy/articles/entity/Articles.hbm.xml</value>
			</list>
		</property>
	</bean>

	<!-- 4.获取session(hibernateTemplete) -->
	<bean class="org.springframework.orm.hibernate5.HibernateTemplate"
		id="hibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 5.配置事务 -->
	<!-- 采用了动态代理 静态代理与动态代理区别? 静态代理:一个代理对象只代理了一个目标 动态代理:一个代理能够代理多个目标(代理=目标+切面+通知) 
		切面是连接点的集合 -->
	<!--声明式事务配置开始 -->
	<!-- 静态代理: 一个代理对象->一个目标对象 BookProxy(BookBizImpl+myMethodBeforeAdvice)->bookBiz 
		OrderProxy(OrderBizImpl+myMethodBeforeAdvice2)-> OrderBiz 动态代理: 一个代理对象->多个目标对象 -->

	<!--1) 开启自动代理 -->
	<aop:aspectj-autoproxy />

	<!--2) 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!--3) 定义事务特性 -->
	<tx:advice id="txAdvice"
		transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />

			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />

			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />

			<tx:method name="load*" propagation="REQUIRED"
				read-only="true" />
			<tx:method name="list*" propagation="REQUIRED"
				read-only="true" />
			<tx:method name="select*" propagation="REQUIRED"
				read-only="true" />
			<tx:method name="query*" propagation="REQUIRED"
				read-only="true" />

			<tx:method name="do*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

	<!--4) 定义切入点 -->
	<aop:config>
		<!-- pointcut属性用来定义一个切入点,分成四个部分理解 [* ][*..][*Biz][.*(..)] -->
		<!-- A: 返回类型,*表示返回类型不限 -->
		<!-- B: 包名,*..表示包名不限 -->
		<!-- C: 类或接口名,*Biz表示类或接口必须以Biz结尾 -->
		<!-- D: 方法名和参数,*(..)表示方法名不限,参数类型和个数不限 -->
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* *..*Biz.*(..))" />
	</aop:config>
	<!-- 声明式事务配置结束 -->

	<!-- 6.配置基础模块 -->
	<bean class="com.myy.base.entity.BaseEntity" abstract="true"
		id="baseEntity"></bean>
	<bean class="com.myy.base.dao.BaseDao" abstract="true"
		id="baseDao">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<bean class="com.myy.base.biz.BaseBiz" abstract="true"
		id="baseBiz"></bean>
	<bean class="com.myy.base.web.BaseAction" abstract="true"
		id="baseAction"></bean>

</beans>		

struts-sy.xml

<?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>
	<package name="sy" extends="base" namespace="/sy">
		<action name="/book_*" class="bookAction" method="{1}">
			<result name="success">/index.jsp</result>
		</action>
	</package>

	<package name="vue" extends="base" namespace="/vue">
		<action name="/userAction_*" class="userAction" method="{1}">
			<result name="success">/index.jsp</result>
		</action>
		<action name="/treeNodeAction_*" class="treeNodeAction"
			method="{1}">
			<result name="success">/index.jsp</result>
		</action>
		<action name="/articlesAction_*" class="articlesAction" method="{1}"> 
			<result name="success">/index.jsp</result> </action>
	</package>
</struts>

程序代码分层

1.简单用户登录

在这里插入图片描述
User

package com.myy.user.entity;

import com.myy.base.entity.BaseEntity;

public class User extends BaseEntity {

	private static final long serialVersionUID = -5181458577389451159L;
	private String uname;
	private String pwd;

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@Override
	public String toString() {
		return "User [uname=" + uname + ", pwd=" + pwd + "]";
	}

}

User.hbm.xml映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2019-9-13 21:50:21 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.myy.user.entity.User" table="t_vue_user">
        <id name="uname" type="java.lang.String" column="uname">
            <generator class="assigned" />
        </id>
        <property name="pwd" type="java.lang.String" column="pwd" >
        </property>
    </class>
</hibernate-mapping>

UserDao

package com.myy.user.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;

import com.myy.base.dao.BaseDao;
import com.myy.base.util.StringUtils;
import com.myy.user.entity.User;

public class UserDao extends BaseDao{
	
	private static final long serialVersionUID = 3914988617367616839L;

	public List<User> login(User user) throws InstantiationException, IllegalAccessException {
		return this.getHibernateTemplate().execute(new HibernateCallback<List<User>>() {

			@Override
			public List<User> doInHibernate(Session session) throws HibernateException {
				Query query ;
				List list = new ArrayList ();
				//获取查询条件
				String uname = user.getUname();
				String pwd = user.getPwd();
				//判断是否为空
				if(StringUtils.isNotBlank(uname) && StringUtils.isNotBlank(pwd)) {
					//拼接hql
					query  = session.createQuery("from User where uname = :uname and pwd = :pwd");
					query.setParameter("uname", uname);
					query.setParameter("pwd", pwd);
					list = query.list();
				}
				return list;
			
			}
		});
		
	} 	
}


UserBiz接口

package com.myy.user.biz;

import java.util.List;

import com.myy.user.entity.User;

public interface UserBiz {
	public List<User> login(User user) throws InstantiationException, IllegalAccessException ;

}

UserBizImpl实现接口

package com.myy.user.biz.impl;

import java.util.List;

import com.myy.user.biz.UserBiz;
import com.myy.user.dao.UserDao;
import com.myy.user.entity.User;

public class UserBizImpl implements UserBiz{
	private UserDao userDao = new UserDao();
	
	
	
	public UserDao getUserDao() {
		return userDao;
	}



	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}



	@Override
	public List<User> login(User user) throws InstantiationException, IllegalAccessException {
		return this.userDao.login(user);
	}




}


web层UserAction

package com.myy.user.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myy.base.util.ResponseUtil;
import com.myy.base.web.BaseAction;
import com.myy.user.biz.UserBiz;
import com.myy.user.entity.User;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends BaseAction implements ModelDriven<User>{
	
	private static final long serialVersionUID = 7322676814532568645L;
	
	private User user = new User();
	private UserBiz userBiz;
	
	
	public UserBiz getUserBiz() {
		return userBiz;
	}


	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}


	public String login () throws  Exception {
			List<User> res = userBiz.login(user);
			if(res.size()==1) {
				this.code = 1;
				this.msg = "登录成功";
				this.result = res;
				
			}else {
				this.code = 0;
				this.msg = "登录失败";
				this.result = res;
			}
		Map<String, Object> map = new HashMap<String, Object>();
		ObjectMapper om = new ObjectMapper();
		map.put("msg", msg);
		map.put("result", result);
		map.put("code", code);
		ResponseUtil.write(response, om.writeValueAsString(map));
		return SUCCESS;
		
	}
	
	
	@Override
	public User getModel() {
		return user;
	}

}


spring-user.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

  <bean class="com.myy.user.dao.UserDao" id="userDao" parent="baseDao">
  </bean>
  <bean class="com.myy.user.biz.impl.UserBizImpl" id="userBiz" parent="baseBiz">
     <property name="userDao" ref="userDao"></property>
  </bean>
   <bean class="com.myy.user.web.UserAction" id="userAction" parent="baseAction">
  <property name="userBiz" ref="userBiz"></property>
  </bean>
</beans>		

spring-hibernate.xml实体映射文件

<!-- 实体映射文件 -->
		<property name="mappingResources">
			<list>
				  <value>com/myy/book/entity/Book.hbm.xml</value>
			      <value>com/myy/user/entity/User.hbm.xml</value>
			</list>
		</property>

配置到struts-sy.xml中

<package name="vue" extends="base" namespace="/vue">
		<action name="/userAction_*" class="userAction" method="{1}">
		<result name="success">/index.jsp</result>
		</action>
</package>

测试结果:
在这里插入图片描述

2.树形菜单显示

在这里插入图片描述
TreeNode

package com.myy.tree.entity;

import java.util.ArrayList;
import java.util.List;

import com.myy.base.entity.BaseEntity;

public class TreeNode extends BaseEntity{

	private static final long serialVersionUID = 7498450046537986252L;
	private int treenodeid;
	private String treenodename;
	private int treenodetype;
	private String url;
	private int position;
	private String icon;
	
	private List<TreeNode> children = new ArrayList<TreeNode>();
	
	public List<TreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}
	public int getTreenodeid() {
		return treenodeid;
	}
	public void setTreenodeid(int treenodeid) {
		this.treenodeid = treenodeid;
	}
	public String getTreenodename() {
		return treenodename;
	}
	public void setTreenodename(String treenodename) {
		this.treenodename = treenodename;
	}
	public int getTreenodetype() {
		return treenodetype;
	}
	public void setTreenodetype(int treenodetype) {
		this.treenodetype = treenodetype;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public int getPosition() {
		return position;
	}
	public void setPosition(int position) {
		this.position = position;
	}
	public String getIcon() {
		return icon;
	}
	public void setIcon(String icon) {
		this.icon = icon;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	@Override
	public String toString() {
		return "TreeNode [treenodeid=" + treenodeid + ", treenodename=" + treenodename + ", treenodetype="
				+ treenodetype + ", url=" + url + ", position=" + position + ", icon=" + icon + ", children=" + children
				+ "]";
	}
	
}

TreeNode.hbm.xml映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2019-9-19 8:53:09 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
   <class table="t_vue_tree_node" name="com.myy.tree.entity.TreeNode" >
		<id name="treenodeid" type="java.lang.Integer" column="tree_node_id">
			<generator class="increment"></generator>
		</id>
		
		<property name="treenodename" type="java.lang.String" column="tree_node_name"></property>
		<property name="treenodetype" type="java.lang.Integer" column="tree_node_type"></property>
	 <!-- <property name="parentnodeid" type="java.lang.Integer" column="parent_node_id"></property> -->
		<property name="url" type="java.lang.String" column="url"></property>
		<property name="position" type="java.lang.Integer" column="position"></property>
		<property name="icon" type="java.lang.String" column="icon"></property>
		
		<bag lazy="false" name="children" cascade="save-update" inverse="true">
			<key column="parent_node_id"></key>
			<one-to-many class="com.myy.tree.entity.TreeNode"/>
		</bag>
		
	</class>
</hibernate-mapping>

TreeNodeDao

package com.myy.tree.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;

import com.myy.base.dao.BaseDao;
import com.myy.tree.entity.TreeNode;

public class TreeNodeDao extends BaseDao{

	private static final long serialVersionUID = -5592721593637608135L;
    public List<TreeNode> list(){
		return this.getHibernateTemplate().execute(new HibernateCallback<List<TreeNode>>() {

			public List<TreeNode> doInHibernate(Session session) throws HibernateException {
				return session.createQuery("from TreeNode").list();
			}
		});
    	
    }
}

TreeNodeBiz

package com.myy.tree.biz;

import java.util.List;

import com.myy.tree.entity.TreeNode;

public interface TreeNodeBiz {
  public List<TreeNode> list();
}

TreeNodeBizImpl

package com.myy.tree.biz.impl;

import java.util.List;

import com.myy.tree.biz.TreeNodeBiz;
import com.myy.tree.dao.TreeNodeDao;
import com.myy.tree.entity.TreeNode;

public class TreeNodeBizImpl implements TreeNodeBiz {
	private TreeNodeDao treeNodeDao = new TreeNodeDao();

	public TreeNodeDao getTreeNodeDao() {
		return treeNodeDao;
	}

	public void setTreeNodeDao(TreeNodeDao treeNodeDao) {
		this.treeNodeDao = treeNodeDao;
	}

	public List<TreeNode> list() {
		return this.treeNodeDao.list();
	}

}

package com.myy.tree.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myy.base.util.ResponseUtil;
import com.myy.base.web.BaseAction;
import com.myy.tree.biz.TreeNodeBiz;
import com.myy.tree.entity.TreeNode;
import com.opensymphony.xwork2.ModelDriven;

public class TreeNodeAction extends BaseAction implements ModelDriven<TreeNode>{

	private static final long serialVersionUID = -7544260381100558404L;

	private TreeNode treeNode = new TreeNode();
	private TreeNodeBiz treeNodeBiz;
	

	public TreeNodeBiz getTreeNodeBiz() {
		return treeNodeBiz;
	}

	public void setTreeNodeBiz(TreeNodeBiz treeNodeBiz) {
		this.treeNodeBiz = treeNodeBiz;
	}
public String list() throws Exception {
		List<TreeNode> res = this.treeNodeBiz.list();

		this.msg = "操作成功";
		this.result = res;
		this.code = 1;
		Map<String, Object> map = new HashMap<String, Object>();
		ObjectMapper om = new ObjectMapper();
		map.put("msg", msg);
		map.put("result", result);
		map.put("code", code);
		ResponseUtil.write(response, om.writeValueAsString(map));
		return SUCCESS;
	}

	@Override
	public TreeNode getModel() {
		// TODO Auto-generated method stub
		return treeNode;
	}

}

spring-hibernate.xml配置实体映射文件

	<value>com/myy/tree/entity/TreeNode.hbm.xml</value>

spring-context.xml

<!-- 分模块开发的模块 treenode模块 -->
	<import resource="spring-treeNode.xml" />

struts-sy.xml

	<action name="/treeNodeAction_*" class="treeNodeAction"
			method="{1}">
			<result name="success">/index.jsp</result>
		</action>

测试结果:
在这里插入图片描述

3.Articles增删改查

Articles

package com.myy.articles.entity;

import com.myy.base.entity.BaseEntity;

public class Articles extends BaseEntity{

	private static final long serialVersionUID = 4901118592948757542L;
	private int id;
	private String title;
	private String body;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	@Override
	public String toString() {
		return "Articles [id=" + id + ", title=" + title + ", body=" + body + "]";
	}
	

}

映射文件Articles.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2019-9-19 10:36:20 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
   <class table="t_vue_articles" name="com.myy.articles.entity.Articles">
		<id name="id" type="java.lang.Integer" column="id">
			<generator class="increment"></generator>
		</id>
		
		<property name="title" type="java.lang.String" column="title"></property>
		<property name="body" type="java.lang.String" column="body"></property>
	</class>
</hibernate-mapping>

ArticlesDao

package com.myy.articles.dao;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;

import com.myy.articles.entity.Articles;
import com.myy.base.dao.BaseDao;
import com.myy.base.util.PageBean;

public class ArticlesDao extends BaseDao{

	private static final long serialVersionUID = -6783383497057922629L;

	public List<Articles> list(Articles Articles , PageBean pageBean) throws InstantiationException, IllegalAccessException {
		BaseDao obj = super.getClass().newInstance();
		return this.getHibernateTemplate().execute(new HibernateCallback<List<Articles>>() {
			@Override
			public List<Articles> doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				String hql = " from Articles";
				return obj.executeQuery(session, hql, null, pageBean);
			}
		});
	}
	
	public void add(Articles Articles) {
		this.getHibernateTemplate().save(Articles);
	}
	
	public void edit(Articles Articles) {
		this.getHibernateTemplate().update(Articles);
	}
	
	public void del(Articles Articles) {
		this.getHibernateTemplate().delete(Articles);
	}
}

ArticlesBiz

package com.myy.articles.biz;

import java.util.List;

import com.myy.articles.entity.Articles;
import com.myy.base.util.PageBean;

public interface ArticlesBiz {
public List<Articles> list(Articles articles , PageBean pageBean) throws InstantiationException, IllegalAccessException ;
	
	public void add(Articles articles);
	
	public void edit(Articles articles);
	
	public void del(Articles articles);
}

ArticlesBizImpl

package com.myy.articles.biz.Impl;

import java.util.List;

import com.myy.articles.biz.ArticlesBiz;
import com.myy.articles.dao.ArticlesDao;
import com.myy.articles.entity.Articles;
import com.myy.base.util.PageBean;

public class ArticlesBizImpl implements ArticlesBiz{
	private ArticlesDao articlesDao;
	

	public ArticlesDao getArticlesDao() {
		return articlesDao;
	}

	public void setArticlesDao(ArticlesDao articlesDao) {
		this.articlesDao = articlesDao;
	}

	@Override
	public List<Articles> list(Articles articles, PageBean pageBean)
			throws InstantiationException, IllegalAccessException {
		return this.articlesDao.list(articles, pageBean);
	}

	@Override
	public void add(Articles articles) {
		this.articlesDao.add(articles);
	}

	@Override
	public void edit(Articles articles) {
		this.articlesDao.edit(articles);
	}

	@Override
	public void del(Articles articles) {
		
	}

}

ArticlesAction

package com.myy.articles.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myy.articles.biz.ArticlesBiz;
import com.myy.articles.entity.Articles;
import com.myy.base.util.ResponseUtil;
import com.myy.base.web.BaseAction;
import com.opensymphony.xwork2.ModelDriven;

public class ArticlesAction extends BaseAction implements ModelDriven<Articles> {

	private static final long serialVersionUID = 1067758913885918921L;

	private ArticlesBiz articlesBiz;
	private Articles articles = new Articles();

	public ArticlesBiz getArticlesBiz() {
		return articlesBiz;
	}

	public void setArticlesBiz(ArticlesBiz articlesBiz) {
		this.articlesBiz = articlesBiz;
	}

	public String list() throws Exception {
		List<Articles> res = articlesBiz.list(articles, null);
		Map<String, Object> map = new HashMap<String, Object>();
		ObjectMapper om = new ObjectMapper();
		map.put("msg", "操作成功");
		map.put("result", res);
		map.put("code", 1);
		ResponseUtil.write(response, om.writeValueAsString(map));
		return SUCCESS;
	}

	public String add() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		ObjectMapper om = new ObjectMapper();
		try {
			this.articlesBiz.add(articles);
			map.put("msg", "操作成功");
			map.put("result", articles.getId());
			map.put("code", articles.getId());
		} catch (Exception e) {
			// TODO: handle exception
			map.put("msg", "操作失败");
			map.put("result", articles.getId());
			map.put("code", articles.getId());
		}
		ResponseUtil.write(response, om.writeValueAsString(map));
		return SUCCESS;
	}

	public String edit() throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		ObjectMapper om = new ObjectMapper();
		try {
			this.articlesBiz.edit(articles);
			map.put("msg", "操作成功");
			map.put("result", articles.getId());
			map.put("code", articles.getId());
		} catch (Exception e) {
			// TODO: handle exception
			map.put("msg", "操作失败");
			map.put("result", articles.getId());
			map.put("code", articles.getId());
		}
		ResponseUtil.write(response, om.writeValueAsString(map));
		return SUCCESS;
	}

	public String del() throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		ObjectMapper om = new ObjectMapper();
		try {
			this.articlesBiz.del(articles);
			map.put("msg", "操作成功");
			map.put("result", articles.getId());
			map.put("code", articles.getId());

		} catch (Exception e) {
			map.put("msg", "操作失败");
			map.put("result", articles.getId());
			map.put("code", 0);
		}
		ResponseUtil.write(response, om.writeValueAsString(map));
		return SUCCESS;
	}

	@Override
	public Articles getModel() {
		return articles;
	}

}

spring-context.xml

     <!-- 分模块开发的模块 article模块 -->
	<import resource="spring-articles.xml"/>

spring-hibernate.xml实体映射文件

	<value>com/myy/articles/entity/Articles.hbm.xml</value>

struts-sy.xml

          <action name="/articlesAction_*" class="articlesAction" method="{1}"> 
			<result name="success">/index.jsp</result> </action>

测试结果:
新增:
在这里插入图片描述
修改:
在这里插入图片描述
删除:
在这里插入图片描述
查询:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值