汇通day01

内容:

  汇通权限管理模块--部门模块

操作步骤:

1.通过maven整合SSM,优化配置文件。

   a. idea整合ssm

     b.优化配置文件

    将spring文件夹下的分为四个配置文件,分别管理spring(applicationContext.xml),spring-mybatis(applicationContext-mybatis.xml),spring事物设置(applicationContext-transaction.xml),spring-mvc(springmvc-config.xml)

  applicationContext.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
>
<context:property-placeholder location="classpath:/jdbc.properties"/>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxPoolSize" value="20"></property>
<property name="minPoolSize" value="3"></property>
</bean>

<!-- aop注解开关 -->
<aop:aspectj-autoproxy/>

<!-- spring容器的注解开关 -->
<context:annotation-config/>
<!-- 包扫描 -->
<context:component-scan base-package="cn.tedu"></context:component-scan>

</beans>

applicationContext-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
>

<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:/mybatis/sqlMapConfig.xml"></property>
<property name="typeAliasesPackage" value="cn.tedu.pojo"></property>
<!-- 如果工程中没有mapper.xml映射文件 则不能进行注入 否则报错 -->
<property name="mapperLocations" value="classpath:/mybatis/mapper/*.xml"></property>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.tedu.mapper"></property>
</bean>


<!-- spring容器的注解开关 -->
<context:annotation-config/>
<!-- 包扫描 -->
<context:component-scan base-package="cn.tedu"></context:component-scan>

</beans>
applicationContext-transaction.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
>


<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 声明式事务的注解开关 -->
<tx:annotation-driven transaction-manager="transactionManager"/>


<!-- aop注解开关 -->
<aop:aspectj-autoproxy/>

<!-- spring容器的注解开关 -->
<context:annotation-config/>
<!-- 包扫描 -->
<context:component-scan base-package="cn.tedu"></context:component-scan>

</beans>
springmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 配置资源解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 文件上传解析器 id必须要写 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"></property>
</bean>

<!-- 默认servlet是指处理静态资源的servlet 意思是放行静态资源文件-->
<mvc:default-servlet-handler/>
<!-- springmvc注解开关 -->
<mvc:annotation-driven/>

<context:annotation-config/>
<context:component-scan base-package="cn.tedu"></context:component-scan>
</beans>
在mybatis下面的sql配置文件,开启mybatis缓存和驼峰命名。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<settings>
<!-- 开启驼峰自动映射 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 二级缓存的总开关 -->
<setting name="cacheEnabled" value="false"/>
</settings>

</configuration>

配置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_3_1.xsd"
version="3.1">

<display-name>Archetype Created Web Application</display-name>
   <!--配置spring的xml的bean的配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>

<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  <!--配置springmvc-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 随着servlet启动的只有springMVC 把原来Spring相关的配置文件改成懒加载 -->
<param-value>classpath:/spring/springmvc-config.xml</param-value>
</init-param>
<!-- servlet启动的优先级 值越小 优先级越高 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 乱码过滤器 -->
<filter>
<filter-name>characterFilter</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>
</filter>
<filter-mapping>
<filter-name>characterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

contextConfigLocation的作用:加载配置文件,也就是初始化配置文件

 我们有配置ContextLoaderListener, Spring会去web.xml中看我们是否有定义contextConfigLocation这        个参数,如果有则Spring容器(Bean工厂)会把定义在该xml文件中的bean加载到容器中,那如果没有定义contextConfigLocation参数,Spring有一个概念就是约定优于配置,也就是说,即使你没有显示定义xml文件的位置,Spring容器会到一个约定的地方去找该文件,如果找不到就要报FileNotFoundException了。

 因为整合了ssm,所以要配置spring和springmvc。

2.导入静态模板页面

  在webapp下面创建静态文件,里面存放页面用到的一些js,css等文件。在WEB-INF下面导入静态模板页面,如下:

  

3.部门管理(增删改查)

 导入数据表:

DROP TABLE IF EXISTS `DEPT_P`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `DEPT_P` (
`DEPT_ID` int(11) NOT NULL AUTO_INCREMENT,
`PARENT_ID` int(11) DEFAULT NULL,
`DEPT_NAME` varchar(45) DEFAULT NULL,
`STATE` varchar(45) DEFAULT NULL,
`CREATE_BY` varchar(45) DEFAULT NULL,
`CREATE_DEPT` varchar(45) DEFAULT NULL,
`CREATE_TIME` timestamp NULL DEFAULT NULL,
`UPDATE_BY` varchar(45) DEFAULT NULL,
`UPDATE_TIME` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`DEPT_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

INSERT INTO `DEPT_P` VALUES (1,3,'财务部','1','semb','11',NULL,NULL,NULL),(2,1,'开发部','1','smeb',NULL,NULL,NULL,NULL),(3,1,'smell','1','smeb',NULL,NULL,NULL,NULL),(6,1,'lol','1',NULL,NULL,NULL,NULL,NULL);

   定义javabean,在pojo中新建class类

  BaseEntity:

    

package cn.tedu.pojo;

import java.util.Date;

public abstract class BaseEntity {
private String createBy;
private String createDept;
private Date creatTime;
private String updateBy;
private Date updateTime;

public String getCreateBy() {
return createBy;
}

public void setCreateBy(String createBy) {
this.createBy = createBy;
}

public String getCreateDept() {
return createDept;
}

public void setCreateDept(String createDept) {
this.createDept = createDept;
}

public Date getCreatTime() {
return creatTime;
}

public void setCreatTime(Date creatTime) {
this.creatTime = creatTime;
}

public String getUpdateBy() {
return updateBy;
}

public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}

public Date getUpdateTime() {
return updateTime;
}

public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}

@Override
public String toString() {
return "BaseEntity{" +
"createBy='" + createBy + '\'' +
", createDept='" + createDept + '\'' +
", creatTime=" + creatTime +
", updateBy='" + updateBy + '\'' +
", updateTime=" + updateTime +
'}';
}
}

  定义Dept: 

package cn.tedu.pojo;

public class Dept extends BaseEntity {
private String deptId;
// 上级部门
private Dept parentDept;
private String deptName;
private Integer state;

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 +
'}';
}
}

 定义DeptController:

    @Controller
    @RequestMapping("/sysadmin/dept/")
    public class DeptController {
      //里面定义部门的增删改查的方法
    }

 如下:

package cn.tedu.controller;

import cn.tedu.pojo.Dept;
import cn.tedu.service.DeptService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;

@Controller
@RequestMapping("/sysadmin/dept/")
public class DeptController {
@Autowired
private DeptService deptService;
//查询所有的部门信息
@RequestMapping("list")
public String list(Model model) {
List<Dept> depts = deptService.findAll();
model.addAttribute("deptList", depts);
return "/sysadmin/dept/jDeptList";
}
//点击禁用实现通用功能
@RequestMapping("stop")
public String stop(Model model, @RequestParam(value = "deptId", required = false) String[] deptIds) {
if (deptIds != null) {
for (String deptId : deptIds
) {
deptService.changeState(0, deptId);
}
}
//重定向当前页面
return "redirect:/sysadmin/dept/list";
}
//点击启用实现通用功能
@RequestMapping("start")
public String start(Model model, @RequestParam(value = "deptId", required = false) String[] deptIds) {
if (deptIds != null) {
for (String deptId : deptIds
) {
deptService.changeState(1, deptId);
}
}
//重定向当前页面
return "redirect:/sysadmin/dept/list";
}

//批量删除实现通用功能
@RequestMapping("delete")
public String delete(Model model, @RequestParam(value = "deptId", required = false) String[] deptIds) {
if (deptIds != null) {
//批量删除
deptService.deleteDeptByIds(deptIds);
}
//重定向当前页面
return "redirect:/sysadmin/dept/list";
}
//跳转到新增页面
@RequestMapping("tocreate")
public String create(Model model) {
List<Dept> depts = deptService.findAll();
model.addAttribute("depts", depts);
return "/sysadmin/dept/jDeptCreate";
}
//保存新增部门的信息的逻辑实现
@RequestMapping("save")
public String create(Model model, Dept dept) {
deptService.saveDept(dept);
return "redirect:/sysadmin/dept/list";
}
//跳转到更新页面,将该部门的信息展示在页面当中
@RequestMapping("toupdate")
public String update(Model model, String deptId) {
//根据id查询该部门的信息
Dept dept = deptService.findOneById(deptId);
model.addAttribute("dept",dept);
//得到所有的部门信息,用于下拉列表
List<Dept> depts = deptService.findAll();
model.addAttribute("depts", depts);
return "sysadmin/dept/jDeptUpdate";
}
//更新部门的逻辑实现
@RequestMapping("update")
public String updateDept(Model model, Dept dept) {
deptService.updateDept(dept);
return "redirect:/sysadmin/dept/list";
}
}

  在service层中:

 DeptService接口:

package cn.tedu.service;

import cn.tedu.pojo.Dept;

import java.util.List;

public interface DeptService {
public List<Dept> findAll();
public void changeState(int state,String deptId);

void deleteDeptByIds(String[] deptIds);

void saveDept(Dept dept);

Dept findOneById(String deptId);

void updateDept(Dept dept);
}

 DeptServiceImp实现类:   

package cn.tedu.service;

import cn.tedu.mapper.DeptMapper;
import cn.tedu.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImp implements DeptService{
@Autowired
private DeptMapper deptMapper;
public List<Dept> findAll() {
return deptMapper.findAll();
}

public void changeState(int state,String deptId) {
deptMapper.changeState(state,deptId);
}

public void deleteDeptByIds(String[] deptIds) {
deptMapper.deleteDeptByIds(deptIds);
}

public void saveDept(Dept dept) {
deptMapper.saveDept(dept);
}

public Dept findOneById(String deptId) {
return deptMapper.findOneById(deptId);
}

public void updateDept(Dept dept) {
deptMapper.updateDept(dept);
}
}

 定义DeptMapper接口    

package cn.tedu.mapper;

import cn.tedu.pojo.Dept;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface DeptMapper {
public List<Dept> findAll();
//注解会自动帮咱们两个参数封装到map中
public void changeState(@Param("state") int state, @Param("deptId") String deptId);

void deleteDeptByIds(String[] deptIds);

void saveDept(Dept dept);

Dept findOneById(String deptId);

void updateDept(Dept dept);
}

在mybatis/mapper/DeptMapper.xmlzhong定义sql的查询语句

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tedu.mapper.DeptMapper">
<resultMap id="deptRM" type="Dept" autoMapping="true">
<id column="dept_id" property="deptId"/>
<association property="parentDept" javaType="Dept">
<id column="pId" property="deptId"/>
<result column="pName" property="deptName"/>
</association>
</resultMap>
<select id="findAll" resultMap="deptRM">SELECT * FROM
dept_p d
left join
(select dept_id pId, dept_name pName from dept_p) p
on d.parent_id=p.pId;</select>
<update id="changeState">
update dept_p set state = #{state} where dept_id = #{deptId}
</update>
<delete id="deleteDeptByIds">
DELETE from dept_p WHERE dept_id IN
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
<insert id="saveDept">
INSERT INTO dept_p (dept_id,dept_name,parent_id,state)VALUES (#{deptId},#{deptName},#{parentDept.deptId},#{state})
</insert>
<select id="findOneById" resultMap="deptRM">
SELECT * FROM
dept_p d
left join
(select dept_id pId, dept_name pName from dept_p) p
on d.parent_id=p.pId WHERE d.dept_id = #{deptId};
</select>
<update id="updateDept">
UPDATE dept_p SET dept_name = #{deptName},parent_id = #{parentDept.deptId},state=#{state} WHERE dept_id = #{deptId}
</update>
</mapper>


5.演示效果:

  部门列表:      新增部门:

  

  修改部门:

   

点击修改即可成功。

以上就是第一天所有的东西了!!!!!!

项目源代码地址:

https://gitee.com/smeb1/HuiTongday01/tree/master/

posted @ 2018-01-03 19:08  sky丶一念初见  阅读(159)  评论(0编辑  收藏  举报