一、 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
);
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
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>
<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>
<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>
<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>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<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>
<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>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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</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;
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">
<context:component-scan base-package="cn.fsm.myprj.service">
</context:component-scan>
<context:component-scan base-package="cn.fsm.myprj.dao">
</context:component-scan>
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.fsm.myprj.dao"/>
</bean>
<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
">
<context:component-scan base-package="com.itheima.ssm.controller">
</context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<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/**" />
<mvc:annotation-driven />
<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>
<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-mvc.xml</param-value>
</init-param>
<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>
<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>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<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 {
public static String dateToString(Date targetDate, String formatString) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
return simpleDateFormat.format(targetDate);
}
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;
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;
@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;
}
@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;
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;
}
}
页面展示:
|