配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring监听器读取Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/beans.xml</param-value>
</context-param>
<!-- Spring解决延迟加载的问题 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 设置编码 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置struts的核心过滤器 -->
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
复制代码
配置jdbc.properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh_config?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
jdbc.initialSize=3
jdbc.minIdle=3
jdbc.maxActive=20
jdbc.maxWait=60000
复制代码
配置beans.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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入外部资源文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath*:jdbc.properties</value>
</array>
</property>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<!-- 相应驱动的jdbcUrl -->
<property name="url" value="${jdbc.url}"></property>
<!-- 数据库的用户名 -->
<property name="username" value="${jdbc.username}"></property>
<!-- 数据库的密码 -->
<property name="password" value="${jdbc.password}"></property>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<property name="minIdle" value="${jdbc.minIdle}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<!--Spring与Hibernate进行集成 配置一个SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 注入hibernate的配置属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/bjlemon/domain/*.hbm.xml"></property>
<!--<property name="mappingResources">
<array>
<value>com/bjlemon/domain/userinfo.hbm.xml</value>
<value>com/bjlemon/domain/departinfo.hbm.xml</value>
</array>
</property>-->
</bean>
<!-- 配置 Spring 的声明式事务 -->
<!-- 配置 hibernate 的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切入点, 再把事务属性和事务切入点关联起来 -->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.bjlemon.service.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor>
</aop:config>
<!--配置HibernateTemplate-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<import resource="classpath*:spring/applicationContext-*.xml"/>
</beans>
复制代码
配置applicationContext-userInfo.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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入外部资源文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath*:jdbc.properties</value>
</array>
</property>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<!-- 相应驱动的jdbcUrl -->
<property name="url" value="${jdbc.url}"></property>
<!-- 数据库的用户名 -->
<property name="username" value="${jdbc.username}"></property>
<!-- 数据库的密码 -->
<property name="password" value="${jdbc.password}"></property>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<property name="minIdle" value="${jdbc.minIdle}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<!--Spring与Hibernate进行集成 配置一个SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 注入hibernate的配置属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/bjlemon/domain/*.hbm.xml"></property>
<!--<property name="mappingResources">
<array>
<value>com/bjlemon/domain/userinfo.hbm.xml</value>
<value>com/bjlemon/domain/departinfo.hbm.xml</value>
</array>
</property>-->
</bean>
<!-- 配置 Spring 的声明式事务 -->
<!-- 配置 hibernate 的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切入点, 再把事务属性和事务切入点关联起来 -->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.bjlemon.service.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor>
</aop:config>
<!--配置HibernateTemplate-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<import resource="classpath*:spring/applicationContext-*.xml"/>
</beans>
复制代码
配置applicationContext-departInfo.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="departInfoDao" class="com.bjlemon.dao.impl.DepartInfoDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="departInfoService" class="com.bjlemon.service.impl.DepartInfoServiceImpl">
<property name="departInfoDao" ref="departInfoDao"></property>
</bean>
<bean id="departInfoAction" class="com.bjlemon.web.DepartInfoAction">
<property name="departInfoService" ref="departInfoService"></property>
<property name="userInfoService" ref="userInfoService"></property>
</bean>
</beans>
复制代码
配置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>
<constant name="struts.devMode" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.objectFactory" value="spring"/>
<package name="base" extends="struts-default" namespace="/">
<action name="userInfoAction_*" class="userInfoAction" method="{1}">
<result name="list">/WEB-INF/pages/userinfo/userinfo_list.jsp</result>
</action>
</package>
<include file="struts/struts-departInfo.xml"></include>
<include file="struts/struts-userInfo.xml"></include>
</struts>
复制代码
配置struts-userInfo.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="userInfo" extends="base" namespace="/userInfo" strict-method-invocation="false">
<action name="userInfoAction_*" class="userInfoAction" method="{1}">
<result name="list">/WEB-INF/pages/userinfo/userinfo_list.jsp</result>
<result name="addUI">/WEB-INF/pages/userinfo/userinfo_add.jsp</result>
<result name="add" type="redirectAction">
<param name="actionName">userInfoAction_list</param>
</result>
<result name="updateUI">/WEB-INF/pages/userinfo/userinfo_update.jsp</result>
<result name="update" type="redirectAction">
<param name="actionName">userInfoAction_list.action</param>
</result>
<result name="delete" type="redirectAction">
<param name="actionName">userInfoAction_list</param>
</result>
</action>
</package>
</struts>
复制代码
配置struts-departInfo.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="departInfo" extends="base" namespace="/departInfo" strict-method-invocation="false">
<action name="departInfoAction_*" class="departInfoAction" method="{1}">
<result name="list">/WEB-INF/pages/departinfo/departinfo_list.jsp</result>
<result name="addUI">/WEB-INF/pages/departinfo/departinfo_add.jsp</result>
<result name="add" type="redirectAction">
<param name="actionName">departInfoAction_list</param>
</result>
<result name="updateUI">/WEB-INF/pages/departinfo/departinfo_update.jsp</result>
<result name="update" type="redirectAction">
<param name="actionName">departInfoAction_list.action</param>
</result>
<result name="delete" type="redirectAction">
<param name="actionName">departInfoAction_list.action</param>
</result>
<result name="findUserInfo">/WEB-INF/pages/departinfo/depart_userinfolist.jsp</result>
</action>
</package>
</struts>
复制代码
com.bjlemon.domain包下
DepartInfo.java
public class DepartInfo {
private int depId;
private String depName;
private int depState;
private String depDesc;
private String ck;
private Integer[] ids;
private List<UserInfo> userInfoList;
public int getDepId() {
return depId;
}
public void setDepId(int depId) {
this.depId = depId;
}
public String getDepName() {
return depName;
}
public void setDepName(String depName) {
this.depName = depName;
}
public int getDepState() {
return depState;
}
public void setDepState(int depState) {
this.depState = depState;
}
public String getDepDesc() {
return depDesc;
}
public void setDepDesc(String depDesc) {
this.depDesc = depDesc;
}
public String getCk() {
return "<input type='checkbox' name='ids' value='"+depId+"'/>";
}
public void setCk(String ck) {
this.ck = ck;
}
public Integer[] getIds() {
return ids;
}
public void setIds(Integer[] ids) {
this.ids = ids;
}
public List<UserInfo> getUserInfoList() {
return userInfoList;
}
public void setUserInfoList(List<UserInfo> userInfoList) {
this.userInfoList = userInfoList;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DepartInfo that = (DepartInfo) o;
return depId == that.depId &&
depState == that.depState &&
Objects.equals(depName, that.depName) &&
Objects.equals(depDesc, that.depDesc);
}
@Override
public int hashCode() {
return Objects.hash(depId, depName, depState, depDesc);
}
@Override
public String toString() {
return "DepartInfo{" +
"depId=" + depId +
", depName='" + depName + '\'' +
", depState=" + depState +
", depDesc='" + depDesc + '\'' +
", ck='" + ck + '\'' +
", ids=" + Arrays.toString(ids) +
", userInfoList=" + userInfoList +
'}';
}
}
复制代码
DepartInfo.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.bjlemon.domain.DepartInfo" table="depart_info" schema="ssh_config">
<id name="depId" column="DEP_ID"/>
<property name="depName" column="DEP_NAME"/>
<property name="depState" column="DEP_STATE"/>
<property name="depDesc" column="DEP_DESC"/>
</class>
</hibernate-mapping>
复制代码
UserInfo.java
public class UserInfo {
private int userId;
private Integer depId;
private String userName;
private String userSex;
private int userState;
private String ck;
private Integer[] ids;
private String depName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public Integer getDepId() {
return depId;
}
public void setDepId(Integer depId) {
this.depId = depId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public int getUserState() {
return userState;
}
public void setUserState(int userState) {
this.userState = userState;
}
public String getCk() {
return "<input type='checkbox' name='ids' value='"+userId+"'/>";
}
public void setCk(String ck) {
this.ck = ck;
}
public Integer[] getIds() {
return ids;
}
public void setIds(Integer[] ids) {
this.ids = ids;
}
public String getDepName() {
return depName;
}
public void setDepName(String depName) {
this.depName = depName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserInfo userInfo = (UserInfo) o;
return userId == userInfo.userId &&
userState == userInfo.userState &&
Objects.equals(depId, userInfo.depId) &&
Objects.equals(userName, userInfo.userName) &&
Objects.equals(userSex, userInfo.userSex);
}
@Override
public int hashCode() {
return Objects.hash(userId, depId, userName, userSex, userState);
}
@Override
public String toString() {
return "UserInfo{" +
"userId=" + userId +
", depId=" + depId +
", userName='" + userName + '\'' +
", userSex='" + userSex + '\'' +
", userState=" + userState +
", ck='" + ck + '\'' +
", ids=" + Arrays.toString(ids) +
", depName='" + depName + '\'' +
'}';
}
}
复制代码
UserInfo.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.bjlemon.domain.UserInfo" table="user_info" schema="ssh_config">
<id name="userId" column="USER_ID"/>
<property name="depId" column="DEP_ID"/>
<property name="userName" column="USER_NAME"/>
<property name="userSex" column="USER_SEX"/>
<property name="userState" column="USER_STATE"/>
</class>
</hibernate-mapping>
复制代码
com.bjlemon.common.utils包下
GenericUtils.java
public class GenericUtils {
/**
* 获取父类中的泛型类型中的实际的类型参数
* @param clazz
* @param index
* @return
*/
public static Class getSuperClassGenericActualTypeArguments(Class clazz,Integer index){
if(clazz==null){
return Object.class;
}
if(index==null||index<0){
return Object.class;
}
Type type=clazz.getGenericSuperclass();
if(type instanceof ParameterizedType){
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
if(ArrayUtils.isNotEmpty(actualTypeArguments)){
if(index>=ArrayUtils.getLength(actualTypeArguments)){
throw new IllegalArgumentException("");
}
return (Class)actualTypeArguments[index];
}
}
return Object.class;
}
}
复制代码
com.bjlemon.common.dao包下
BaseDao.java
public interface BaseDao<T> {
void save(T t);
void delete(T t);
void deleteById(Serializable id);
void deleteByIds(Serializable[] ids);
void deleteByIdList(List<Serializable>idList);
void update(T t);
T fingById(Serializable id);
List<T> findAll();
List<T> find(String whereHql,Object...params);
}
复制代码
com.bjlemon.common.dao.impl包下
public class BaseDaoImpl<T> implements BaseDao<T> {
private Class<T> entityClass= GenericUtils.getSuperClassGenericActualTypeArguments(this.getClass(),0);
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public void save(T t) {
this.hibernateTemplate.persist(t);
}
@Override
public void delete(T t) {
this.hibernateTemplate.delete(t);
}
@Override
public void deleteById(Serializable id){}
@Override
public void deleteByIds(Serializable[] ids){}
@Override
public void deleteByIdList(List<Serializable>idList){}
@Override
public void update(T t) {
this.hibernateTemplate.merge(t);
}
@Override
public T fingById(Serializable id) {
return this.hibernateTemplate.load(entityClass,id);
}
@Override
public List<T> findAll() {
return (List<T>) this.hibernateTemplate.find(" from "+this.entityClass.getSimpleName()+" o ");
}
@Override
public List<T> find(String whereHql, Object... params) {
if(StringUtils.isBlank(whereHql)){
return this.findAll();
}
String hql=" from "+this.entityClass.getSimpleName()+" o "+whereHql;
return (List<T>) this.hibernateTemplate.find(hql,params);
}
}
复制代码
com.bjlemon.common.web包下
BaseAction.java
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
private Class<T> clazz= GenericUtils.getSuperClassGenericActualTypeArguments(this.getClass(),0);
protected T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
@Override
public T getModel() {
try {
if(this.t==null){
this.t=this.clazz.newInstance();
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return this.t;
}
}
复制代码
com.bjlemon.dao包下
UserInfoDao.java
public interface UserInfoDao extends BaseDao<UserInfo> {
}
复制代码
DepartInfoDao.java
public interface DepartInfoDao extends BaseDao<DepartInfo> {
}
复制代码
com.bjlemon.dao.impl包下
UserInfoDaoImpl.java
public class UserInfoDaoImpl extends BaseDaoImpl<UserInfo> implements UserInfoDao {
}
复制代码
DepartInfoDaoImpl.java
public class DepartInfoDaoImpl extends BaseDaoImpl<DepartInfo> implements DepartInfoDao {
}
复制代码
com.bjlemon.service包下
UserInfoService.java
public interface UserInfoService {
void addUserInfo(UserInfo userInfo);
void deleteUserInfo(UserInfo userInfo);
void deleteUserInfoById(Integer id);
void deleteUserInfoByIds(Integer[] ids);
void modifyUserInfo(UserInfo userInfo);
UserInfo fingUserInfoById(Integer id);
List<UserInfo> findAllUserInfo();
List<UserInfo> findUserInfoList(String whereHql,Object...params);
}
复制代码
DepartInfoService.java
public interface DepartInfoService {
void addDepartInfo(DepartInfo departInfo);
void deleteDepartInfo(DepartInfo departInfo);
void deleteDepartInfoById(Integer id);
void deleteDepartInfoByIds(Integer[] ids);
void modifyDepartInfo(DepartInfo departInfo);
DepartInfo fingDepartInfoById(Integer id);
List<DepartInfo> findAllDepartInfo();
List<DepartInfo> findDepartInfoList(String whereHql,Object...params);
}
复制代码
com.bjlemon.service.impl包下
UserInfoServiceImpl.java
public class UserInfoServiceImpl implements UserInfoService {
private UserInfoDao userInfoDao;
public void setUserInfoDao(UserInfoDao userInfoDao) {
this.userInfoDao = userInfoDao;
}
@Override
public void addUserInfo(UserInfo userInfo) {
if(userInfo==null){
throw new IllegalArgumentException("");
}
this.userInfoDao.save(userInfo);
}
@Override
public void deleteUserInfo(UserInfo userInfo) {
if(userInfo==null){
throw new IllegalArgumentException("");
}
this.userInfoDao.delete(userInfo);
}
@Override
public void deleteUserInfoById(Integer id){
if(id==null||id<=0){
throw new IllegalArgumentException("");
}
//真删除
//this.deleteUserInfo(this.fingUserInfoById(id));
//假删除
UserInfo userInfo=this.userInfoDao.fingById(id);
userInfo.setUserState(1);
this.userInfoDao.update(userInfo);
}
@Override
public void deleteUserInfoByIds(Integer[] ids){
if(ids==null||ids.length==0){
throw new IllegalArgumentException("");
}
for(int i=0;i<ids.length;i++){
this.deleteUserInfoById(ids[i]);
}
}
@Override
public void modifyUserInfo(UserInfo userInfo) {
if(userInfo==null){
throw new IllegalArgumentException("");
}
this.userInfoDao.update(userInfo);
}
@Override
public UserInfo fingUserInfoById(Integer id) {
if(id==null||id<=0){
throw new IllegalArgumentException("");
}
return this.userInfoDao.fingById(id);
}
@Override
public List<UserInfo> findAllUserInfo() {
return this.userInfoDao.findAll();
}
@Override
public List<UserInfo> findUserInfoList(String whereHql, Object... params) {
return this.userInfoDao.find(whereHql,params);
}
}
复制代码
DepartInfoServiceImpl.java
public class DepartInfoServiceImpl implements DepartInfoService {
private DepartInfoDao departInfoDao;
private UserInfoDao userInfoDao;
public void setDepartInfoDao(DepartInfoDao departInfoDao) {
this.departInfoDao = departInfoDao;
}
@Override
public void addDepartInfo(DepartInfo departInfo) {
if(departInfo==null){
throw new IllegalArgumentException("");
}
this.departInfoDao.save(departInfo);
}
@Override
public void deleteDepartInfo(DepartInfo departInfo) {
if(departInfo==null){
throw new IllegalArgumentException("");
}
//真删除
//this.departInfoDao.delete(departInfo);
//假删除
departInfo.setDepState(1);
this.departInfoDao.update(departInfo);
}
@Override
public void deleteDepartInfoById(Integer id){
if(id==null||id<=0){
throw new IllegalArgumentException("");
}
//真删除
//this.deleteDepartInfo(this.fingDepartInfoById(id));
//假删除
DepartInfo departInfo=this.departInfoDao.fingById(id);
departInfo.setDepState(1);
this.departInfoDao.update(departInfo);
}
@Override
public void deleteDepartInfoByIds(Integer[] ids){
if(ids==null||ids.length==0){
throw new IllegalArgumentException("");
}
for(int i=0;i<ids.length;i++){
this.deleteDepartInfoById(ids[i]);
}
}
@Override
public void modifyDepartInfo(DepartInfo departInfo) {
if(departInfo==null){
throw new IllegalArgumentException("");
}
this.departInfoDao.update(departInfo);
}
@Override
public DepartInfo fingDepartInfoById(Integer id) {
if(id==null||id<=0){
throw new IllegalArgumentException("");
}
return this.departInfoDao.fingById(id);
}
@Override
public List<DepartInfo> findAllDepartInfo() {
return this.departInfoDao.findAll();
}
@Override
public List<DepartInfo> findDepartInfoList(String whereHql, Object... params) {
return this.departInfoDao.find(whereHql,params);
}
}
复制代码
com.bjlemon.web包下
UserInfoAction.java
public class UserInfoAction extends BaseAction<UserInfo>{
private UserInfoService userInfoService;
private DepartInfoService departInfoService;
private String userInfoName;
public void setUserInfoName(String userInfoName) {
this.userInfoName = userInfoName;
}
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
public void setDepartInfoService(DepartInfoService departInfoService) {
this.departInfoService = departInfoService;
}
public String list()throws Exception{
List<UserInfo> userInfoList=new ArrayList<>();
String userName=this.userInfoName;
String hql="";
if(StringUtils.isNotEmpty(userName)){
hql=" where o.userName like ? and o.userState = ?";
userInfoList=this.userInfoService.findUserInfoList(hql,"%"+userName+"%",0);
}else{
hql=" where o.userState = ?";
userInfoList=this.userInfoService.findUserInfoList(hql,0);
}
for(int i=0;i<userInfoList.size();i++){
userInfoList.get(i).setDepName(this.departInfoService.fingDepartInfoById(userInfoList.get(i).getDepId()).getDepName());
}
ActionContext.getContext().put("userInfoList",userInfoList);
this.userInfoName=null;
return "list";
}
public String addUI()throws Exception{
List<DepartInfo> departInfoList=departInfoService.findAllDepartInfo();
ActionContext.getContext().put("departInfoList",departInfoList);
return "addUI";
}
public String add()throws Exception{
UserInfo userInfo=this.t;
this.userInfoService.addUserInfo(userInfo);
return "add";
}
public String updateUI()throws Exception{
UserInfo userInfo=this.t;
userInfo=this.userInfoService.fingUserInfoById(userInfo.getUserId());
DepartInfo departInfo=this.departInfoService.fingDepartInfoById(userInfo.getDepId());
ActionContext.getContext().put("userInfo",userInfo);
ActionContext.getContext().put("departInfo",departInfo);
return "updateUI";
}
public String update()throws Exception{
UserInfo userInfo=this.t;
this.userInfoService.modifyUserInfo(userInfo);
return "update";
}
public String delete()throws Exception{
UserInfo userInfo=this.t;
this.userInfoService.deleteUserInfoByIds(userInfo.getIds());
return "delete";
}
}
复制代码
DepartInfoAction.java
public class DepartInfoAction extends BaseAction<DepartInfo> {
private DepartInfoService departInfoService;
private UserInfoService userInfoService;
private String deptName;
public void setDepartInfoService(DepartInfoService departInfoService) {
this.departInfoService = departInfoService;
}
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
public void setDeptName(String depName) {
this.deptName = depName;
}
public String list()throws Exception{
List<DepartInfo> departInfoList=new ArrayList<>();
String depName=this.deptName;
String hql="";
if(StringUtils.isNotBlank(depName)){
hql=" where o.depName = ? and o.depState = ?";
departInfoList=this.departInfoService.findDepartInfoList(hql,depName,0);
}else{
hql=" where o.depState = ?";
departInfoList=this.departInfoService.findDepartInfoList(hql,0);
}
ActionContext.getContext().put("departInfoList",departInfoList);
this.deptName=null;
return "list";
}
public String addUI()throws Exception{
return "addUI";
}
public String add()throws Exception{
DepartInfo departInfo=this.t;
this.departInfoService.addDepartInfo(departInfo);
return "add";
}
public String updateUI()throws Exception{
DepartInfo departInfo=this.t;
departInfo=this.departInfoService.fingDepartInfoById(departInfo.getDepId());
ActionContext.getContext().put("departInfo",departInfo);
return "updateUI";
}
public String update()throws Exception{
DepartInfo departInfo=this.t;
this.departInfoService.modifyDepartInfo(departInfo);
return "update";
}
public String delete()throws Exception{
DepartInfo departInfo=this.t;
Integer [] ids=departInfo.getIds();
String hql="";
for(int i=0;i<ids.length;i++){
hql=" where o.depId = ? and o.userState = ?";
List<UserInfo> userInfo=this.userInfoService.findUserInfoList(hql,ids[i],0);
if(userInfo.size()>0){
throw new IllegalArgumentException("部门下有员工,删除失败");
}
}
this.departInfoService.deleteDepartInfoByIds(departInfo.getIds());
return "delete";
}
public String findUserInfo()throws Exception{
DepartInfo departInfo=this.t;
String hql=" where o.depName = ? and o.depState = ?";
departInfo=this.departInfoService.findDepartInfoList(hql,departInfo.getDepName(),0).get(0);
hql=" where o.depId = ? and o.userState = ?";
List<UserInfo> userInfoList=this.userInfoService.findUserInfoList(hql,departInfo.getDepId(),0);
for(int i=0;i<userInfoList.size();i++){
userInfoList.get(i).setDepName(this.departInfoService.fingDepartInfoById(userInfoList.get(i).getDepId()).getDepName());
}
ActionContext.getContext().put("userInfoList",userInfoList);
return "findUserInfo";
}
}
复制代码
WEB-INF/pages/common/taglib.jsp包下
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
复制代码
WEB-INF/pages/userinfo/userinfo_list.jsp包下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/taglib.jsp"%>
<html>
<head>
<title>Title</title>
<script src="${pageContext.request.contextPath}/resource/js/jquery-1.9.0.min.js"></script>
<script>
$(function(){
$("#all").click(function () {
var flag=this.checked;
$("input[name='ids']").each(function () {
this.checked=flag;
});
});
$("input[name='ids']").click(function () {
var flag=$("input[name='ids']").length==$("input[name='ids']:checked").length
$("#all").each(function () {
this.checked=flag;
});
});
$("#del").click(function(){
var ids=$("input[name='ids']:checked");
if(ids.length==0){
alert("请选择删除的员工");
return;
}
if(confirm("确认要删除吗?")){
var form=$("#form");
form.attr("action","${pageContext.request.contextPath}/userInfo/userInfoAction_delete.action");
form.submit();
}
});
})
</script>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/userInfo/userInfoAction_list.action" id="form">
<div>
员工名称:<input type="text" name="userInfoName">
<input type="submit" value="查询">
<a href="${pageContext.request.contextPath}/userInfo/userInfoAction_addUI.action">添加</a>
<input type="button" id="del" value="删除">
</div>
<div>
<table border="1">
<thead>
<tr>
<th><input type="checkbox" id="all"></th>
<th>员工编号</th>
<th>名称</th>
<th>性别</th>
<th>所属部门</th>
<th>员工状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userInfoList}" var="userInfo">
<tr>
<td>${userInfo.ck}</td>
<td>${userInfo.userId}</td>
<td>${userInfo.userName}</td>
<td>${userInfo.userSex}</td>
<td>${userInfo.depName}</td>
<td>${userInfo.userState==0?"有效":"删除"}</td>
<td>
<a href="${pageContext.request.contextPath}/userInfo/userInfoAction_updateUI.action?userId=${userInfo.userId}">修改员工</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</form>
</body>
</html>
复制代码
WEB-INF/pages/userinfo/userinfo_add.jsp包下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/taglib.jsp"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/userInfo/userInfoAction_add.action" method="post">
所属部门:<select name="depId">
<option>选择部门11</option>
<c:forEach items="${departInfoList}" var="departInfo">
<option value="${departInfo.depId}">${departInfo.depName}</option>
</c:forEach>
</select><br>
员工姓名:<input type="text" name="userName"><br>
性别:<input type="radio" name="userSex" value="男" checked>男
<input type="radio" name="userSex" value="女">女<br>
<input type="submit" value="添加">
</form>
</body>
</html>
复制代码
WEB-INF/pages/userinfo/userinfo_update.jsp包下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/taglib.jsp"%>
<html>
<head>
<title>Title</title>
<script src="${pageContext.request.contextPath}/resource/js/jquery-1.9.0.min.js"></script>
<script>
$(function(){
$("[name=userSex][value=${userInfo.userSex}]").attr("checked","checked");
});
</script>
</head>
<body>
<form action="${pageContext.request.contextPath}/userInfo/userInfoAction_update" method="post">
<input type="hidden" name="userId" value="${userInfo.userId}">
所属部门:<select name="depId">
<option value="${departInfo.depId}">${departInfo.depName}</option>
</select><br>
员工姓名:<input type="text" name="userName" value="${userInfo.userName}"><br>
性别:<input type="radio" name="userSex" value="男">男
<input type="radio" name="userSex" value="女">女<br>
<input type="submit" value="修改">
</form>
</body>
</html>
复制代码
WEB-INF/pages/departinfo/departinfo_list.jsp包下
departinfo
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/taglib.jsp"%>
<html>
<head>
<title>Title</title>
<script src="${pageContext.request.contextPath}/resource/js/jquery-1.9.0.min.js"></script>
<script>
$(function(){
$("#all").click(function () {
var flag=this.checked;
$("input[name='ids']").each(function () {
this.checked=flag;
});
});
$("input[name='ids']").click(function () {
var flag=$("input[name='ids']").length==$("input[name='ids']:checked").length
$("#all").each(function () {
this.checked=flag;
});
});
$("#del").click(function(){
var ids=$("input[name='ids']:checked");
if(ids.length==0){
alert("请选择删除的部门");
return;
}
if(confirm("确认要删除吗?")){
var form=$("#form");
form.attr("action","${pageContext.request.contextPath}/departInfo/departInfoAction_delete.action");
form.submit();
}
});
})
</script>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/departInfo/departInfoAction_list.action" id="form">
<div>
部门名称:<input type="text" name="deptName">
<input type="submit" value="查询">
<a href="${pageContext.request.contextPath}/departInfo/departInfoAction_addUI.action">添加</a>
<input type="button" id="del" value="删除">
</div>
<div>
<table border="1">
<thead>
<tr>
<th><input type="checkbox" id="all"></th>
<th>部门编号</th>
<th>部门名称</th>
<th>部门备注</th>
<th>部门状态</th>
<th>部门操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${departInfoList}" var="departInfo">
<tr>
<td>${departInfo.ck}</td>
<td>${departInfo.depId}</td>
<td>${departInfo.depName}</td>
<td>${departInfo.depDesc}</td>
<td>${departInfo.depState==0?"有效":"删除"}</td>
<td>
<a href="${pageContext.request.contextPath}/departInfo/departInfoAction_updateUI.action?depId=${departInfo.depId}">修改员工</a>
<a href="${pageContext.request.contextPath}/departInfo/departInfoAction_findUserInfo.action?depName=${departInfo.depName}">查看员工</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</form>
</body>
</html>
复制代码
WEB-INF/pages/departinfo/departinfo_add.jsp包下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/taglib.jsp"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/departInfo/departInfoAction_add.action" method="post">
<label>部门名称</label><input type="text" name="depName"><br>
<label>部门备注</label><textarea name="depDesc"></textarea><br>
<input type="submit" value="保存">
</form>
</body>
</html>
复制代码
WEB-INF/pages/departinfo/departinfo_update.jsp包下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/taglib.jsp"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/departInfo/departInfoAction_update" method="post">
<input type="hidden" name="depId" value="${departInfo.depId}">
部门名称:<input type="text" name="depName" value="${departInfo.depName}"><br>
部门备注:<textarea name="depDesc">${departInfo.depDesc}</textarea><br>
<input type="submit" value="提交">
</form>
</body>
</html>
复制代码
WEB-INF/pages/departinfo/depart_userinfolist.jsp包下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/taglib.jsp"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>员工编号</th>
<th>名称</th>
<th>性别</th>
<th>所属部门</th>
<th>员工状态</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userInfoList}" var="userInfo">
<tr>
<td>${userInfo.userId}</td>
<td>${userInfo.userName}</td>
<td>${userInfo.userSex}</td>
<td>${userInfo.depName}</td>
<td>${userInfo.userState==0?"有效":"删除"}</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="${pageContext.request.contextPath}/departInfo/departInfoAction_list">返回部门列表</a>
</body>
</html>
复制代码