第一章:ssm框架整合
1、创建maven的工程
-
创建ssm_parent父工程
-
创建ssm_web子模块(打包方式是war包) 使用骨架
-
创建ssm_service子模块(打包方式是jar包)
-
创建ssm_dao子模块(打包方式是jar包)
-
创建ssm_domain子模块(打包方式是jar包)
-
创建ssm_utils子模块(打包方式是jar包)
-
web依赖于service,service依赖于dao,dao依赖于domain
-
在ssm_parent的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"> <modelVersion>4.0.0</modelVersion> <groupId>com.itheima</groupId> <artifactId>ssm_parent_331</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>ssm_dao_331</module> <module>ssm_service_331</module> <module>ssm_web_331</module> <module>ssm_domain_331</module> <module>ssm_utils_331</module> </modules>
<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>
<spring.version>5.0.2.RELEASE</spring.version>
<spring.security.version>5.0.2.RELEASE</spring.security.version>
</properties>
<dependencies>
<!-- spring相关的jar包 -->
<!-- 容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- mybatis与Spring整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- AOP切面 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- servletAPI -->
<!-- JSP应用 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- servlet应用 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- 日志记录工具 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.9.1</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--oracle的jar包-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.2.0</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</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>
</dependencies>
8. 在ssm_web项目中导入静态页面
9. 部署ssm_web的项目,只要把ssm_web项目加入到tomcat服务器中即可
#### 2、配置Dao层的配置文件
在ssm_dao项目中创建applicationContext-dao.xml的配置文件,编写具体的配置信息
<?xml version="1.0" encoding="UTF-8"?>
<!--引入jdbc属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--创建数据源对象-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--SqlSessionFactoryBean对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源对象-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--扫描dao包,创建dao、接口的动态代理对象:MapperScannerConfigurer-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定dao层的包路径-->
<property name="basePackage" value="com.itheima.dao"></property>
</bean>
#### 3、配置Service层的配置文件
在ssm_service项目中创建applicationContext-service.xml的配置文件,编写具体的配置信息
```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:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.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">
<!--引入持久层的配置文件-->
<!--<import resource="classpath*:spring/applicationContext-dao.xml"></import>-->
<!--扫描包,创建业务层对象-->
<context:component-scan base-package="com.itheima.service"></context:component-scan>
<!--事务管理器对象-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事务通知对象-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--aop切面配置-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:advisor>
</aop:config>
</beans>
4、配置数据库连接信息jdbc.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.174.129:1521:orcl
jdbc.user=itheima_331
jdbc.password=itheima
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/ssm_01
#jdbc.user=root
#jdbc.password=root
5、配置SpringMVC的配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描包:创建web层对象-->
<context:component-scan base-package="com.itheima.controller"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--静态资源放行-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--<mvc:resources mapping="/css/*" location="/css/"></mvc:resources>-->
<!--<mvc:resources mapping="/img/*" location="/img/"></mvc:resources>-->
<!--<mvc:resources mapping="/plugins/*" location="/plugins/"></mvc:resources>-->
</beans>
6、配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--全局参数:指定配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</context-param>
<!--编码过滤器-->
<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>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第二章:产品(旅游)模块功能实现
1、创建产品表和实体
1.1 创建数据库和表结构
create sequence product_seq;
CREATE TABLE product(
id number PRIMARY KEY ,
productNum VARCHAR2(50) NOT NULL UNIQUE,
productName VARCHAR2(50),
cityName VARCHAR2(50),
departureTime date,
productPrice number(8,2),
productDesc VARCHAR2(500),
productStatus INT
)
其中字段描述如下:
序号 | 字段名称 | 字段类型 | 字段描述 |
---|---|---|---|
1 | id | bigint | 无意义 |
2 | productNum | varchar(50) | 产品编号,唯一,不为空 |
3 | productName | varchar(50) | 产品名称(路线名称) |
4 | cityName | varchar(50) | 出发城市 |
5 | departureTime | date | 出发时间 |
6 | productPrice | number(8,2) | 产品价格 |
7 | productDesc | varchar(500) | 产品描述 |
8 | productStatus | int | 状态(0 关闭 1 开启) |
1.2 创建Product实体类
package com.itheima.domain;
public class Product {
private Integer id;
private String productNum;
private String productName;
private String cityName;
private Date departureTime;
private Double productPrice;
private String productDesc;
private Integer productStatus;
//省略getter和setter... ...
}
2、查询所有产品功能
2.1 页面入口地址
[外链图片转存失败(img-YCA1UvsG-1568821246154)(img\1.png)]
2.2 编写Controller
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
ProductService productService;
/**
* 查询全部
* @return
*/
@RequestMapping("/findAll")
public ModelAndView findAll(){
//准备数据
List<Product> productList = productService.findAll();
//创建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
//添加数据
modelAndView.addObject("productList",productList);
//指定页面
modelAndView.setViewName("product-list");
return modelAndView;
}
}
2.3 编写Service
ProductService接口
public interface ProductService {
/**
* 查询全部
* @return
*/
List<Product> findAll();
}
ProductServiceImpl接口实现
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
ProductDao productDao;
@Override
public List<Product> findAll() {
return productDao.findAll();
}
}
2.4 编写Dao
public interface ProductDao {
@Select("select * from product")
List<Product> findAll();
}
2.5 编写页面product-list.jsp
引入标签库:注意,如果jstl表达式没有效果,注意查看web.xml里面的头部信息
你的EL被禁用了 <%@ page isELIgnored="false" %>
然后去看你的web.xml的头
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
2.4版本默认启用el表达式,如果使用2.5版本,默认el表达式是关闭的
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
jstl的格式化标签库
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!--数据列表-->
<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_asc">ID</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>
<%--
foreache循环标签
items: 要循环的集合对象
var:循环中的每一个对象
--%>
<c:forEach items="${productList}" var="product">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${product.id}</td>
<td>${product.productNum}</td>
<td>${product.productName}</td>
<td>${product.cityName}</td>
<td>
<%--日期格式化库中的标签
value: 具体的日期格式
pattern: 转换要使用的格式
--%>
<fmt:formatDate value="${product.departureTime}" pattern="yyyy-MM-dd"></fmt:formatDate>
</td>
<td>${product.productPrice}</td>
<td>${product.productStatus == 1?"开启":"关闭"}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs"
οnclick='location.href="all-order-manage-edit.html"'>订单</button>
<button type="button" class="btn bg-olive btn-xs"
οnclick='location.href="all-order-manage-edit.html"'>查看</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
3、添加产品功能
3.1 页面入口
[外链图片转存失败(img-E5xP4w0w-1568821246155)(img\2.png)]
3.2 编写Controller
/**
* 保存产品
* @return
*/
@RequestMapping("/save")
public String save(Product product){
//保存操作
productService.save(product);
//请求查询全部
return "redirect:/product/findAll";
}
3.3 编写Service
ProductService接口
/**
* 保存产品
* @param product
*/
void save(Product product);
ProductServiceImpl实现
@Override
public void save(Product product) {
productDao.save(product);
}
3.4 编写Dao
// @Insert("insert into product values(product_seq.nextval , #{productNum},#{productName},#{cityName},#{departureTime}, #{productPrice},#{productDesc}, #{productStatus})")
// keyProperty:主键属性名
// keyColumn:主键列名
// before:在添加之前true/之后false获取主键
// resultType: 主键的类型
// statement: 获取主键的sql语句
//mysql:select last_insert_id();
// oracle: select product_seq.nextval from dual
@Insert("insert into product values(#{id} , #{productNum},#{productName},#{cityName},#{departureTime}, #{productPrice},#{productDesc}, #{productStatus})")
@SelectKey(keyProperty = "id",keyColumn = "id",before = true,resultType = Integer.class,
statement = "select product_seq.nextval from dual")
void save(Product product);
3.5 自定义类型转换器
package com.itheima.converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author 黑马程序员
* @Company http://www.ithiema.com
* @Version 1.0
*/
public class StringToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
System.out.println(source);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date date = sdf.parse(source);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
spring-mvc.xml
<!--注解驱动-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!--创建类型转换器工厂类对象-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.itheima.converter.StringToDateConverter"></bean>
</set>
</property>
</bean>
4、修改产品功能-数据回显
4.1 页面入口
[外链图片转存失败(img-02giKLxy-1568821246156)(img\3.png)]
4.2 编写Controller
@RequestMapping("/updateUI")
public ModelAndView updateUI(Integer id){
//根据id查询产品对象
Product product = productService.findById(id);
//创建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
//添加数据
modelAndView.addObject("product", product);
//指定页面
modelAndView.setViewName("product-update");
return modelAndView;
}
4.3 编写Service
ProductService接口
/**
* 根据id查询
* @param id
* @return
*/
Product findById(Integer id);
ProductServiceImpl实现
@Override
public Product findById(Integer id) {
return productDao.findById(id);
}
4.4 编写Dao
/**
* 根据id查询
* @param id
* @return
*/
@Select("select p.*,to_char(departureTime,'yyyy-mm-dd hh24:mi') departureTimeStr from product p where id = #{abc}")
Product findById(Integer id);
3.5 编写页面
页面:
<form action="${pageContext.request.contextPath}/product/update"
method="post">
<%--
隐藏域:隐藏主键id编号 , 最后更新操作时需要
--%>
<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 id="productStatus" class="form-control select2" style="width: 100%"
name="productStatus">
<option value="0" >关闭</option>
<option value="1" selected="selected">开启</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"
οnclick="history.back(-1);">返回</button>
</div>
<!--工具栏/--> </section>
<!-- 正文区域 /-->
</form>
js 代码
<script type="text/javascript">
//选择产品的状态
//#productStatus :获取select标签
//#productStatus option :select标签中的所有的选项
//#productStatus option[value=1]: select标签中的选项中属性value=1的option
//prop: 设置某些标签中的(选择性)属性
//attr: 设置某些标签中的(普通)属性
$("#productStatus option[value=${product.productStatus}]").prop("selected","selected");
</script>
5、修改产品功能-数据修改
5.1 页面入口
[外链图片转存失败(img-Nax9vDz9-1568821246156)(img\4.png)]
5.2 编写Controller
@RequestMapping("/update")
public String update(Product product){
//更新操作
productService.update(product);
//请求查询全部
return "redirect:/product/findAll";
}
5.3 编写Service
ProductService接口
/**
* 更新
* @param product
*/
void update(Product product);
ProductServiceImpl实现
@Override
public void update(Product product) {
productDao.update(product);
}
5.4 编写Dao
/**
* 更新产品
* @param product
*/
@Update("update product set productNum=#{productNum},productName=#{productName},cityName=#{cityName},departureTime=#{departureTime}, productPrice=#{productPrice},productDesc=#{productDesc}, productStatus=#{productStatus} where id = #{id}")
void update(Product product);
6、删除单个产品
6.1 页面代码实现
[外链图片转存失败(img-jUqdUWxG-1568821246157)(assets/1540875176499.png)]
<button type="button" class="btn bg-olive btn-xs"
onclick='delOne(${product.id})'>删除</button>
function delOne(id){
if(confirm("您确定要删除吗?")){
// 删除操作
location.href = "${pageContext.request.contextPath}/product/delOne?id="+id;
}
}
6.2 编写Controller
/*
删除单个产品
*/
@RequestMapping("/delOne")
public String delOne(Integer id){
//执行删除操作
productService.delOne(id);
// 请求查询全部
return "redirect:/product/findAll";
}
6.3 编写Service
/**
* 删除单个产品
* @param id
*/
void delOne(Integer id);
@Override
public void delOne(Integer id) {
productDao.delById(id);
}
6.4 编写Dao
/**
* 根据id删除产品
* @param id
*/
@Delete("delete from product where id = #{abc}")
void delById(Integer id);
7、删除多个产品功能
7.1 页面代码实现
[外链图片转存失败(img-a7trnTEx-1568821246157)(img\5.png)]
<%--如果发现表单提交的数据为null,则把表单移动到表格之外--%>
<form id="delForm" action="${pageContext.request.contextPath}/product/delMany">
<!--数据列表-->
<input name="ids" type="checkbox" value="${product.id}">
<!--数据列表/-->
</form>
//删除多个产品
function delMany(){
if(confirm("您确定要删除选择的产品吗?")) {
//获取表单
// document.forms的到所有的表单 document.forms[0]
var delForm = $("#delForm");
//表单提交
//submit: 表单对象执行提交操作
delForm.submit();
}
}
7.2 编写Controller
/**
* 删除多个产品
* @param ids
* @return
*/
@RequestMapping("/delMany")
public String delMany(Integer[] ids){
//执行删除操作
productService.delMany(ids);
//请求查询全部
return "redirect:/product/findAll";
}
7.3 编写Service
ProductService接口
/**
* 删除多个产品
* @param ids
*/
void delMany(Integer[] ids);
ProductServiceImpl实现
@Override
public void delMany(Integer[] ids) {
//判断ids是否为null
if(ids != null){
//遍历ids,删除多个产品
for (Integer id : ids) {
productDao.delById(id);
}
}
}
第三章:订单模块实现
1、创建订单表和实体
1.1 创建数据库和表结构
create sequence order_seq;
CREATE TABLE orders(
id number PRIMARY KEY,
orderNum VARCHAR2(20) NOT NULL UNIQUE,
orderTime DATE,
peopleCount number,
orderDesc VARCHAR2(500),
payType number,
orderStatus number,
productId int,
FOREIGN KEY (productId) REFERENCES product(id)
)
其中字段描述如下:
序号 | 字段名称 | 字段类型 | 字段描述 |
---|---|---|---|
1 | id | bigint | 无意义、主键自动增长 |
2 | orderNum | varchar(50) | 订单编号 不为空 唯一 |
3 | orderTime | DATE | 下单时间 |
4 | peopleCount | int | 出行人数 |
5 | orderDesc | varchar(500) | 订单描述(其它信息) |
6 | payType | int | 支付方式(0 支付宝 1 微信 2其它) |
7 | orderStatus | int | 订单状态(0 未支付 1 已支付) |
8 | productId | int | 产品id 外键 |
1.2 创建Product实体类
package com.itheima.domain;
public class Order {
private Long id;
private String orderNum;
private Date orderTime;
private Integer peopleCount;
private String orderDesc;
private Integer payType;
private Integer orderStatus;
private Product product;//该订单属于哪一个产品
//省略getter和setter... ...
}
2、查询所有订单功能
2.1 页面入口地址
[外链图片转存失败(img-3Q70E5Fi-1568821246158)(img/6.png)]
2.2 编写Controller
@Controller
@RequestMapping("/order")
public class OrderController {
@Autowired
OrderService orderService;
/**
* 查询所有订单
* @return
*/
@RequestMapping("/findAll")
public ModelAndView findAll(){
//查询所有订单数据
List<Order> orderList = orderService.findAll();
//创建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
//添加数据
modelAndView.addObject("orderList",orderList);
//指定页面
modelAndView.setViewName("order-list");
return modelAndView;
}
}
2.3 编写Service
OrderService接口
public interface OrderService {
/**
* 查询全部订单
* @return
*/
List<Order> findAll();
}
OrderServiceImpl接口实现
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
OrderDao orderDao;
@Override
public List<Order> findAll() {
return orderDao.findAll();
}
}
2.4 编写Dao
/**
* 查询全部
* @return
*/
@Select("select * from orders")
// @Results 映射多列数据
// @Result:映射单列数据
// select :全限类名+ 方法名 == mapperId
@Results({
@Result(property = "product",column = "productId",javaType = Product.class,
one = @One(select = "com.itheima.dao.ProductDao.findById"))
})
List<Order> findAll();
2.5 编写页面order-list.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<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_asc">ID</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="${orderList}" var="order">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${order.id}</td>
<td>${order.orderNum}</td>
<td>
<fmt:formatDate value="${order.orderTime}" pattern="yyyy-MM-dd HH:mm"></fmt:formatDate>
</td>
<td>${order.peopleCount}</td>
<%--
支付宝-- 0
微信 -- 1
其他 -- 2
--%>
<td>${order.payType==0?"支付宝":order.payType==1?"微信":"其他"}</td>
<td>${order.orderStatus == 0 ?"未支付":"已支付"}</td>
<td>
${order.product.productName}
</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs"
οnclick='location.href="all-order-manage-edit.html"'>订单</button>
<button type="button" class="btn bg-olive btn-xs"
οnclick='location.href="all-order-manage-edit.html"'>查看</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
3、添加订单功能-添加订单页面数据准备
3.1 页面入口
新增订单页面需要准备产品的数据列表
[外链图片转存失败(img-R77Ft1JL-1568821246158)(img\7.png)]
[外链图片转存失败(img-yjkgOBzk-1568821246159)(img\8.png)]
[外链图片转存失败(img-ea3bpULh-1568821246159)(img\9.png)]
3.2 编写Controller
@Autowired
ProductService productService;
/**
* 添加订单数据回显
* @return
*/
@RequestMapping("/addUI")
public ModelAndView addUI(){
//查询所有的产品数据
List<Product> productList = productService.findAll();
//创建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
//添加数据
modelAndView.addObject("productList", productList);
//指定页面
modelAndView.setViewName("order-add");
return modelAndView;
}
3.3 编写页面order-add.jsp
<div class="col-md-2 title">选择产品</div>
<div class="col-md-4 data">
<select class="form-control select2" style="width: 100%"
name="product.id">
<c:forEach items="${ productList }" var="p">
<%--
value:给程序员使用
文本:给客户看的
--%>
<option value="${ p.id }" >${ p.productName }</option>
</c:forEach>
</select>
</div>
3、添加订单功能-保存订单数据
3.1 页面入口地址
[外链图片转存失败(img-fMv0H0Ju-1568821246159)(img\10.png)]
2.2 编写Controller
/**
* 保存订单
* @param order
* @return
*/
@RequestMapping("/save")
public String add(Order order){
//保存操作
orderService.save(order);
//请求查询全部
return "redirect:/order/findAll";
}
2.3 编写Service
OrderService接口
/**
* 保存订单
* @param order
*/
void save(Order order);
OrderServiceImpl接口实现
@Override
public void save(Order order) {
orderDao.save(order);
}
2.4 编写Dao
/**
* 保存订单
* @param order
*/
@Insert("insert into orders values(order_seq.nextval ,#{orderNum}, #{orderTime} ,#{peopleCount},#{orderDesc}, #{payType} , #{orderStatus} , #{product.id})")
void save(Order order);
注意事项:
1.orcal 数据库执行事务操作后,要点提交或者回滚,不然后面的指令会一直等待。