ssm框架整合

Easter79
• 阅读 763

applicationContext.xml

<context:property-placeholder location="classpath:/db-mysql.properties"/>

<context:component-scan base-package="cn.tarena.ht.service" />

springmvc-config.xml

mvc:annotation-driven/

<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

ht

contextConfigLocation classpath:spring/applicationContext\*.xml org.springframework.web.context.ContextLoaderListener encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF8 encodingFilter /\* shiroFilter org.springframework.web.filter.DelegatingFilterProxy targetFilterLifecycle true shiroFilter /\* springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring/springmvc-config.xml 1 springmvc / index.jsp

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>
update dept\_p set state = #{state} where dept\_id in( #{deptId} ) delete from dept\_p where dept\_id in( #{deptId} ) insert into dept\_p(dept\_id,parent\_id,dept\_name,state,create\_time) values( #{deptId}, #{parentDept.deptId}, #{deptName}, #{state}, #{createTime} ) update dept\_p set parent\_id = #{parentDept.deptId}, dept\_name = #{deptName}, state = #{state}, update\_time = #{updateTime}
  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 findAll();

//将参数封装为一个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 deptList = deptService.findAll(); model.addAttribute("deptList", deptList);

  //跳转到部门列表页面
  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 findAll();

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 findAll() {

  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

4.0.0 cn.tedu HT1703 war 0.0.1-SNAPSHOT HT1703 Maven Webapp http://maven.apache.org <junit.version>4.12</junit.version> <spring.version>4.2.3.RELEASE</spring.version>

  <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>
  
org.springframework spring-context ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-jdbc ${spring.version} org.springframework spring-aspects ${spring.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>
org.apache.tomcat.maven tomcat7-maven-plugin 2.2 8090 /
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
3年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k