SSH整合【二】

16 篇文章 0 订阅
4 篇文章 0 订阅

SSH整合二
1、用户的登录、注册、查询所有用户
2、对文章进行增删查改
3、树形菜单
在这里插入图片描述

代码如下:
我们SSH整合【一】的就不加在上面了
Article:

package com.xzy.articles.biz;

import java.util.List;

import com.xzy.articles.entity.Article;
import com.xzy.base.util.PageBean;

public interface ArticleBiz {
	public int add(Article article);
	public void del(Article article);
	public List<Article> list(Article article,PageBean pageBean);
	public void edit(Article article);
}

ArticleBizImpl.java

package com.xzy.articles.biz.impl;

import java.util.List;

import com.xzy.articles.biz.ArticleBiz;
import com.xzy.articles.dao.ArticleDao;
import com.xzy.articles.entity.Article;
import com.xzy.base.util.PageBean;

public class ArticleBizImpl implements ArticleBiz {
	
	private ArticleDao articleDao;
	
	public ArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	@Override
	public int add(Article article) {
		return articleDao.add(article);
	}

	@Override
	public void del(Article article) {
		articleDao.del(article);
	}
	
	@Override
	public List<Article> list(Article article,PageBean pageBean) {
		return articleDao.list(article, pageBean);
	}

	@Override
	public void edit(Article article) {
		articleDao.edit(article);
	}

}

package com.xzy.articles.entity;

import com.xzy.base.entity.BaseEntity;

public class Article extends BaseEntity {
	
	private static final long serialVersionUID = -6409101675377070678L;
	
	private Integer id;
	private String title;
	private String body;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer 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;
	}
	
	public Article() {
		super();
	}
	public Article(Integer id, String title, String body) {
		super();
		this.id = id;
		this.title = title;
		this.body = body;
	}
	@Override
	public String toString() {
		return "Article [id=" + id + ", title=" + title + ", body=" + body + "]";
	}
}

Article.hbm.xml

<?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 table="t_vue_articles" name="com.xzy.articles.entity.Article">
		<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>

ArticleAction.java

package com.xzy.articles.web;

import java.util.List;

import com.opensymphony.xwork2.ModelDriven;
import com.xzy.articles.biz.ArticleBiz;
import com.xzy.articles.entity.Article;
import com.xzy.base.util.PageBean;
import com.xzy.base.web.BaseAction;

public class ArticleAction extends BaseAction implements ModelDriven<Article>{
	private static final long serialVersionUID = 1L;
	private ArticleBiz articleBiz;
	private Article article=new Article();
	private PageBean pageBean;
	
	public ArticleBiz getArticleBiz() {
		return articleBiz;
	}

	public void setArticleBiz(ArticleBiz articleBiz) {
		this.articleBiz = articleBiz;
	}

	/**
	 * 增加
	 * @return
	 */
	public String add() {
		int add = articleBiz.add(article);
		return null;
	}
	
	public String list() {
		PageBean pageBean = new PageBean();
		pageBean.setRequest(request);
		List<Article> list = articleBiz.list(article,pageBean);
		for (Article ar : list) {
			System.out.println(ar);
		}
		return null;
	}
	
	public String edit() {
		articleBiz.edit(article);
		return null;
	}
	
	public String del() {
		articleBiz.del(article);
		return null;
	}
	
	@Override
	public Article getModel() {
		return article;
	}
}

TreeNode

package com.xzy.treenode.entity;

import java.util.HashSet;
import java.util.Set;

import com.xzy.base.entity.BaseEntity;

public class TreeNode extends BaseEntity{
	private static final long serialVersionUID = 1L;
	private Integer nodeId;
	private String nodeName;
	private Integer treeNodeType;
	private Integer position;
	private String url;
	private TreeNode parent;
	private Set<TreeNode> children = new HashSet<TreeNode>();
	private Integer initChildren = 0;

	public Integer getNodeId() {
		return nodeId;
	}

	public void setNodeId(Integer nodeId) {
		this.nodeId = nodeId;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public Integer getTreeNodeType() {
		return treeNodeType;
	}

	public void setTreeNodeType(Integer treeNodeType) {
		this.treeNodeType = treeNodeType;
	}

	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public TreeNode getParent() {
		return parent;
	}

	public void setParent(TreeNode parent) {
		this.parent = parent;
	}

	public Set<TreeNode> getChildren() {
		return children;
	}

	public void setChildren(Set<TreeNode> children) {
		this.children = children;
	}

	public Integer getInitChildren() {
		return initChildren;
	}

	public void setInitChildren(Integer initChildren) {
		this.initChildren = initChildren;
	}
}

TreeNode.hbm.xml

<?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.xzy.treenode.entity.TreeNode" table="t_vue_tree_node">
		<id name="nodeId" type="java.lang.Integer" column="tree_node_id">
			<generator class="increment" />
		</id>
		<property name="nodeName" type="java.lang.String"
			column="tree_node_name">
		</property>
		<property name="treeNodeType" type="java.lang.Integer"
			column="tree_node_type">
		</property>
		<property name="position" type="java.lang.Integer"
			column="position">
		</property>
		<property name="url" type="java.lang.String"
			column="url">
		</property>
		
		<many-to-one name="parent" class="com.xzy.treenode.entity.TreeNode" column="parent_node_id"/>
		
		<set name="children" cascade="save-update" inverse="true">
			<key column="parent_node_id"></key>
			<one-to-many class="com.xzy.treenode.entity.TreeNode"/>
		</set>
	</class>
</hibernate-mapping>

dao

package com.xzy.treenode.dao;

import java.util.List;

import javax.swing.tree.TreeNode;

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

import com.xzy.base.dao.BaseDao;

public class TreeNodeDao extends BaseDao {
	
	private static final long serialVersionUID = 1L;

	public List<TreeNode> list(){
		return (List<TreeNode>) this.getHibernateTemplate().execute(new HibernateCallback<List<TreeNode>>() {

			@Override
			public List<TreeNode> doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
					return session.createQuery("from TreeNode").list();
			}
		});
	}
}

biz:

package com.xzy.treenode.biz;

import java.util.List;

import javax.swing.tree.TreeNode;

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

TreeNodeBizImpl.java

package com.xzy.treenode.biz.impl;

import java.util.List;

import javax.swing.tree.TreeNode;

import com.xzy.treenode.biz.TreeNodeBiz;
import com.xzy.treenode.dao.TreeNodeDao;

public class TreeNodeBizImpl implements TreeNodeBiz{
	private TreeNodeDao treeNodeDao;
	
	public TreeNodeDao getTreeNodeDao() {
		return treeNodeDao;
	}

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

	@Override
	public List<TreeNode> list() {
		// TODO Auto-generated method stub
		return treeNodeDao.list();
	}

}

dao

package com.xzy.treenode.dao;

import java.util.List;

import javax.swing.tree.TreeNode;

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

import com.xzy.base.dao.BaseDao;

public class TreeNodeDao extends BaseDao {
	
	private static final long serialVersionUID = 1L;

	public List<TreeNode> list(){
		return (List<TreeNode>) this.getHibernateTemplate().execute(new HibernateCallback<List<TreeNode>>() {

			@Override
			public List<TreeNode> doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
					return session.createQuery("from TreeNode").list();
			}
		});
	}
}

web

package com.xzy.treenode.web;

import java.util.List;

import com.xzy.base.web.BaseAction;
import com.xzy.treenode.biz.TreeNodeBiz;
import com.xzy.treenode.entity.TreeNode;
import com.opensymphony.xwork2.ModelDriven;

public class TreeNodeAction extends BaseAction implements ModelDriven<TreeNode>{
	
	private static final long serialVersionUID = 1L;
	private TreeNodeBiz treenodeBiz;
	private TreeNode treeNode=new TreeNode();
	
	

	public TreeNodeBiz getTreenodeBiz() {
		return treenodeBiz;
	}

	public void setTreenodeBiz(TreeNodeBiz treenodeBiz) {
		this.treenodeBiz = treenodeBiz;
	}

	public String list() {
		List<TreeNode> list = treenodeBiz.list();
		for (TreeNode t : list) {
			System.out.println(t);
		}
		return null;
	}

	
	
	@Override
	public TreeNode getModel() {
		return treeNode;
	}
}

User

user.java

package com.xzy.user.entity;

import com.xzy.base.entity.BaseEntity;

public class User extends BaseEntity{
	
	private static final long serialVersionUID = -4701879345583373175L;
	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" 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 table="t_vue_user" name="com.xzy.user.entity.User">
		<id name="uname" type="java.lang.String" column="uname">
			<generator class="assigned"></generator>
		</id>
		<property name="pwd" type="java.lang.String" column="pwd"></property>
	</class>
</hibernate-mapping>

UserDao.java

package com.xzy.user.dao;

import java.util.List;

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

import com.xzy.base.dao.BaseDao;
import com.xzy.user.entity.User;

public class UserDao extends BaseDao{

	private static final long serialVersionUID = -6811855750915604463L;
	
	
	public int add(User user) {
		return (int) this.getHibernateTemplate().save(user);
	}
	
	/**
	 * 登录
	 */
	public User queryLogin(User user) {
		return  this.getHibernateTemplate().execute(new HibernateCallback<User>() {

			@Override
			public User doInHibernate(Session session) throws HibernateException {
				String hql="from User where uname='"+user.getUname()+"' and pwd='"+user.getPwd()+"'";
				 List<User> list = session.createQuery(hql).list();
				 if(list.size()==0) {
					 return null;
				 }
				return list.get(0);
			}
		});
	}
	/**
	 * 查所有
	 * @return
	 */
	public List<User> list() {
		return this.getHibernateTemplate().execute(new HibernateCallback<List<User>>() {

			@Override
			public List<User> doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				return session.createQuery("from User").list();
			}
		});
	
	}	
	
	
}	

user.biz/impl

package com.xzy.user.biz;

import java.util.List;

import com.xzy.user.entity.User;

public interface UserBiz {
	public List<User> list();
	public User queryLogin(User user);
	public int add(User user);
}

UserBizImpl.java

package com.xzy.user.biz.impl;

import java.util.List;

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

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

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

	@Override
	public List<User> list() {
		return userDao.list();
	}

	@Override
	public User queryLogin(User user) {
		return userDao.queryLogin(user);
	}

	@Override
	public int add(User user) {
		return userDao.add(user);
	}
}

UserAction.java

package com.xzy.user.web;

import java.util.List;

import com.opensymphony.xwork2.ModelDriven;
import com.xzy.base.web.BaseAction;
import com.xzy.user.biz.UserBiz;
import com.xzy.user.entity.User;

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

	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}
	/**
	 * 登录
	 * @return
	 */
	public String login() {
		User u = userBiz.queryLogin(user);
		System.out.println(u);
		if(u ==null) {
			
		}
		return null;
	}
	
	/**
	 * 注册
	 * @return
	 */
	public String register() {
		int n = userBiz.add(user);
		System.out.println(n);
		return null;
	}
	/**
	 * 查所有
	 * @return
	 */
	public String list() {
		List<User> list = this.userBiz.list();
		for (User u : list) {
			System.out.println(u);
		}
		return null;
	}
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}
}

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- status : 指定log4j本身的打印日志的级别.ALL< Trace < DEBUG < INFO < WARN < ERROR 
	< FATAL < OFF。 monitorInterval : 用于指定log4j自动重新配置的监测间隔时间,单位是s,最小是5s. -->
<Configuration status="WARN" monitorInterval="30">
	<Properties>
		<!-- 配置日志文件输出目录 ${sys:user.home} -->
		<Property name="LOG_HOME">/root/workspace/lucenedemo/logs</Property>
		<property name="ERROR_LOG_FILE_NAME">/root/workspace/lucenedemo/logs/error</property>
		<property name="WARN_LOG_FILE_NAME">/root/workspace/lucenedemo/logs/warn</property>
		<property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t-%L] %-5level %logger{36} - %msg%n</property>
	</Properties>

	<Appenders>
		<!--这个输出控制台的配置 -->
		<Console name="Console" target="SYSTEM_OUT">
			<!-- 控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
			<ThresholdFilter level="trace" onMatch="ACCEPT"
				onMismatch="DENY" />
			<!-- 输出日志的格式 -->
			<!-- %d{yyyy-MM-dd HH:mm:ss, SSS} : 日志生产时间 %p : 日志输出格式 %c : logger的名称 
				%m : 日志内容,即 logger.info("message") %n : 换行符 %C : Java类名 %L : 日志输出所在行数 %M 
				: 日志输出所在方法名 hostName : 本地机器名 hostAddress : 本地ip地址 -->
			<PatternLayout pattern="${PATTERN}" />
		</Console>

		<!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用 -->
		<!--append为TRUE表示消息增加到指定文件中,false表示消息覆盖指定的文件内容,默认值是true -->
		<File name="log" fileName="logs/test.log" append="false">
			<PatternLayout
				pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
		</File>
		<!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size, 则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
		<RollingFile name="RollingFileInfo" fileName="${LOG_HOME}/info.log"
			filePattern="${LOG_HOME}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
			<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
			<ThresholdFilter level="info" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout
				pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
			<Policies>
				<!-- 基于时间的滚动策略,interval属性用来指定多久滚动一次,默认是1 hour。 modulate=true用来调整时间:比如现在是早上3am,interval是4,那么第一次滚动是在4am,接着是8am,12am...而不是7am. -->
				<!-- 关键点在于 filePattern后的日期格式,以及TimeBasedTriggeringPolicy的interval, 日期格式精确到哪一位,interval也精确到哪一个单位 -->
				<!-- log4j2的按天分日志文件 : info-%d{yyyy-MM-dd}-%i.log -->
				<TimeBasedTriggeringPolicy interval="1"
					modulate="true" />
				<!-- SizeBasedTriggeringPolicy:Policies子节点, 基于指定文件大小的滚动策略,size属性用来定义每个日志文件的大小. -->
				<!-- <SizeBasedTriggeringPolicy size="2 kB" /> -->
			</Policies>
		</RollingFile>

		<RollingFile name="RollingFileWarn" fileName="${WARN_LOG_FILE_NAME}/warn.log"
			filePattern="${WARN_LOG_FILE_NAME}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="warn" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout
				pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="2 kB" />
			</Policies>
			<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
			<DefaultRolloverStrategy max="20" />
		</RollingFile>

		<RollingFile name="RollingFileError" fileName="${ERROR_LOG_FILE_NAME}/error.log"
			filePattern="${ERROR_LOG_FILE_NAME}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd-HH-mm}-%i.log">
			<ThresholdFilter level="error" onMatch="ACCEPT"
				onMismatch="DENY" />
			<PatternLayout
				pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
			<Policies>
				<!-- log4j2的按分钟 分日志文件 : warn-%d{yyyy-MM-dd-HH-mm}-%i.log -->
				<TimeBasedTriggeringPolicy interval="1"
					modulate="true" />
				<!-- <SizeBasedTriggeringPolicy size="10 MB" /> -->
			</Policies>
		</RollingFile>

	</Appenders>

	<!--然后定义logger,只有定义了logger并引入的appender,appender才会生效 -->
	<Loggers>
		<!--过滤掉spring和mybatis的一些无用的DEBUG信息 -->
		<logger name="org.springframework" level="INFO"></logger>
		<logger name="org.mybatis" level="INFO"></logger>

		<!-- 第三方日志系统 -->
		<logger name="org.springframework" level="ERROR" />
		<logger name="org.hibernate" level="ERROR" />
		<logger name="org.apache.struts2" level="ERROR" />
		<logger name="com.opensymphony.xwork2" level="ERROR" />
		<logger name="org.jboss" level="ERROR" />


		<!-- 配置日志的根节点 -->
		<root level="all">
			<appender-ref ref="Console" />
			<appender-ref ref="RollingFileInfo" />
			<appender-ref ref="RollingFileWarn" />
			<appender-ref ref="RollingFileError" />
		</root>

	</Loggers>

</Configuration>

spring-articles.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.xzy.articles.dao.ArticleDao" parent="baseDao" id="articleDao">
	</bean>
	<bean class="com.xzy.articles.biz.impl.ArticleBizImpl" parent="baseBiz"
		id="articleBiz">
		<property name="articleDao" ref="articleDao"></property>
	</bean>
	<bean class="com.xzy.articles.web.ArticleAction" id="articleAction"
		parent="baseAction">
		<property name="articleBiz" ref="articleBiz"></property>
	</bean>
</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">

	<!-- 1、注册jdbc相关的配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 2、配置数据库连接池C3P0 -->
	<!-- 注册数据库连接文件db.properties -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 配置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相关信息 -->
	<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/xzy/book/entity/Book.hbm.xml</value>
				<value>com/xzy/user/entity/User.hbm.xml</value>
				<value>com/xzy/treenode/entity/TreeNode.hbm.xml</value>
				<value>com/xzy/articles/entity/Article.hbm.xml</value>
				
			</list>
		</property>
	</bean>

	<!-- 4、配置事务 -->
	<!--声明式事务配置开始 -->
	<!-- 
		静态代理:
			一个代理对象->一个目标对象
			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>
	<!-- 声明式事务配置结束 -->

	<!-- 5、配置HibernateTemplate -->
	<bean class="org.springframework.orm.hibernate5.HibernateTemplate" id="hibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 6、分模块开发(只配置base模块) -->
	<bean class="com.xzy.base.entity.BaseEntity" abstract="true" id="baseEntity"></bean>
	<bean class="com.xzy.base.dao.BaseDao" abstract="true" id="baseDao" >
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<bean class="com.xzy.base.biz.BaseBiz" abstract="true" id="baseBiz"></bean>
	<bean class="com.xzy.base.web.BaseAction" abstract="true" id="baseAction"></bean>
</beans>

配置胶水:
在这里插入图片描述
spring-treenode.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.xzy.treenode.dao.TreeNodeDao" parent="baseDao" id="treeNodeDao">
	</bean>
	<bean class="com.xzy.treenode.biz.impl.TreeNodeBizImpl" parent="baseBiz" id="treenodeBiz">
		<property name="treeNodeDao" ref="treeNodeDao"></property>
	</bean>
	<bean class="com.xzy.treenode.web.TreeNodeAction" id="treenodeAction" parent="baseAction">
		<property name="treenodeBiz" ref="treenodeBiz"></property>
	</bean>
</beans>

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.xzy.user.dao.UserDao" parent="baseDao" id="userDao">
	</bean>
	<bean class="com.xzy.user.biz.impl.UserBizImpl" parent="baseBiz"
		id="userBiz">
		<property name="userDao" ref="userDao"></property>
	</bean>
	<bean class="com.xzy.user.web.UserAction" id="userAction"
		parent="baseAction">
		<property name="userBiz" ref="userBiz"></property>
	</bean>
</beans>

struts-articles.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="articles" extends="base" namespace="/articles">
	<!--这里的class不再是全路径了,而是spring所管理的bean的ID  -->
		<action name="/article_*" class="articleAction" method="{1}">
		</action>
	</package>
</struts>

struts-treenode.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="treenode" extends="base" namespace="/treenode">
	<!--这里的class不再是全路径了,而是spring所管理的bean的ID  -->
		<action name="/node_*" class="treenodeAction" method="{1}">
		</action>
	</package>
</struts>

struts-user.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="user" extends="base" namespace="/user">
	<!--这里的class不再是全路径了,而是spring所管理的bean的ID  -->
		<action name="/user_*" class="userAction" method="{1}">
		</action>
	</package>
</struts>

struts.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>
	<include file="struts-default.xml"></include>
	<include file="struts-base.xml"></include>
	<include file="struts-sy.xml"></include>
	<include file="struts-user.xml" />
	<include file="struts-articles.xml" />
	<include file="struts-treenode.xml" />
</struts>

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

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值