产品表信息描述
序号 | 字段名称 | 字段类型 | 字段描述 |
---|---|---|---|
1 | id | number | 主键,自动增长 |
2 | productNum | varchar2(50) | 产品编号,唯一,不为空 |
3 | productName | varchar2(50) | 产品名称(路线名称) |
4 | cityName | varchar2(50) | 出发城市 |
5 | departureTime | timestamp | 出发时间 |
6 | productPrice | number(8,2) | 产品价格 |
7 | productDesc | varchar2(500) | 产品描述 |
8 | productStatus | number(1) | 状态(0:关闭,1:开启) |
--创建表空间
create tablespace ssmtest
datafile 'c:\ssmtest.dbf'
size 10m
autoextend on
next 10m;
--创建用户
create user ssm49 identified by ssmtest default tablespace ssmtest ;
--授权
grant dba to ssmtest ;
创建表sql,为了方便实现主键自增,我们这里创建序列。
--建表
create table product(
id number(20) primary key,
productNum varchar2(50),
productName varchar2(50),
cityName varchar2(50),
departureTime date,
productPrice number(8,2),
productDesc varchar2(500),
productStatus number(1)
);
--创建序列
create sequence product_seq;
--插入测试数据
insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
values (product_seq.NEXTVAL, '001', '西冲1日游', '深圳', to_date('22-08-2018 08:00:00', 'dd-mm-yyyy hh24:mi:ss'), 294, '畅游西冲,体验潜水,快艇冲浪,烧烤BBQ,海边戏水游玩', 1);
insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
values (product_seq.NEXTVAL, '002', '西冲1日游', '深圳', to_date('07-08-2018 08:30:00', 'dd-mm-yyyy hh24:mi:ss'), 88, '专车专导,畅游西冲,烧烤BBQ,快艇冲浪', 1);
insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
values (product_seq.NEXTVAL, '003', '南澳西冲1日游', '深圳', to_date('22-08-2018 07:50:00', 'dd-mm-yyyy hh24:mi:ss'), 98, '杨梅坑美人鱼拍摄基地 BBQ海边烧烤 、快艇冲浪与CS野战', 1);
insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
values (product_seq.NEXTVAL, '004', '西冲沙滩1日游', '深圳', to_date('22-08-2018 08:00:00', 'dd-mm-yyyy hh24:mi:ss'), 86, '杨梅坑单车 BBQ海边烧烤 CS野战', 1);
insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
values (product_seq.NEXTVAL, '005', '南澳西冲1日游', '深圳', to_date('22-08-2018 09:00:00', 'dd-mm-yyyy hh24:mi:ss'), 78, '八千人出游、好评达9成、快艇上情人岛、杨梅坑环海骑行、观美人鱼拍摄基地、海边BBQ、海边戏水游玩', 1);
insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
values (product_seq.NEXTVAL, 'No.001', '深圳一日', '深圳', to_date('04-09-2018 09:45:00', 'dd-mm-yyyy hh24:mi:ss'), 99, 'asdfasdf', 1);
commit;
<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.0modelVersion>
<groupId>com.vikinggroupId>
<artifactId>ssm-parentartifactId>
<version>1.0-SNAPSHOTversion>
<modules>
<module>ssm-modelmodule>
<module>ssm-daomodule>
<module>ssm-servicemodule>
<module>ssm-webmodule>
<module>ssm-utilmodule>
modules>
<packaging>pompackaging>
<properties>
<spring.version>5.0.2.RELEASEspring.version>
<slf4j.version>1.6.6slf4j.version>
<oracle.version>10.2.0.1.0oracle.version>
<mybatis.version>3.4.5mybatis.version>
<aspectjweaver.version>1.6.8aspectjweaver.version>
<junit.version>4.12junit.version>
<jsp-api.version>2.0jsp-api.version>
<servlet-api.version>2.5servlet-api.version>
<jstl.version>1.2jstl.version>
<mybatis-spring.version>1.3.0mybatis-spring.version>
<druid.version>1.0.9druid.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>${aspectjweaver.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>${servlet-api.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>${jsp-api.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>${jstl.version}version>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc14artifactId>
<version>${oracle.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>${mybatis-spring.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>18081port>
<path>/path>
<uriEncoding>UTF-8uriEncoding>
configuration>
plugin>
plugins>
build>
project>
该工程用于提供工具类的。
在其中创建一个类,提供时间转字符串类型。
package com.viking.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 日期处理工具
*/
public class DateUtil {
public static SimpleDateFormat ymdHms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static SimpleDateFormat ymdHm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public static SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
/**
* 格式化日期
* @param date
* @param format
* @return
*/
public static String date2Str(Date date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
public static String date2Str(Date date, DateFormat format){
return format.format(date);
}
}
创建Product即可
package com.viking.domain;
public class Product {
private Integer id;
private String productNum;
private String productName;
private String cityName;
private Date departureTime;
private Float productPrice;
private String productDesc;
private int productStatus;
//get..set..
}
在ssm-domain的pom.xml中引入spring以及工具包的依赖
<dependencies>
<dependency>
<groupId>com.vikinggroupId>
<artifactId>ssm-utilartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<scope>providedscope>
dependency>
dependencies>
<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>ssm-parentartifactId>
<groupId>com.vikinggroupId>
<version>1.0-SNAPSHOTversion>
<relativePath>../ssm-parent/pom.xmlrelativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>ssm-daoartifactId>
<dependencies>
<dependency>
<groupId>com.vikinggroupId>
<artifactId>ssm-domainartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc14artifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
dependency>
dependencies>
project>
在ssm-dao的resources目录下,创建spring-mybatis.xml文件,内容如下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="jdbc:oracle:thin:@192.168.66.166:1521:orcl" />
<property name="username" value="ssmtest" />
<property name="password" value="ssmtest" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.viking.domain"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.viking.dao" />
bean>
beans>
package com.viking.dao;
public interface ProductDao {}
<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>ssm-parentartifactId>
<groupId>com.vikinggroupId>
<version>1.0-SNAPSHOTversion>
<relativePath>../ssm-parent/pom.xmlrelativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>ssm-serviceartifactId>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>com.vikinggroupId>
<artifactId>ssm-daoartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
dependency>
dependencies>
project>
在ssm-service的resources目录下,创建spring.xml文件,内容如下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.Exception" />
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.Exception" />
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.Exception" />
<tx:method name="modify*" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.Exception" />
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.Exception" />
<tx:method name="*" read-only="true" />
tx:attributes>
tx:advice>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
<aop:config>
<aop:pointcut expression="execution(* com.viking.service.impl.*.*(..))"
id="tranpointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="tranpointcut" />
aop:config>
<import resource="spring-mybatis.xml" />
beans>
package com.viking.service;
public interface ProductService {}
package com.viking.service.impl;
import com.viking.service.ProductService;
import org.springframework.stereotype.Service;
@Service
public class ProductServiceImpl implements ProductService {}
<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>ssm-parentartifactId>
<groupId>com.vikinggroupId>
<version>1.0-SNAPSHOTversion>
<relativePath>../ssm-parent/pom.xmlrelativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>ssm-webartifactId>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>com.vikinggroupId>
<artifactId>ssm-serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
dependency>
dependencies>
project>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.shtmlwelcome-file>
welcome-file-list>
web-app>
在ssm-web的resources下创建springmvc.xml,内容如下
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="com.viking" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
bean>
<mvc:default-servlet-handler />
<import resource="spring.xml" />
beans>
package com.viking.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/product")
public class ProductController {}
复制 页面\ssm_pages\src\main\webapp目录下的目录和文件,除了WEB-INF外,其它所有文件都复制到ssm-web\src\main\webapp目录下
@Controller
@RequestMapping(value = "/product")
public class ProductController {
@Autowired
private ProductService productService;
/***
* 商品列表查询
* @param model
* @return
*/
@RequestMapping(value = "/list")
public String list(Model model){
//集合查询
List<Product> products = productService.list();
model.addAttribute("products",products);
return "product-list";
}
}
//接口
public interface ProductService {
List<Product> list();
}
//接口实现
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public List<Product> list() {
return productDao.list();
}
}
public interface ProductDao {
/**
* 查询所有
* @return
*/
@Select("select * from product")
List<Product> list();
}
<!--数据列表-->
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right: 0px;"><input
id="selall" type="checkbox" class="icheckbox_square-blue">
</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="${products}" var="product">
<tr>
<td><input name="ids" value="${product.id}" type="checkbox"></td>
<td>${product.productNum}</td>
<td>${product.productName}</td>
<td>${product.cityName}</td>
<td>
${product.departureTime}
</td>
<td>${product.productPrice}</td>
<td>${product.productDesc}</td>
<td>
${product.productStatus}
</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs"
onclick='del(${product.id})'>删除</button>
<button type="button" class="btn bg-olive btn-xs"
onclick='location.href="/product/one?id=${product.id}"'>查看</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!--数据列表/-->
JSP页面显示的日期为默认的英文日期,转换成字符串格式有2种方式。
使用fmt标签进行转换
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatDate value="${product.departureTime }" pattern="yyyy-MM-dd hh:mm:ss"/>
在JavaBean中添加属性,重写该属性的get方法进行转换
/**
* 重写get方法,返回字符串类型的时间
* @return
*/
public String getDepartureTimeStr() {
if(departureTime == null) {
return "";
}else {
return DateUtil.dateToStr(departureTime, DateUtil.ymdHm);
}
}
jsp中用这个
${product.departureTimeStr}
此时我们会发现,出来的时间里没有了小时与分钟,但查询数据库里的数据时,确实有小时与分钟的显示。是什么原因导致的呢?
这是mybatis与oracle整合时出现的日期时间处理问题,mybatis默认对date类型的数据只显示ymd,而oracle中的date类型是包含hms的,我们可以通过以下方法实现
@Results(@Result(column = "departureTime",property = "departureTime",jdbcType = JdbcType.TIMESTAMP))
有2种方法实现
<!--1 在JSP页面进行判断-->
<c:if test="${product.productStatus == 0 }">关闭</c:if>
<c:if test="${product.productStatus == 1 }">开启</c:if>
// 2 在JavaBean中添加属性,重写get方法
private String productStatusStr;
public String getProductStatusStr() {
return productStatus == 0?"关闭":"开启";
}
点击产品列表中的【新建】按钮,跳转到添加产品页面(如下图所示),当点击【保存】成功后,跳转到产品列表页面
在ProductController中增加2个方法,一个是跳转到增加页面,一个是实现增加,他们的访问路径一致,提交方式不一样。
/***
* 新增页面跳转
*/
@RequestMapping(value = "/initAdd")
public String initAdd(){
// 为什么独立出一个请求的方法:
//方便以后的权限控制,如果登陆的用户没有 添加 的权限,就会进入拦截里,如果只是在页面做页面之间的跳转则不利于后期的权限控制
return "product-add";
}
/***
* 增加操作
* @return
*/
@RequestMapping(value = "/add")
public String add(Product product){
//增加数据
int count = productService.add(product);
return "redirect:/product/list";
}
新增一个add方法
//接口
int add(Product product);
//实现类
@Override
public int add(Product product) {
return productDao.add(product);
}
新增一个add方法,这里我们用到了序列,使用的是@SelectKey注解实现序列调用
/***
* 增加操作
* @param product
* @return
*/
@SelectKey(statement = "SELECT PRODUCT_SEQ.NEXTVAL FROM dual", keyProperty = "id", before = true, resultType =Integer.class)
@Insert("insert into product(id,productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus)values(#{id},#{productNum},#{productName},#{cityName},#{departureTime},#{productPrice},#{productDesc},#{productStatus})")
int add(Product product);
除了以上使用selectKey的方式外,我们还可以直接调用序列的nextval方式,做法如下,把#{id}改成PRODUCT_SEQ.NEXTVAL即可
@Insert("insert into product(id,productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus)values(PRODUCT_SEQ.NEXTVAL,#{productNum},#{productName},#{cityName},#{departureTime},#{productPrice},#{productDesc},#{productStatus})")
int add(Product product);
<form action="${pageContext.request.contextPath}/product/add" method="post">
<!-- 正文区域 -->
<section class="content"> <!--产品信息-->
<div class="panel panel-default">
<div class="panel-heading">产品信息</div>
<div class="row data-type">
<div class="col-md-2 title">产品编号</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="productNum"
placeholder="产品编号" value="">
</div>
<div class="col-md-2 title">产品名称</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="productName"
placeholder="产品名称" value="">
</div>
<div class="col-md-2 title">出发时间</div>
<div class="col-md-4 data">
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="text" class="form-control pull-right"
id="datepicker-a3" name="departureTime">
</div>
</div>
<div class="col-md-2 title">出发城市</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="cityName"
placeholder="出发城市" value="">
</div>
<div class="col-md-2 title">产品价格</div>
<div class="col-md-4 data">
<input type="text" class="form-control" placeholder="产品价格"
name="productPrice" value="">
</div>
<div class="col-md-2 title">产品状态</div>
<div class="col-md-4 data">
<select class="form-control select2" style="width: 100%"
name="productStatus">
<option value="0" selected="selected">关闭</option>
<option value="1">开启</option>
</select>
</div>
<div class="col-md-2 title rowHeight2x">其他信息</div>
<div class="col-md-10 data rowHeight2x">
<textarea class="form-control" rows="3" placeholder="其他信息"
name="productDesc"></textarea>
</div>
</div>
</div>
<!--订单信息/--> <!--工具栏-->
<div class="box-tools text-center">
<button type="submit" class="btn bg-maroon">保存</button>
<button type="button" class="btn bg-default"
onclick="history.back(-1);">返回</button>
</div>
<!--工具栏/--> </section>
<!-- 正文区域 /-->
</form>
在进行数据绑定的时候,日期出现了异常,该格式的日期不支持默认数据类型转换
自定义类型转换器(比较麻烦,可查阅springMVC的资料)
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
private Date departureTime;
在Controller类中添加方法,进行类型转换
/**
* 类型转换
* @param dataBinder
*/
@InitBinder
public void initBinderDate(WebDataBinder dataBinder) {
dataBinder.registerCustomEditor(Date.class, new PropertiesEditor() {
// JSP页面传过来的数据
public void setAsText(String text) throws IllegalArgumentException {
// 把字符串转换成日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date date = sdf.parse(text);
// 设置值
super.setValue(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
}
点击产品列表中的【修改】按钮,跳转到【产品修改页面】,在【产品修改页面】中修改后点击【修改】按钮,提交数据到后台,成功后跳转到【产品列表】页面
增加2个方法,一个是根据id查询产品,一个是保存方法
/***
* 修改产品
* @return
*/
@RequestMapping(value = "/update")
public String update(Product product){
int mcount = productService.update(product);
return "redirect:/product/list";
}
/**
* 修改产品 回显
* 通过编号查询产品信息
* @param id
* @param model
* @return
*/
@RequestMapping("/initUpdate")
public String initUpdate(Long id,Model model){
Product product = productService.getById(id);
model.addAttribute("product",product);
return "product-update";
}
分别在接口和实现类中新增2个方法
//接口 ProductService
Product getById(Long id);
int update(Product product);
//实现类 ProductServiceImpl
@Override
public Product getById(Long id) {
return productDao.getById(id);
}
@Override
public int update(Product product) {
return productDao.update(product);
}
新增2个方法,一个根据ID查询,一个修改方法。
/**
* 根据ID查询
* @param id
* @return
*/
@Select("select * from product where id=#{id}")
Product getById(Long id);
/***
* 修改操作
* @param product
* @return
*/
@Update("update product set productNum = #{productNum},productName=#{productName},cityName=#{cityName},departureTime=#{departureTime},productPrice=#{productPrice},productDesc=#{productDesc},productStatus=#{productStatus} where id = #{id}")
int update(Product product);
<form action="${pageContext.request.contextPath}/product/update" method="post">
<input type="hidden" name="id" value="${product.id}">
<!-- 正文区域 -->
<section class="content"> <!--产品信息-->
<div class="panel panel-default">
<div class="panel-heading">产品信息</div>
<div class="row data-type">
<div class="col-md-2 title">产品编号</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="productNum"
placeholder="产品编号" value="${product.productNum}"
readonly="readonly">
</div>
<div class="col-md-2 title">产品名称</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="productName"
placeholder="产品名称" value="${product.productName}">
</div>
<div class="col-md-2 title">出发时间</div>
<div class="col-md-4 data">
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="text" class="form-control pull-right"
id="datepicker-a3" name="departureTime"
value="${product.departureTimeStr}">
</div>
</div>
<div class="col-md-2 title">出发城市</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="cityName"
placeholder="出发城市" value="${product.cityName}">
</div>
<div class="col-md-2 title">产品价格</div>
<div class="col-md-4 data">
<input type="text" class="form-control" placeholder="产品价格"
name="productPrice" value="${product.productPrice}">
</div>
<div class="col-md-2 title">产品状态</div>
<div class="col-md-4 data">
<select class="form-control select2" style="width: 100%"
name="productStatus">
<option value="0" selected="selected">关闭</option>
<option value="1">开启</option>
</select>
</div>
<div class="col-md-2 title rowHeight2x">其他信息</div>
<div class="col-md-10 data rowHeight2x">
<textarea class="form-control" rows="3" placeholder="其他信息"
name="productDesc">${product.productDesc}</textarea>
</div>
</div>
</div>
<!--订单信息/--> <!--工具栏-->
<div class="box-tools text-center">
<button type="submit" class="btn bg-maroon">修改</button>
<button type="button" class="btn bg-default"
onclick="history.back(-1);">返回</button>
</div>
<!--工具栏/--> </section>
<!-- 正文区域 /-->
</form>
在【产品列表】页面中,点击【删除】按钮,弹出询问框,确认后提交到后台进行删除操作,成功后跳转到【产品列表】页面
/***
* 根据ID删除
* @param id
* @return
*/
@RequestMapping(value = "/delete")
public String delete(Integer id){
int dcount = productService.deleteById(id);
return "redirect:/product/list";
}
//接口
int deleteById(Integer id);
//实现类
@Override
public int deleteById(Integer id) {
return productDao.deleteById(id);
}
/***
* 删除操作
* @param id
* @return
*/
@Delete("delete from product where id=#{id}")
int deleteById(Integer id);
修改product-list.jsp,操作的列中修改按钮后添加删除按钮的调用方法
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs" onclick='location.href="/product/initUpdate?id=${product.id}"'>修改</button>
<button type="button" class="btn bg-olive btn-xs" onclick='del(${product.id})'>删除</button>
</td>
且在页面最后的js中添加del方法,用于删除时的方法调用
function del(id){
// 防呆判断,避免误删
if(confirm("确认要删除吗")){
location.href = "/product/delete?id=" + id;
}
}
序号 | 字段名称 | 字段类型 | 字段描述 |
---|---|---|---|
1 | id | number | 主键,自动增长 |
2 | orderNum | varchar2(50) | 订单编号,不为空且唯一 |
3 | orderTime | timestamp | 下单时间 |
4 | peopleCount | number | 出行人数 |
5 | orderDesc | varchar2(500) | 订单描述(其它信息) |
6 | payType | number(1) | 支付类型(0:支付宝,1:微信,2:其它) |
7 | orderStatus | number(1) | 订单状态(0:未支付,1:已支付) |
8 | productId | number | 对应产品编号 |
productId描述了订单与产品之间的关系。
--创建订单表
create table orders(
id number(20) primary key,
orderNum varchar2(20) NOT NULL UNIQUE,
orderTime timestamp,
peopleCount number(5),
orderDesc varchar2(500),
payType number(1),
orderStatus number(1),
productId number(20)
);
--创建序列
create sequence orders_seq;
public class Orders {
private Long id;
private String orderNum;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private Date orderTime;
private Integer peopleCount;
private String orderDesc;
private Integer payType;
private Integer orderStatus;
//private Long productId;
//一对一映射关系
private Product product;
//get..set..toString
}
@Controller
@RequestMapping(value = "/orders")
public class OrdersController {
@Autowired
private OrdersService ordersService;
/***
* 订单列表
* @param model
* @return
*/
@RequestMapping(value = "/list")
public String list(Model model){
List<Orders> orders = ordersService.list();
model.addAttribute("orders",orders);
return "order-list";
}
}
//接口
public interface OrdersService {
List<Orders> list();
}
//实现类
@Service
public class OrdersServiceImpl implements OrdersService {
@Autowired
private OrdersDao ordersDao;
@Override
public List<Orders> list() {
return ordersDao.list();
}
}
public interface OrdersDao {
/**
* 查询所有
*
* @return
*/
@Select("select o.id as oid,o.orderNum,o.orderTime,o.peopleCount,o.orderDesc,o.payType,o.orderStatus,p.* from orders o,product p where o.productId = p.id")
@Results(value = {
@Result(id = true, property = "id", column = "oid"),
@Result(property = "orderNum", column = "orderNum"),
@Result(property = "orderTime", column = "orderTime"),
@Result(property = "peopleCount", column = "peopleCount"),
@Result(property = "orderDesc", column = "orderDesc"),
@Result(property = "payType", column = "payType"),
@Result(property = "orderStatus", column = "orderStatus"),
@Result(property = "product.id", column = "id"),
@Result(property = "product.productNum", column = "productNum"),
@Result(property = "product.productName", column = "productName"),
@Result(property = "product.cityName", column = "cityName"),
@Result(property = "product.departureTime", column = "departureTime"),
@Result(property = "product.productPrice", column = "productPrice"),
@Result(property = "product.productDesc", column = "productDesc"),
@Result(property = "product.productStatus", column = "productStatus")
})
List<Orders> list();
}
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right: 0px;"><input
id="selall" type="checkbox" class="icheckbox_square-blue">
</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="${orders}" var="orders">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${orders.orderNum}</td>
<td>${orders.product.productName}</td>
<td>${orders.product.cityName}</td>
<td>${orders.product.departureTimeStr}</td>
<td>${orders.product.productPrice}</td>
<td>${orders.peopleCount}</td>
<td><c:if test="${orders.orderStatus==0}">未支付</c:if><c:if test="${orders.orderStatus==1}">已支付</c:if></td>
<td>
<c:choose>
<c:when test="${orders.payType == 0}">支付宝</c:when>
<c:when test="${orders.payType == 1}">微信</c:when>
<c:otherwise>其它</c:otherwise>
</c:choose>
</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs"
onclick='location.href="${pageContext.request.contextPath}/pages/order-show.jsp"'>订单</button>
<button type="button" class="btn bg-olive btn-xs"
onclick='location.href="${pageContext.request.contextPath}/pages/order-show.jsp"'>查看</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
点击【订单列表】页面中的【新建】按钮,进入【新增】订单页面,提交【保存】成功后跳转到【订单列表】页面
新增2个方法,分别实现页面跳转和保存订单操作。在这里,页面需要加载产品信息供用户选择,所以需要注入ProductService来实现查询.
@Autowired
private ProductService productService;
/**
* 保存操作
* @param orders
* @return
*/
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String add(Orders orders){
int acount = ordersService.add(orders);
return "redirect:/orders/list";
}
/***
* 增加订单页面跳转
* @return
*/
@RequestMapping(value = "/initAdd",method = RequestMethod.GET)
public String initAdd(Model model){
//查询所有产品信息,页面需要提供产品下拉列表,所以需要注入ProductService来实现查询所有的产品
List<Product> products = productService.list();
model.addAttribute("products",products);
return "order-add";
}
//接口
int add(Orders orders);
//实现类
@Override
public int add(Orders orders) {
return ordersDao.add(orders);
}
/**
* 插入数据操作
* @param orders
* @return
*/
@Insert("insert into orders (id,orderNum,orderTime,peopleCount,orderDesc,payType,orderStatus,productId) values (ORDERS_SEQ.NEXTVAL,#{orderNum},#{orderTime},#{peopleCount},#{orderDesc},#{payType},#{orderStatus},#{product.id})")
int add(Orders orders);
重点:
增加订单
订单查询
时间格式处理
使用序列自动生成主键
熟练@Insert、@Select、@Update、@Delete注解的使用