今日内容
- 环境搭建
- AdminLTE前端框架
- 代码优化
- 列表分页插件
第一章 项目环境搭建(仔细)
1. 建立数据环境
直接导入提供的saas-export.sql
2. 建立分模块工程
2.1 模块设计
![image](https://i-blog.csdnimg.cn/blog_migrate/d813333bf13644368ef0341d9eba2a7b.png)
2.2 创建export_parent父工程
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>export_parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>export_commons</module>
</modules>
<!-- 集中定义依赖版本号 -->
<properties>
<junit.version>4.12</junit.version>
<spring.version>5.1.6.RELEASE</spring.version>
<pagehelper.version>5.1.2</pagehelper.version>
<servlet-api.version>2.5</servlet-api.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mysql.version>5.1.32</mysql.version>
<druid.version>1.0.9</druid.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<activemq.version>5.11.2</activemq.version>
</properties>
<!--依赖管理-->
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 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.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<!-- dubbo2.6.X以上的版本开始 2.8.4不需要 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<!-- 从自动根据这个区间下载最新的版本,如果报错你就改成7.2.20或7.2.27 -->
<version>[7.2.0,7.2.99]</version>
</dependency>
<!--json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<!--shiro-->
<!--shiro和spring整合-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--shiro核心包-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<!--rabbitMq-->
<!--Mq-->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
<!-- Javamail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<!-- cxf 进行rs开发 必须导入 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.5.0</version>
</dependency>
<dependency>
<groupId>org.olap4j</groupId>
<artifactId>olap4j</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<!--定时任务 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<!--Hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.3 创建export_commons模块
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>export_parent</artifactId>
<groupId>com.itheima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>export_commons</artifactId>
</project>
2.4 创建export_domain模块
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>export_parent</artifactId>
<groupId>com.itheima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>export_domain</artifactId>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>export_commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.5 创建export_dao模块
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>export_parent</artifactId>
<groupId>com.itheima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>export_dao</artifactId>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>export_domain</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.6 创建export_system_service模块
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>export_parent</artifactId>
<groupId>com.itheima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>export_system_service</artifactId>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>export_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.7 创建export_manager_web模块
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>export_parent</artifactId>
<groupId>com.itheima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>export_manager_web</artifactId>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>export_system_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
3. 加入SSM环境
以企业的查询为例子做SSM的整合
3.1 修改export_domain模块
创建Company实体类
package com.itheima.domain.company;
import java.util.Date;
public class Company implements Serializable {
/**
* 属性名必须是驼峰命名
* 注意属性类型
*/
private String id;
private String name;
private Date expirationDate;
private String address;
private String licenseId;
private String representative;
private String phone;
private String companySize;
private String industry;
private String remarks;
private Integer state;
private Double balance;
private String city;
}
3.2 修改export_dao模块
3.2.1 创建dao的接口
package com.itheima.dao.company;
import com.itheima.domain.company.Company;
import java.util.List;
/**
* 创建dao接口
*/
public interface CompanyDao {
/**
* 查询列表
*/
List<Company> findAll();
}
3.2.2 创建映射文件
<mapper namespace="com.itheima.dao.company.CompanyDao">
<!--自定义文件映射-->
<resultMap id="BaseResultMap" type="com.itheima.domain.company.Company">
<id property="id" column="id"/>
<result property="name" column="name" />
<result property="expirationDate" column="expiration_date" />
<result property="address" column="address" />
<result property="licenseId" column="license_id" />
<result property="representative" column="representative" />
<result property="phone" column="phone" />
<result property="companySize" column="company_size" />
<result property="industry" column="industry" />
<result property="remarks" column="remarks" />
<result property="state" column="state" />
<result property="balance" column="balance" />
<result property="city" column="city" />
</resultMap>
<select id="findAll" resultMap="BaseResultMap">
select * from ss_company
</select>
</mapper>
3.2.3 加入配置文件
druid SqlSessionFactoryBean Mapper
resources\config\jdbc.properties
resources\log4j.properties
resources\spring\applicationContext-dao.xml
3.2.4 测试
.class) (SpringJUnit4ClassRunner
@ContextConfiguration("classpath:spring/applicationContext-dao.xml")
public class DaoTest {
private CompanyDao companyDao;
public void testFindAll(){
List<Company> companies = companyDao.findAll();
for (Company company : companies) {
System.out.println(company);
}
}
}
3.3 修改export_system_service模块
3.3.1 创建service接口
package com.itheima.service.company;
import com.itheima.domain.company.Company;
import java.util.List;
public interface CompanyService {
List<Company> findAll();
}
3.3.2 创建service实现类
public class CompanyServiceImpl implements CompanyService {
private CompanyDao companyDao;
public List<Company> findAll() {
return companyDao.findAll();
}
}
3.3.3 加入spring的配置文件
1 组件扫描 2 事务处理
resources/spring/applicationContext-service.xml
3.3.4 测试
.class) (SpringJUnit4ClassRunner
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
public class ServiceTest {
private CompanyService companyService;
public void testFindAll(){
List<Company> companies = companyService.findAll();
for (Company company : companies) {
System.out.println(company);
}
}
}
3.4 修改export_manager_web模块
3.4.1 加入springmvc的配置文件
1.组件扫描 2. 三大组件
resources/spring/springmvc.xml
3.4.2 加入web.xml
1 spring的监听器 2 springmvc的前端控制器 3 中文乱码过滤器
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><!--spring监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.4.3 编写controller
package com.itheima.web.controller.company;
import com.itheima.domain.company.Company;
import com.itheima.service.company.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
"/company") (
public class CompanyController {
private CompanyService companyService;
/**
* 查看列表
* name字段主要用于打印日志
*/
"/list",name = "企业列表查询") (value =
public String list(HttpServletRequest request){
List<Company> companies = companyService.findAll();
request.setAttribute("list",companies);
return "company/company-list";
}
}
3.4.4 部署项目
JRebel查询,选择全部部署,选择debug运行
3.4.5 访问测试
手动输入地址:http://localhost:8080/company/list.do 访问
4. 添加前台页面
在资料中找到项目页面的文件夹, 里面有两个文件:webapp和LoginController.java
1) 将webapp下的所有文件复制到当前环境的webapp下, 提示重复的直接覆盖掉
2) 将LoginController.java复制到controller包下
重启tomcat, 然后再次访问http://localhost:8080/company/list.do
第二章 AdminLTE(了解)
1. AdminLTE简介
AdminLTE是一款开源的模板主题工具,它提供了多个模板页面, 通过它我们可以快速的创建一个响应式的Html5网站。
它的代码托管在git上, 我们可以访问 https://github.com/almasaeed2010/AdminLTE获取AdminLTE源代码。
在今天的资料中给大家提供了两个版本的代码:
- AdminLTE-2.4.10 官方的包
\AdminLTE-2.4.10\index.html
- adminlte2-itheima-master 传智做了一部分汉化的包
\adminlte2-itheima-master\release\dist\pages\all-admin-index.html
2. AdminLTE使用入门
自定义一个框架页面, 在webapp下面新建一个adminLTE-demo.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--美化页面-->
<link rel="stylesheet" href="plugins/adminLTE/css/AdminLTE.css">
<link rel="stylesheet" href="plugins/bootstrap/css/bootstrap.css">
<link rel="stylesheet" href="plugins/adminLTE/css/skins/_all-skins.css">
</head>
<body class="hold-transition skin-purple sidebar-mini">
<!--主体div-->
<div class="wrapper">
<!-- 页面头部 -->
<header class="main-header">
<!-- Logo -->
<a class="logo">
数据后台管理
</a>
<!--nav-->
<nav class="navbar navbar-static-top">
<span class="hidden-xs">张猿猿</span>
</nav>
</header>
<!-- 导航侧栏 -->
<aside class="main-sidebar">
<section class="sidebar">
<div class="user-panel">
张猿猿
</div>
<ul class="treeview-menu">
<li>
<a href="https://www.taobao.com/" target="myIframe">
淘宝
</a>
</li>
<li>
<a href="https://www.baidu.com/" target="myIframe">
百度
</a>
</li>
<li>
<a href="http://localhost:8080/company/list.do" target="myIframe">
百度
</a>
</li>
</ul>
</section>
</aside>
<!-- 内容区域 -->
<div class="content-wrapper">
<!--iframe-->
<iframe name="myIframe" width="100%" height="800px" src="https://www.itcast.cn/">
</iframe>
</div>
<!-- 底部导航 -->
<footer class="main-footer">底部导航</footer>
</div>
</body>
</html>
3. 当前请求的访问流程
为什么访问http://localhost:8080就能显示出一个漂亮的页面?
![image](https://i-blog.csdnimg.cn/blog_migrate/b66fb6eeecf73b3b3b40ee6230dc99b6.png)
第三章 企业管理(重点)
1. 新建企业
1.1 思路说明
点击新建按钮—到达后台控制器—转发到新建页面–填写表单,执行新建—到达后台—执行保存数据—重定向到列表查询方法
1.2 CompanyController
/**
* 添加功能
*/
"/toAdd",name = "跳转到新增企业的页面") (value =
public String toAdd(){
return "company/company-add";
}
"/edit",name = "新增企业") (value =
public String edit(Company company){
// 设置id
company.setId(UUID.randomUUID().toString());
// 调用service保持
companyService.save(company);
// 重定向到列表页
return "redirect:/company/list.do";
}
1.3 CompanyService
void save(Company company);
public void save(Company company) {
companyDao.save(company);
}
1.4 CompanyDao
void save(Company company);
<insert id="save">
insert into ss_company (
id,
name,
expiration_date,
address,
license_id,
representative,
phone,
company_size,
industry,
remarks,
state,
balance,
city
) values (
#{id},
#{name},
#{expirationDate},
#{address},
#{licenseId},
#{representative},
#{phone},
#{companySize},
#{industry},
#{remarks},
#{state},
#{balance},
#{city}
);
</insert>
类型转换异常的处理
package com.itheima.domain.company;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class Company implements Serializable{
/**
* 属性名必须是驼峰命名
* 注意属性类型
*/
private String id;
private String name;
/**
* 类型转换器
* 注解形式: 实现简单, 每个实体类中,只要有类型需要转换都要加这个注解
* 编码形式: 实现麻烦, 只需要实现一次
*/
"yyyy-MM-dd") (pattern =
private Date expirationDate;
private String address;
private String licenseId;
private String representative;
private String phone;
private String companySize;
private String industry;
private String remarks;
private Integer state;
private Double balance;
private String city;
}
2. 编辑企业
2.1 思路说明
点击编辑按钮—到达后台控制器(准备回显数据)—转发到编辑页面–填写表单,执行修改—到达后台—执行修改数据—重定向到列表查询方法
2.2 进入编辑页面
2.2.1 CompanyController
/**
* 修改功能数据回显
*/
"/toUpdate",name = "跳转到修改企业的页面") (value =
public String toUpdate(String id,HttpServletRequest request){
// 根据id查询
Company company = companyService.findById(id);
// 保存的request
request.setAttribute("company",company);
// 转发页面
return "company/company-update";
}
2.2.2 CompanyService
Company findById(String id);
public Company findById(String id) {
return companyDao.findById(id);
}
2.2.3 CompanyDao
Company findById(String id);
<select id="findById" resultMap="BaseResultMap">
select * from ss_company where id = #{id}
</select>
2.3 编辑企业
2.3.1 CompanyController
/**
* 修改、新增公用同一个方法
*/
"/edit",name = "新增企业") (value =
public String edit(Company company){
// 判断id是否为空,空进行新增操作,非空进行修改操作
if (StringUtils.isEmpty(company.getId())) {
// 设置id
company.setId(UUID.randomUUID().toString());
// 调用service保持
companyService.save(company);
// 重定向到列表页
}else {
companyService.update(company);
}
return "redirect:/company/list.do";
}
2.3.2 CompanyService
void update(Company company);
public void update(Company company) {
companyDao.update(company);
}
2.3.3 CompanyDao
void update(Company company);
<update id="update">
update ss_company
<set>
<if test="name != null and name != ''">
name=#{name},
</if>
<if test="expirationDate != null">
expiration_date=#{expirationDate},
</if>
<if test="address != null and address != ''">
address=#{address},
</if>
<if test="licenseId != null and licenseId != ''">
license_id=#{licenseId},
</if>
<if test="representative != null and representative != ''">
representative=#{representative},
</if>
<if test="phone != null and phone != ''">
phone=#{phone},
</if>
<if test="companySize != null and companySize != ''">
company_size=#{companySize},
</if>
<if test="industry != null and industry != ''">
industry=#{industry},
</if>
<if test="remarks != null and remarks != ''">
remarks=#{remarks},
</if>
<if test="state != null">
state=#{state},
</if>
<if test="balance != null">
balance=#{balance},
</if>
<if test="city != null and city != ''">
city=#{city},
</if>
</set>
where id = #{id}
</update>
3. 删除企业
3.1 思路说明
点击删除按钮—到达后台控制器—执行删除数据—重定向到列表查询方法
3.2 CompanyController
/**
* 删除内容
*/
"/delete",name = "删除企业") (value =
public String delete(String id){
// 调用service删除
companyService.delete(id);
// 重定向到list
return "redirect:/company/list.do";
}
3.3 CompanyService
void deleteById(String id);
public void deleteById(String id) {
companyDao.deleteById(id);
}
3.4 CompanyDao
void deleteById(String id);
<delete id="deleteById">
delete from ss_company where id = #{id}
</delete>
4. 逻辑删除
在大数据时代,数据库中的所有记录会用于数据统计,商业分析等。通过物理删除(delete方式删除)的方式删除磁盘内容是极不推荐的做法。
这就有了逻辑删除的方式,逻辑删除就是为要删除的数据打上一个删除标记,而不真正删掉数据,但是注意查询的时候要查询未打删除标记的数据。
逻辑删除:
(1)修改ss_company,添加一个字段is_deleted,默认值是0, 0表示正常数据 ,1表示删除数据
(2)修改查询企业的方法,只查询is_deleted状态是0的数据
<!--查询所有的企业列表-->
<select id="findAll" resultMap="BaseResultMap">
select * from ss_company where is_deleted=0
</select>
(3)修改原来的删除方法,此时的删除不是delete 而应该是update修改is_deleted的状态为1
<delete id="deleteById" parameterType="string">
update ss_company set is_deleted=1 where id=#{id}
</delete>
小结
所谓逻辑删除就是在表中加入类似is_deleted
字段,将删除操作变成更新操作。
- 当is_deleted=1时就代表这条记录已经删除。
- 当is_deleted=0时就代表这条记录真实有效。
- 在展示数据列表的时候,需要查询所有is_deleted=0的数据
第四章 代码优化
1. 全局异常处理器
全局异常处理器
方式一: 自定义一个类去实现
ExceptionHandlerResolver
方式二: 注解
@ControllerAdvice标在类上 @ExceptionHandler(value)表在方法上, 指定的value代表当前方法可以处理的异常类型
在com.itheima.web.handlers
包下创建CommonExceptionHandler
package com.itheima.web.handlers;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
public class CommonExceptionHandler {
//处理所有的Exception及其子类
.class) (Exception
public String exceptionHandler(Exception e, HttpServletRequest request) {
//1. 打印异常,给程序员
e.printStackTrace();
request.setAttribute("errorMsg", e.getMessage());
//2. 返回页面, 给用户
return "error";
}
}
2. BaseController抽取
2.1 抽取BaseController
package com.itheima.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class BaseController {
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
}
2.2 修改CompanyController
其他的Controller继承BaseController, 然后方法声明中的request就可以全部去掉了
package com.itheima.web.controller.company;
import com.itheima.domain.company.Company;
import com.itheima.service.company.CompanyService;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.UUID;
"/company") (
public class CompanyController extends BaseController {
private CompanyService companyService;
/**
* 查看列表
* name字段主要用于打印日志
*/
"/list",name = "企业列表查询") (value =
public String list(){
List<Company> companies = companyService.findAll();
request.setAttribute("list",companies);
return "company/company-list";
}
/**
* 添加功能
*/
"/toAdd",name = "跳转到新增企业的页面") (value =
public String toAdd(){
return "company/company-add";
}
/**
* 修改、新增公用同一个方法
*/
"/edit",name = "新增企业") (value =
public String edit(Company company){
// 判断id是否为空,空进行新增操作,非空进行修改操作
if (StringUtils.isEmpty(company.getId())) {
// 设置id
company.setId(UUID.randomUUID().toString());
// 调用service保持
companyService.save(company);
// 重定向到列表页
}else {
companyService.update(company);
}
return "redirect:/company/list.do";
}
/**
* 修改功能数据回显
*/
"/toUpdate",name = "跳转到修改企业的页面") (value =
public String toUpdate(String id){
// 根据id查询
Company company = companyService.findById(id);
// 保存的request
request.setAttribute("company",company);
// 转发页面
return "company/company-update";
}
/**
* 删除内容
*/
"/delete",name = "删除企业") (value =
public String delete(String id){
// 调用service删除
companyService.delete(id);
// 重定向到list
return "redirect:/company/list.do";
}
}
第五章 企业列表分页(会用)
1. 分页思想回顾
![image](https://i-blog.csdnimg.cn/blog_migrate/c7b1d6bcdaae6136a1384938a24c25f9.png)
2. 传统分页
2.1 准备PageInfo
com.itheima.vo.PageInfo
2.2 CompanyController
"/list", name = "企业列表查询") (value =
public String list(
@RequestParam(defaultValue = "1", name = "page") Integer pageNum,
@RequestParam(defaultValue = "1") Integer pageSize) {
PageInfo pageInfo = companyService.findByPage(pageNum, pageSize);
request.setAttribute("page", pageInfo);
return "/company/company-list";
}
2.3 CompanyService
PageInfo findByPage(Integer pageNum, Integer pageSize);
public PageInfo findByPage(Integer pageNum, Integer pageSize) {
//1. 查询total 总记录数
Long total = companyDao.findTotal();
//2. 查询list 数据集合
int startIndex = (pageNum - 1) * pageSize;
List<Company> list = companyDao.findList(startIndex, pageSize);
return new PageInfo(pageNum, pageSize, list, total);
}
2.4 CompanyDao
Long findTotal();
List<Company> findList(@Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);
<select id="findTotal" resultType="java.lang.Long">
select count(1) from ss_company;
</select>
<select id="findList" resultMap="BaseResultMap">
select * from ss_company limit #{startIndex},#{pageSize}
</select>
2.5 修改页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../base.jsp"%>
<!DOCTYPE html>
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>数据 - AdminLTE2定制版</title>
<meta name="description" content="AdminLTE2定制版">
<meta name="keywords" content="AdminLTE2定制版">
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
<!-- 页面meta /-->
<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
</head>
<script>
function deleteById() {
var id = getCheckId()
if(id) {
if(confirm("你确认要删除此条记录吗?")) {
location.href="/company/delete.do?id="+id;
}
}else{
alert("请勾选待处理的记录,且每次只能勾选一个")
}
}
</script>
<body>
<div id="frameContent" class="content-wrapper" style="margin-left:0px;">
<section class="content-header">
<h1>
企业管理
<small>企业列表</small>
</h1>
<ol class="breadcrumb">
<li><a href="all-admin-index.html"><i class="fa fa-dashboard"></i> 首页</a></li>
</ol>
</section>
<!-- 内容头部 /-->
<!-- 正文区域 -->
<section class="content">
<!-- .box-body -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">列表</h3>
</div>
<div class="box-body">
<!-- 数据表格 -->
<div class="table-box">
<!--工具栏-->
<div class="pull-left">
<div class="form-group form-inline">
<div class="btn-group">
<button type="button" class="btn btn-default" title="新建" onclick='location.href="/company/toAdd.do"'><i class="fa fa-file-o"></i> 新建</button>
<button type="button" class="btn btn-default" title="删除" onclick='deleteById()'><i class="fa fa-trash-o"></i> 删除</button>
<button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
</div>
</div>
</div>
<div class="box-tools pull-right">
<div class="has-feedback">
<input type="text" class="form-control input-sm" placeholder="搜索">
<span class="glyphicon glyphicon-search form-control-feedback"></span>
</div>
</div>
<!--工具栏/-->
<!--数据列表-->
<table id="dataList" class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right:0px;">
</th>
<th class="sorting">企业名称</th>
<th class="sorting">所在地</th>
<th class="sorting">地址</th>
<th class="sorting">企业法人</th>
<th class="sorting">联系方式</th>
<th class="sorting">所属行业</th>
<th class="sorting">状态</th>
<th class="sorting">余额</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${page.list}" var="item">
<tr>
<td><input name="ids" value="${item.id}" type="checkbox"></td>
<td>
${item.name}
</td>
<td>${item.city}</td>
<td>${item.address}</td>
<td>${item.representative}</td>
<td>${item.phone}</td>
<td>${item.industry}</td>
<td>${item.state ==0?'未审核':'已审核'}</td>
<td class="text-center">${item.balance}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs" onclick='location.href="/company/toUpdate.do?id=${item.id}"'>编辑</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<!-- /.box-body -->
<!-- .box-footer-->
<%--<div class="box-footer">
<div class="box-tools pull-right">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">首页</a>
</li>
<li><a href="#">上一页</a></li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">下一页</a></li>
<li>
<a href="#" aria-label="Next">尾页</a>
</li>
</ul>
</div>
</div>--%>
<div class="box-footer">
<jsp:include page="../common/page.jsp">
<jsp:param value="${ctx}/company/list.do" name="pageUrl"/>
</jsp:include>
</div>
<!-- /.box-footer-->
</div>
</section>
</div>
</body>
</html>
3. PageHelper插件介绍
PageHelper是一款优秀的开源mybatis分页插件,它支持常用的数据库,例如 mysql、oracle、mariaDB、DB2、SQLite、Hsqldb 等。
网址:https://pagehelper.github.io/
PageHelper的使用非常简单,主要包括下面3步:
第1步:引入坐标(已处理)
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
第2步:添加配置
export_dao/src/main/resources/spring/applicationContext-dao.xml
在SqlSessionFactoryBean的配置中添加一个plugins项
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
第3步:开始使用
// 设置要查询的页码和每页大小
PageHelper.startPage(pageNum,pageSize);
// 执行一个查询,注意这个查询中不用再去写分页条件(limit)
List<Company> list = companyDao.findAll();
// 组装一个PageInfo,注意这里的PageInfo不是我们自己的,是插件提供的
// 参数1: 上一步查询得到的集合 参数2: 页面显示几个页码
return new PageInfo<Company>(list,10);
4. PageHelper实现分页(重点)
4.1 添加插件的配置
在applicationContext-dao.xml配置文件中的SqlSessionFactoryBean的配置中添加一个plugins项
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
4.2 切换PageInfo
删掉我们自定义的com.itheima.vo.PageInfo
类, 改用插件提供的com.github.pagehelper.PageInfo
替代
4.3 修改CompanyServiceImpl
修改CompanyServiceImpl中分页实现
public PageInfo findByPage(Integer pageNum, Integer pageSize) {
//1. 设置pageNum和pageSize
PageHelper.startPage(pageNum,pageSize);
//2. 调用一个查询所有的方法
List<Company> list = companyDao.findAll();
//3. 直接返回PageInfo
return new PageInfo(list,10);
}
5. 分页原理
![image](https://i-blog.csdnimg.cn/blog_migrate/ab8533c58785068dcaa06510f40dee59.png)