企业权限管理项目一


一、 AdminLTE

1.AdminLTE介绍

AdminLTE是一款建立在bootstrap和jquery之上的开源的模板主题工具,它提供了一系列响应的、
可重复使用的组件,并内置了多个模板页面;同时自适应多种屏幕分辨率,兼容PC和移动端。通
过AdminLTE,我们可以快速的创建一个响应式的Html5网站。AdminLTE框架在网页架构与设计
上,有很大的辅助作用,尤其是前端架构设计师,用好AdminLTE 不但美观,而且可以免去写很大
CSS与JS的工作量。

AdminLTE依赖于两个框架Bootstrap3与JQuery1.11+。

2.AdminLTE基本使用介绍

布局:
.wrapper包住了body下的所有代码
.main-header里是网站的logo和导航栏的代码
.main-sidebar里是用户面板和侧边栏菜单的代码
.content-wrapper里是页面的页面和内容区域的代码
.main-footer里是页脚的代码
.control-sidebar里是页面右侧侧边栏区域的代码

布局选项:
fixed:固定
layout-boxed:盒子布局
layout-top-nav:顶部隐藏
sidebar-collapse:侧边栏隐藏
sidebar-mini:侧边栏隐藏时有小图标

皮肤:
skin-blue:蓝色
skin-black:黑色
skin-purple:紫色
skin-yellow:黄色
skin-red:红色
skin-green:绿色

3.AdminLTE使用示例-首页

在这里插入图片描述

4.AdminLTE使用示例-产品添加

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、SSM整合案例的基本介绍

1.SSM综合练习功能介绍

主要讲解Maven工程搭建,以及基于MySQL数据库的商品表信息,并完成SSM整合。

1. 商品查询
基于SSM整合基础上完成商品查询,要掌握主面页面main.jsp及商品显示页面product-list.jsp页面的创建。

2. 商品添加
进一步巩固SSM整合,并完成商品添加功能,要注意事务操作以及product-add.jsp页面生成。

3. 订单查询
订单的查询操作,它主要完成简单的多表查询操作,查询订单时,需要查询出与订单关联的其它表中信息,要了解订单及其它表关联关系。

4. 订单分页查询
订单分页查询,使用的是mybatis分页插件PageHelper,要掌握PageHelper的基本使用。

5. 订单详情查询
订单详情是用于查询某一个订单的信息。

6. Spring Security 概述
 Spring Security是 Spring 项目组中用来提供安全认证服务的框架,要掌握spring Security框架的配置及基本的认证与授权操作。

7. 用户管理
用户管理中掌握基于spring Security的用户登录、退出操作。以及用户查询、添加、详情有等操作,这些功能
的练习是对前期SSM知识点的进一步巩固。

8. 角色管理
角色管理主要完成角色查询、角色添加。

9. 资源权限管理
资源权限管理主要完成查询、添加操作,它的操作与角色管理类似,角色管理以及资源权限管理都是对权限管理的
补充。

10. 权限关联与控制
主要会讲解用户角色关联、角色权限关联,这两个操作是为了后续完成授权操作的基础,关于授权操作会在服务器端及页面端分别完成。

11. AOP日志处理
 AOP日志处理,使用spring AOP切面来完成系统级别的日志收集。

2.SSM综合练习表结构介绍

1. 产品表:
在这里插入图片描述

2. 订单表:旅客表与订单表多对多关系
在这里插入图片描述

3. 会员表:
在这里插入图片描述

4. 旅客表:
在这里插入图片描述

5. 用户表:角色表和用户表多对多关系
在这里插入图片描述

6. 角色表:资源权限表和角色表多对多关系
在这里插入图片描述

7. 资源权限表:
在这里插入图片描述

8. 日志表:
在这里插入图片描述

三、产品操作

1.数据库与表结构介绍

1. 如果id采用int类型:

CREATE DATABASE erm; -- 创建企业权限管理数据库

USE erm; -- 使用企业权限管理数据库

CREATE TABLE product(
	id INT PRIMARY KEY AUTO_INCREMENT, -- 无意义,主键
	productNum VARCHAR(50) NOT NULL UNIQUE, -- 产品编号,唯一且不为空
	productName VARCHAR(50), -- 产品名称(路线名称)
	cityName VARCHAR(50),	-- 出发城市
	departureTime TIMESTAMP, -- 出发时间
	productPrice DOUBLE(11,2), -- 产品价格
	productDesc VARCHAR(500), -- 产品描述
	productStatus INT -- 产品状态(0:关闭,1:开启)
);

INSERT INTO PRODUCT VALUES (NULL , 'product001', '德州三日游', '德州', '20220529143800', 1000, '家乡的路线', 0);

INSERT INTO PRODUCT VALUES (NULL , 'product002', '济南三日游', '济南', '20220530130000', 2000, '成长的地方', 0);

INSERT INTO PRODUCT VALUES (NULL , 'product003', '淄博三日游', '淄博', '20220429113200', 1800, '留恋的城市', 1);

INSERT INTO PRODUCT VALUES (NULL , 'product004', '北京三日游', '北京', '20220610090800', 2500, '不错的旅行', 1);

2. 如果id采用varchar类型:

CREATE DATABASE ssm;

USE ssm;

CREATE TABLE product(
	
	id VARCHAR(32) PRIMARY KEY,
	productNum VARCHAR(50) NOT NULL UNIQUE,
	productName VARCHAR(50),
	cityName VARCHAR(50),
	departureTime TIMESTAMP,
	productPrice DOUBLE,
	productDesc VARCHAR(500),
	productStatus INT
);

INSERT INTO PRODUCT
VALUES (id , 'itcast-001', '北京三日游', '北京', '20220529143800', 1200, '不错的旅行', 1);

-------------------------------------------------------------------------------------------
DELIMITER $$

CREATE
    /*[DEFINER = { user | CURRENT_USER }]*/
    TRIGGER `ssm`.`id_test` BEFORE INSERT	-- 触发器名称
    ON `ssm`.`product` 		-- 触发器被触发的时机
    FOR EACH ROW BEGIN 		-- 触发器所作用的表名称
	SET new.id=REPLACE(UUID(),'-',''); -- 触发器执行的逻辑
    END$$

DELIMITER ;

在这里插入图片描述

2.SSM环境搭建

1.创建项目:myprj_erm
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

2. 创建模块:以myprj_erm_dao举例,需要创建dao、service、domain、utils子模块
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. 创建模块:web模块需要使用骨架
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4. pom.xml:

    <properties>
	    <!-- 版本锁定 -->
        <spring.version>5.0.2.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <mysql.version>5.1.6</mysql.version>
        <mybatis.version>3.4.5</mybatis.version>
        <spring.security.version>5.0.1.RELEASE</spring.security.version>
    </properties>

	<!-- spring核心包 -->  
	<!-- spring AOP -->  
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</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-orm</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-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

		<!--spring整合junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
		<!--servlet环境-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

		<!-- JSTL标签类 -->  
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency> 
        
        <!-- 日志文件管理包 -->  
        <!-- log start -->  
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log end -->  

		<!-- mybatis核心包及spring整合mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

		 <!-- 数据库连接池 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <!--<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>-->

		<!--security依赖-->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.version}</version>
        </dependency>

		<!--mysql环境-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!--<dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>-->
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
            <!-- 编译器 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

5. domain:
在这里插入图片描述

Product.java

package cn.fsm.myprj.domain;

import java.util.Date;

/**
 * 产品
 */
public class Product {
    private int id;  //主键
    private String productNum;  //编号 唯一
    private String productName;  //名称
    private String cityName;  //出发城市
    private Date departureTime;  //出发时间
    private String departureTimeStr;
    private double productPrice;  //产品价格
    private String productDesc;  //产品描述
    private Integer productStatus;  //状态  0关闭 1开启
    private String productStatusStr;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProductNum() {
        return productNum;
    }

    public void setProductNum(String productNum) {
        this.productNum = productNum;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public Date getDepartureTime() {
        return departureTime;
    }

    public void setDepartureTime(Date departureTime) {
        this.departureTime = departureTime;
    }

    public String getDepartureTimeStr() {
        return departureTimeStr;
    }

    public void setDepartureTimeStr(String departureTimeStr) {
        this.departureTimeStr = departureTimeStr;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    public String getProductDesc() {
        return productDesc;
    }

    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }

    public Integer getProductStatus() {
        return productStatus;
    }

    public void setProductStatus(Integer productStatus) {
        this.productStatus = productStatus;
    }

    public String getProductStatusStr() {
        return productStatusStr;
    }

    public void setProductStatusStr(String productStatusStr) {
        this.productStatusStr = productStatusStr;
    }
}

6. dao:
在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>myprj_erm</artifactId>
        <groupId>cn.fsm.myprj_erm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>myprj_erm_dao</artifactId>
    <dependencies>
        <dependency>
            <groupId>cn.fsm.myprj_erm</groupId>
            <artifactId>myprj_erm_domain</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>
package cn.fsm.myprj.dao;

import cn.fsm.myprj.domain.Product;

import java.util.List;

/**
 * 产品
 */
public interface IProductDao {

    // 查询所有产品
    public List<Product> findAll() throws Exception;
}

7. service:
在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>myprj_erm</artifactId>
        <groupId>cn.fsm.myprj_erm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>myprj_erm_service</artifactId>
    <dependencies>
        <dependency>
            <groupId>cn.fsm.myprj_erm</groupId>
            <artifactId>myprj_erm_dao</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>cn.fsm.myprj_erm</groupId>
            <artifactId>myprj_erm_domain</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

IProductService

package cn.fsm.myprj.service;

import cn.fsm.myprj.domain.Product;

import java.util.List;

public interface IProductService {

    public List<Product> findAll() throws Exception;
}

ProductServiceImpl

package cn.fsm.myprj.service.impl;

import cn.fsm.myprj.dao.IProductDao;
import cn.fsm.myprj.domain.Product;
import cn.fsm.myprj.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {

    @Autowired
    private IProductDao productDao;

    @Override
    public List<Product> findAll() throws Exception {
        return productDao.findAll();
    }
}

整合配置文件:
在这里插入图片描述

8. db.properties:配置数据库配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/erm
jdbc.username=root
jdbc.password=root

9. 配置applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/context
	http://www.springframework.org/schema/context/spring-context.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">

    <!-- 开启注解扫描,管理service和dao -->
    <context:component-scan base-package="cn.fsm.myprj.service">
    </context:component-scan>
    <context:component-scan base-package="cn.fsm.myprj.dao">
    </context:component-scan>

    <!-- 加载db.properties -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- 把交给IOC管理 SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 扫描dao接口 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.fsm.myprj.dao"/>
    </bean>

    <!-- 配置Spring的声明式事务管理 -->
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 开启事务注解支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

10. 配置spring-mvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
           ">

    <!-- 扫描controller的注解,别的不扫描 -->
    <context:component-scan base-package="com.itheima.ssm.controller">
    </context:component-scan>

    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- JSP文件所在的目录 -->
        <property name="prefix" value="/pages/" />
        <!-- 文件的后缀名 -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 设置静态资源不过滤 -->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/img/" mapping="/img/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/plugins/" mapping="/plugins/**" />

    <!-- 开启对SpringMVC注解的支持 -->
    <mvc:annotation-driven />

    <!--
	    支持AOP的注解支持,AOP底层使用代理技术
	    JDK动态代理,要求必须有接口
	    cglib代理,生成子类对象,proxy-target-class="true" 默认使用cglib的方式
	-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

11. web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <!-- 配置监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置加载类路径的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>

  <!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
    <load-on-startup>1</load-on-startup>
  </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>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3.产品操作-查询全部产品1

1. 创建controller:

package cn.fsm.myprj.erm.controller;

import cn.fsm.myprj.domain.Product;
import cn.fsm.myprj.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Product> productList = productService.findAll();
        modelAndView.addObject("", productList);
        modelAndView.setViewName("");
        return modelAndView;
    }
}

2. 导入IProductService依赖和配置tomcat7插件:
在这里插入图片描述
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>

<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>cn.fsm.myprj_erm</groupId>
  <artifactId>myprj_erm_web</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>myprj_erm_web Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>cn.fsm.myprj_erm</groupId>
      <artifactId>myprj_erm_domain</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>cn.fsm.myprj_erm</groupId>
      <artifactId>myprj_erm_service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>myprj_erm_web</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <port>8888</port>
        </configuration>

        <version>2.2</version>
      </plugin>
    </plugins>
  </build>
</project>

4.产品操作-查询全部产品2

在这里插入图片描述

1. 创建pages文件夹:将product-list.jsp存入pages文件夹中
在这里插入图片描述

2. 修改index.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>

<html>
<body>
<a href="${pageContext.request.contextPath}/product/findAll.do">查询所有的产品信息</a>
</body>
</html>

3. 将页面资源复制到webapp目录下:
在这里插入图片描述

5.产品操作-查询全部产品3

1. 根据product-list.jsp页面循环遍历的数组名称:

package cn.fsm.myprj.erm.controller;

import cn.fsm.myprj.domain.Product;
import cn.fsm.myprj.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Product> productList = productService.findAll();
        modelAndView.addObject("productList", productList);
        modelAndView.setViewName("product-list");
        return modelAndView;
    }
}

2. 添加product-list.jsp头部、导航侧栏的文件到pages文件夹:
在这里插入图片描述

3. idea配置运行:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4. 解决所有产品信息列表不显示产品状态、日期(含转换):
在这里插入图片描述

package cn.fsm.erm.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
    /**
     * 日期转换成字符串
     * @param targetDate 目标日期
     * @param formatString 格式字符串
     * @return
     */
    public static String dateToString(Date targetDate, String formatString) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
        return simpleDateFormat.format(targetDate);
    }

    /**
     * 字符串转换成日期
     * @param targetString 目标字符串
     * @param formatString 格式字符串
     * @return
     * @throws ParseException
     */
    public static Date stringToDate(String targetString, String formatString) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
        return simpleDateFormat.parse(targetString);
    }
}

package cn.fsm.erm.domain;

import cn.fsm.erm.utils.DateUtils;

import java.util.Date;

public class Product {
    private int id;  //主键
    private String productNum;  //编号 唯一
    private String productName;  //名称
    private String cityName;  //出发城市
    private Date departureTime;  //出发时间
    private String departureTimeStr;
    private double productPrice;  //产品价格
    private String productDesc;  //产品描述
    private Integer productStatus;  //状态  0关闭 1开启
    private String productStatusStr;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProductNum() {
        return productNum;
    }

    public void setProductNum(String productNum) {
        this.productNum = productNum;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public Date getDepartureTime() {
        return departureTime;
    }

    public void setDepartureTime(Date departureTime) {
        this.departureTime = departureTime;
    }

    public String getDepartureTimeStr() {
        if (departureTime != null) {
            departureTimeStr = DateUtils.dateToString(departureTime, "yyyy-MM-dd HH:mm:ss");
        }
        return departureTimeStr;
    }

    public void setDepartureTimeStr(String departureTimeStr) {
        this.departureTimeStr = departureTimeStr;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    public String getProductDesc() {
        return productDesc;
    }

    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }

    public Integer getProductStatus() {
        return productStatus;
    }

    public void setProductStatus(Integer productStatus) {
        this.productStatus = productStatus;
    }

    public String getProductStatusStr() {
        if(productStatus != null) {
           if (productStatus == 0 ) {
               productStatusStr = "关闭";
           } else if (productStatus == 1) {
               productStatusStr = "开启";
           }
        }
        return productStatusStr;
    }

    public void setProductStatusStr(String productStatusStr) {
        this.productStatusStr = productStatusStr;
    }
}

页面效果:
在这里插入图片描述

6.main.jsp页面制作

1. 首页导入:
在这里插入图片描述在这里插入图片描述

2. 修改首页跳转:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>

<html>
<body>
<%--<a href="${pageContext.request.contextPath}/product/findAll.do">查询所有的产品信息</a>--%>
<jsp:forward page="/pages/main.jsp"></jsp:forward>
</body>
</html>

页面展示:
在这里插入图片描述

7.产品操作-添加产品流程描述

在这里插入图片描述

8.产品操作-产品添加操作1

1. 新增product-add.jsp页面:
在这里插入图片描述

2. 修改product-list.jsp页面:新增新建跳转到product-add.jsp页面
在这里插入图片描述

3. 开始编写添加产品操作流程:

package cn.fsm.erm.controller;

import cn.fsm.erm.domain.Product;
import cn.fsm.erm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 查询所有产品
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Product> productList = productService.findAll();
        modelAndView.addObject("productList", productList);
        modelAndView.setViewName("product-list");
        return modelAndView;
    }

    /**
     * 添加产品
     * @param product 页面传递的产品信息
     * @return 查询所有产品
     * @throws Exception
     */
    @RequestMapping("/save.do")
    public String save(Product product) throws Exception {
        productService.save(product);
        return "redirect:findAll.do";
    }
}

package cn.fsm.erm.service;

import cn.fsm.erm.domain.Product;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface IProductService {

    public List<Product> findAll() throws Exception;

    void save(Product product);
}

package cn.fsm.erm.service.impl;

import cn.fsm.erm.dao.IProductDao;
import cn.fsm.erm.domain.Product;
import cn.fsm.erm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {

    @Autowired
    private IProductDao productDao;

    @Override
    public List<Product> findAll() throws Exception {
        return productDao.findAll();
    }

    @Override
    public void save(Product product) {
        productDao.save(product);
    }
}

package cn.fsm.erm.dao;

import cn.fsm.erm.domain.Product;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface IProductDao {

    @Select("select * from product")
    public List<Product> findAll() throws Exception;

    @Insert("insert into product values(null,#{productNum},#{productName},#{cityName}," +
            "#{departureTime},#{productPrice},#{productDesc},#{productStatus})")
    void save(Product product);
}

添加日志:
在这里插入图片描述

9.产品操作-产品添加操作2(类型转换)

package cn.fsm.erm.domain;

import cn.fsm.erm.utils.DateUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

public class Product {
    private int id;  //主键
    private String productNum;  //编号 唯一
    private String productName;  //名称
    private String cityName;  //出发城市
    @DateTimeFormat(pattern = "yyyy-HH-dd HH:mm")
    private Date departureTime;  //出发时间
    private String departureTimeStr;
    private double productPrice;  //产品价格
    private String productDesc;  //产品描述
    private Integer productStatus;  //状态  0关闭 1开启
    private String productStatusStr;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProductNum() {
        return productNum;
    }

    public void setProductNum(String productNum) {
        this.productNum = productNum;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public Date getDepartureTime() {
        return departureTime;
    }

    public void setDepartureTime(Date departureTime) {
        this.departureTime = departureTime;
    }

    public String getDepartureTimeStr() {
        if (departureTime != null) {
            departureTimeStr = DateUtils.dateToString(departureTime, "yyyy-MM-dd HH:mm:ss");
        }
        return departureTimeStr;
    }

    public void setDepartureTimeStr(String departureTimeStr) {
        this.departureTimeStr = departureTimeStr;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    public String getProductDesc() {
        return productDesc;
    }

    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }

    public Integer getProductStatus() {
        return productStatus;
    }

    public void setProductStatus(Integer productStatus) {
        this.productStatus = productStatus;
    }

    public String getProductStatusStr() {
        if(productStatus != null) {
           if (productStatus == 0 ) {
               productStatusStr = "关闭";
           } else if (productStatus == 1) {
               productStatusStr = "开启";
           }
        }
        return productStatusStr;
    }

    public void setProductStatusStr(String productStatusStr) {
        this.productStatusStr = productStatusStr;
    }
}

页面展示:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值