applicationContext.xml
<context:property-placeholder location="classpath:/db-mysql.properties"/>
<context:component-scan base-package="cn.tarena.ht.service" />
springmvc-config.xml
<context:component-scan base-package="cn.tarena.ht.controller" />
<mvc:default-servlet-handler />
db-mysql.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ht?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull jdbc.username=root jdbc.password=root
db-oracle.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:XE jdbc.username=ht1703 jdbc.password=ht1703
log4j.properties
log4j.rootLogger=DEBUG,A1 log4j.logger.org.mybatis = info log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
web.xml
mybatis-config.xml
<!-- 通用Mapper插件 -->
<!-- <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
主键自增回写方法,默认值MYSQL,详细说明请看文档
property name="IDENTITY" value="MYSQL" /
通用Mapper接口,多个通用接口用逗号隔开
<property name="mappers" value="cn.tarena.ht.mapper.base.SysMapper" />
</plugin> -->
DeptMapper.xml
<!--自关联 一对一-->
<association property="parentDept" javaType="Dept">
<id column="p\_id" property="deptId"/>
<result column="p\_name" property="deptName"/>
</association>
where dept\_id = #{deptId}
DeptMapper
package cn.tarena.ht.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import cn.tarena.ht.pojo.Dept;
public interface DeptMapper {
//查询全部部门信息
public List
//将参数封装为一个Map public void updateState(@Param("deptIds") String[] deptIds, @Param("state") int state);
//根据deptId 删除部门信息 public void deleteDepts(String[] deptIds);
//部门的新增 public void saveDept(Dept dept);
//根据Id查询数据 public Dept findOne(String deptId);
//修改部门信息 public void updateDept(Dept dept); }
applicationContext-mybatis.xml
applicationContext-shiro.xml
<bean id="lifeCycleBeanProcessor"
class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifeCycleBeanProcessor">
<!--表示以cglib生成代理对象
1.有无接口 都可以生成代理对象
2.代理对象是目标对象的子类
Shiro中必须以cglib的形式生成代理对象
-->
<property name="proxyTargetClass" value="true"></property>
</bean>
<aop:aspectj-autoproxy proxy-target-class="true">
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--必须注入Relam提供真实的信息,方便做判断 -->
<property name="realm" ref="AuthRealm"></property>
</bean>
<!--自定义Realm 是我们自己手动编写的.里边需要提供真实的用户数据 -->
<bean id="AuthRealm" class="cn.tarena.ht.shiro.AuthRealm">
<!--引入自定义的加密器 -->
<property name="credentialsMatcher" ref="authCredential"></property>
</bean>
<bean id="authCredential" class="cn.tarena.ht.shiro.AuthCredential"></bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<!--表示注入安全中心,以后做校验 -->
<property name="securityManager" ref="securityManager"></property>
</bean>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!--注入安全中心 -->
<property name="securityManager" ref="securityManager"></property>
<!--只要没有经过认证的请求,都会被拦截到指定页面 -->
<property name="loginUrl" value="/index.jsp"></property>
<property name="filterChainDefinitions">
<value>
<!--如果什么都不加比较无需拦截,直接跳转
/\* 表示只拦截请求 不拦截静态资源文件
/\*\* 表示拦截全部的资源 包括请求和静态资源文件
authc 表示拦截
anon 表示放行
-->
/toLogin.action = anon
/login.action = anon
/staticfile/\*\* = anon
/\*\* = authc
</value>
</property>
</bean>
applicationContext-transaction.xml
<tx:advice id="txAdvice" transaction-manager="transactionManager"> tx:attributes <tx:method name="query*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="get*" read-only="true" />
<!-- 主库执行操作,事务传播行为定义为默认行为 -->
<tx:method name="save\*" propagation="REQUIRED" />
<tx:method name="update\*" propagation="REQUIRED" />
<tx:method name="delete\*" propagation="REQUIRED" />
<!--其他方法使用默认事务策略 -->
<tx:method name="\*" read-only="true"/>
</tx:attributes>
aop:config <aop:pointcut id="txPointcut" expression="execution(* cn.tarena.ht.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
DeptController
package cn.tarena.ht.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;
import cn.tarena.ht.pojo.Dept; import cn.tarena.ht.service.DeptService;
@Controller @RequestMapping("/sysadmin/dept/") public class DeptControllr {
@Resource private DeptService deptService;
@RequestMapping("list")
public String findList(Model model){
List
//跳转到部门列表页面
return "sysadmin/dept/jDeptList";
}
//状态的改变 @RequestMapping("stop") public String toStop(@RequestParam(value="deptId",required=true) String[] deptIds){
int state = 0; //表示状态停用
deptService.updateState(deptIds,state);
//重定向到部门列表页面
return "redirect:/sysadmin/dept/list";
}
@RequestMapping("start") public String toStart(@RequestParam(value="deptId",required=true) String[] deptIds){ int state = 1; //表示状态停用 deptService.updateState(deptIds,state);
//重定向到部门列表页面
return "redirect:/sysadmin/dept/list";
}
//部门删除 @RequestMapping("delete") public String toDelete(@RequestParam(value="deptId",required=true)String[] deptIds){
deptService.deleteDepts(deptIds);
return "redirect:/sysadmin/dept/list";
}
//跳转部门新增页面 @RequestMapping("tocreate") public String toCreate(Model model){
//查询部门全部列表数据
List<Dept> parentDeptList = deptService.findAll();
model.addAttribute("parentDeptList", parentDeptList);
return "sysadmin/dept/jDeptCreate";
}
@RequestMapping("save") public String saveDept(Dept dept){
deptService.saveDept(dept);
return "redirect:/sysadmin/dept/list";
}
@RequestMapping("toupdate") public String updateDept(String deptId,Model model){
//准备修改数据
Dept dept = deptService.findOne(deptId);
//准备上级部门的下拉列表
List<Dept> parentList = deptService.findAll();
model.addAttribute("dept", dept);
model.addAttribute("parentList", parentList);
return "sysadmin/dept/jDeptUpdate";
}
@RequestMapping("update") public String updateDept(Dept dept){
deptService.updateDept(dept);
return "redirect:/sysadmin/dept/list";
}
//部门查看 @RequestMapping("toview") public String toView(String deptId,Model model){
Dept dept = deptService.findOne(deptId);
model.addAttribute("dept", dept);
//跳转到查看页面
return "sysadmin/dept/jDeptView";
}
}
Dept
package cn.tarena.ht.pojo; //部门对象 public class Dept extends BaseEntity{ private String deptId; private Dept parentDept; //一对一关联关系 private String deptName; //部门名称 private Integer state; //部门状态 1启用 0停用
public String getDeptId() { return deptId; } public void setDeptId(String deptId) { this.deptId = deptId; } public Dept getParentDept() { return parentDept; } public void setParentDept(Dept parentDept) { this.parentDept = parentDept; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public Integer getState() { return state; } public void setState(Integer state) { this.state = state; } @Override public String toString() { return "Dept [deptId=" + deptId + ", parentDept=" + parentDept + ", deptName=" + deptName + ", state=" + state + "]"; }
}
DeptService
package cn.tarena.ht.service;
import java.util.List;
import cn.tarena.ht.pojo.Dept;
public interface DeptService {
public List
public void updateState(String[] deptIds, int state);
public void deleteDepts(String[] deptIds);
public void saveDept(Dept dept);
public Dept findOne(String deptId);
public void updateDept(Dept dept); }
DeptServiceImpl
package cn.tarena.ht.service.impl;
import java.util.Date; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import cn.tarena.ht.mapper.DeptMapper; import cn.tarena.ht.pojo.Dept; import cn.tarena.ht.service.DeptService; @Service public class DeptServiceImpl implements DeptService {
@Autowired private DeptMapper deptMapper;
@Override
public List
return deptMapper.findAll();
}
@Override public void updateState(String[] deptIds, int state) {
deptMapper.updateState(deptIds,state);
}
@Override public void deleteDepts(String[] deptIds) {
deptMapper.deleteDepts(deptIds);
}
@Override public void saveDept(Dept dept) {
dept.setCreateTime(new Date()); //添加新增日期
deptMapper.saveDept(dept);
}
@Override public Dept findOne(String deptId) {
return deptMapper.findOne(deptId);
}
@Override public void updateDept(Dept dept) { dept.setUpdateTime(new Date()); deptMapper.updateDept(dept);
}
}
pom.xml
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
<mysql.version>5.1.32</mysql.version>
<bonecp-spring.version>0.8.0.RELEASE</bonecp-spring.version>
<druid.version>1.0.9</druid.version>
<mapper.version>2.3.2</mapper.version>
<pagehelper.version>3.4.2</pagehelper.version>
<jsqlparser.version>0.9.1</jsqlparser.version>
<slf4j.version>1.6.4</slf4j.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<jackson.version>2.4.2</jackson.version>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>${mybatis.paginator.version}</version>
</dependency>
<!-- 通用Mapper,所有的单表的代码都不用编写 -->
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
<!--转化JSON的工具 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Oracle版权问题,maven目录必须自己手动建立 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 超高性能连接池 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId>
<version>2.3.9</version>
<scope>compile</scope>
</dependency>
<!-- Apache Shiro 权限架构 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>${jsqlparser.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- 字符加密、解密 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<!-- 数据校验 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
</dependencies>