一、SSM框架 maven模块+oracle+注解开发
1.初始化数据库
--创建表空间语句 create tablespace ssm_69 datafile 'c:\ssm_69.dbf' size 100m autoextend on next 10m; --创建用户授予权限 create user ssm_69 identified by ssm_69 default tablespace ssm_69; grant dba to ssm_69; --创建序列语句 create sequence common_sequence; --创建表的sql语句 CREATE TABLE product( id number(9) PRIMARY KEY , productNum VARCHAR2(50) , productName VARCHAR2(50), cityName VARCHAR2(50), --更精确日期默认秒后6位 DepartureTime TIMESTAMP(0), productPrice NUMBER(8,2), productDesc VARCHAR2(500), productStatus number(2) ) ----插入测试的数据 insert into PRODUCT values (common_sequence.nextval, 'itcast-001', '魔都五日游', '上海', sysdate, 1800, '魔都我来了', 0); insert into PRODUCT values (common_sequence.nextval, 'itcast-002', '雾都三日游', '北京', sysdate, 1200, '不错的旅行', 1); insert into PRODUCT values (common_sequence.nextval, 'itcast-003', '杭州三日游', '北京',sysdate, 1200, '不错的旅行', 1); insert into PRODUCT values (common_sequence.nextval, 'itcast-004', '魔都五日游', '上海', sysdate, 1800, '魔都我来了', 0); insert into PRODUCT values (common_sequence.nextval, 'itcast-005', '雾都三日游', '北京', sysdate, 1200, '不错的旅行', 1); insert into PRODUCT values (common_sequence.nextval, 'itcast-006', '雾都三日游', '北京', sysdate, 1200, '不错的旅行', 1); commit;
2.创建模块工程
maven_parent ssm_domain_69 ssm_dao_69 ssm_util_69 ssm_service_69
3.引入资源文件
adminLte模板 css js pl ugins
4.引入依赖
拷贝pom文件的坐标
5.代码开发
1.domain层
private Long 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;
2.dao层的代码和配置文件开发
1.代码 dao的接口 2配置文件 applicationContext.xml 数据源 sessionFactory 扫描接口的包路径 ---------------------------------------------------------------------- <!--数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@192.168.14.128:1521:orcl"></property> <property name="username" value="ssm_69"></property> <property name="password" value="ssm_69"></property> </bean> <!--sessionFactory--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > <property name="dataSource" ref="dataSource"></property> <!--别名路径扫描--> <property name="typeAliasesPackage" value="cn.itcast.domain"></property> <!--加载mybatis的核心配置文件--> </bean> <!--扫描dao接口路径--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.itcast.dao"></property> </bean>
3.service代码开发
代码 业务接口和实现类 实现类添加事务支持的注解 @Service @Transactional public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; @Override public List<Product> findAllProduct() { return productDao.findAllProduct(); } } 配置文件 扫描实现类包 事务管理器 事务的注解驱动 <!--扫描业务类包路径--> <context:component-scan base-package="cn.itcast.service"></context:component-scan> <!--事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource"></property> </bean> <!--事务注解驱动--> <tx:annotation-driven></tx:annotation-driven>
4.web层代码和配置
<context:component-scan base-package="cn.itcast.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--静态资源放行--> <mvc:default-servlet-handler></mvc:default-servlet-handler> -------------------------------------------------------- <!--springMvc的核心控制器--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--spring的监听器--> <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> <filter> <filter-name>charachterFileter</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>charachterFileter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
二、产品模块
1.产品列表
1.dao接口注解实现数据查询 select("select * from product") 2.jsp页面循环提取数据 3.处理数据的展示 日期格式化 domain的product get方法处理 //获取返回格式化的日期字符串显式页面 public String getDepartureTimeStr() { //返回处理后的字符串 return DateUtil.formatDateToStr(this.departureTime); } util模块下 添加工具类 public static String formatDateToStr(Date date){ try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return ""; } } 状态显示 return this.productStatus==0?"无效":"有效";
2.新增产品
1.新建按钮发起请求 后台跳转添加页面 οnclick='location.href="${pageContext.request.contextPath}/product/addProductUI"'> controller方法跳转页面 @RequestMapping("/addProductUI") public String addProductUI(){ return "product/productAdd"; } 2.添加产品的实现 form表单的请求url对应动作类的保存方法 <form action="${pageContext.request.contextPath}/product/addProduct" 表单内部的标签属性name 和 java对象属性一致 3.日期传递参数处理 自定义使用转换器 convert 配置在spirngMvc的xml文件中 注解解决 在domain的属性上使用@DateTimeFormat 自定义属性编辑器实现 @InitBinder public void initBind(WebDataBinder binder){ //参数1指定需要转换的类型 binder.registerCustomEditor(Date.class,new PropertiesEditor(){ @Override public void setAsText(String dateStr) throws IllegalArgumentException { //接受浏览器传递的字符串转换成日期后 Date date = DateUtil.parseStrToDate(dateStr); //字符串转换成日期后 赋值属性 setValue(date); } }); }
3.修改产品
页面的数据回显功能--------------------- 1.点击修改按钮传递产品id 跳转页面 ${pageContext.request.contextPath}/product/updateProductUI?productId=${product.id}"'>修改</button> 2.controller动作类查询对象返回页面 @RequestMapping("/updateProductUI") public String updateProductUI(Integer productId,Model model){ //通过id得到产品对象 Product product = productService.findProductById(productId); model.addAttribute("product",product); return "product/productUpdate"; } 3.jsp页面的数据回显 对象的数据通过标签的value属性 el表达式获取数据 对象的状态展示需要根据判断 选中下拉框 <option value="0" <c:if test="${product.productStatus==0}">selected </c:if>>关闭</option> <option value="1" ${product.productStatus==1?"selected":""}>开启</option> ------------------------------------------------------- 数据的更新功能 jsp页面传递被修改的产品id <input type="hidden" name="id" value="${product.id}"> 表单的name属性注意 表单的请求路径 动作类接受保存 跳转列表查询 @RequestMapping("/updateProduct") public String updateProduct(Product product){ //更新产品数据 productService.updateProduct(product); //更新成功跳转查询 return "redirect:/product/findAllProduct"; }
4.删除产品
1.点击删除按钮触发事件 οnclick='javascript:deleteById(${product.id});'>删除</button> function deleteById(productId) { //弹框提示确认删除否? var flag = window.confirm("你确定要删除吗?"); if(flag){ //确认删除请求删除的url地址 window.location.href="${pageContext.request.contextPath}/product/deleteProductById?productId="+productId; } } 2.controler接受id删除 @RequestMapping("/deleteProductById") public String deleteProductById(Integer productId){ productService.deleteProductById(productId); //查询列表展示 return "redirect:/product/findAllProduct"; }
三、订单模块
1.订单和产品数据列表展示懒加载形式实现
1.初始化数据库订单表
CREATE TABLE orders( id NUMBER(9) PRIMARY KEY , orderNum VARCHAR2(20) NOT NULL UNIQUE, orderTime TIMESTAMP(0), peopleCount NUMBER, orderDesc VARCHAR2(500), payType NUMBER(2), orderStatus NUMBER(2), productId NUMBER(9), FOREIGN KEY (productId) REFERENCES product(id) )
2.订单列表功能实现
dao的查询sql需要懒加载的形式编写 @Select("select * from orders") @Results({ @Result(column = "id",property = "id"), @Result(column = "productId",property = "product", one=@One(select="cn.itcast.dao.ProductDao.findProductById")), })
3.页面的数据展示
<%--下面是if判断和三目运算的比较--%> <td> <c:if test="${order.payType==0}"> 支付宝 </c:if> <c:if test="${order.payType==1}"> 微 信 </c:if> <c:if test="${order.payType==2}"> 其 他 </c:if> </td> <td>${order.orderStatus==1?"已支付": "未支付"}</td>