看过之前的蜕变系列文章,相信你对SpringMVC 、Spring、 Mybatis的整合有了一定的心得,学会了搭建属于自己的开发框架。今天我们就在这个基础上写一个demo程序,演示下怎么在框架下进行开发。 猿蜕变同样是一个原创系列文章,帮助你从一个普通的小白,开始掌握一些行业内通用的框架技术知识以及锻炼你对系统设计能力的提升,完成属于你的蜕变,更多精彩内容,敬请大家关注公主号猿人工厂,点击猿人养成获取! 使用框架编写代码,有利于让代码保持相对稳定的方式进行开发,模式也比较固定,一套框架产出的代码也十分便于后期的维护。 我们搭建的框架中,对代码进行了分层处理。Controller负责和页面进行交互,关注web层后端的逻辑,Service关心功能层面的业务逻辑,为web层的功能点,提供业务层面的功能实现。Dao层,供service调用,为具体的业务服务提供数据支撑。 接下来我们通过一个小demo来感受下怎么使用SpringMVC、Spring、Mybatis框架进行开发。 第一步 编写你的页面 addTravelRoute.jsp 第二步 编写你的web层 第三步 编写你的服务层 第四步 编写你的数据层 第五步 编写你的Mapper TravelRouteMapper.xml 我建了一个技术群,群里有很多高手,加小编微信,备注:学习。带你见识更多的高手,帮你快速成长。
<%@page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"isELIgnored="false"%>
<html>
<head>
<title>ssm框架测试页面</title>
</head>
<body>
<form action="/addTravelRoute"method=post>
<lable>线路名称:</lable>
<input type="text"name="travelRouteName" id="travelRouteName" /><br/>
<lable>价格:</lable>
<input type="text"name="travelRoutePrice" id="travelRoutePrice" /><br/>
<lable>线路介绍:</lable>
<input type="text"name="travelRouteIntroduce" id="travelRouteIntroduce"/><br />
<lable>线路日期:</lable>
<input type="text"name="travelRouteDate" id="travelRouteDate" /><br/>
<input type="submit"value="提交"id="submit" /><br />
</form>
</body>
</html>
package com.pz.web.study.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.pz.web.study.ssm.domain.TravelRoute;
import com.pz.web.study.ssm.service.TravelRouteService;
@Controller
publicclass TravelRouteController {
private TravelRouteService travelRouteService;
@RequestMapping("/addTravelRouteFrom")
public String addTraveFrom() throws Exception {
return"addTravelRoute";
}
@RequestMapping("/addTravelRoute")
public String addTravelRoute(TravelRoutetravelRoute) throws Exception {
travelRoute.setTravelRouteCid(1);
travelRoute.setIsThemeTour("1");
travelRoute.setTravelRouteFlag(1);
travelRoute.setTravelSellerId(1L);
travelRoute.setTravelRouteImage("img/product/small/m3db4d2277b5df3d98597f79082ef92d6d.jpg");
travelRouteService.addTravelRoute(travelRoute);
return"success";
}
publicvoidsetTravelRouteService(TravelRouteServicetravelRouteService) {
this.travelRouteService = travelRouteService;
}
}
package com.pz.web.study.ssm.service;
import com.pz.web.study.ssm.domain.TravelRoute;
public interface TravelRouteService {
public voidaddTravelRoute(TravelRoute travelRoute);
}
package com.pz.web.study.ssm.service.impl;
import com.pz.web.study.ssm.dao.TravelRouteDao;
import com.pz.web.study.ssm.domain.TravelRoute;
import com.pz.web.study.ssm.service.TravelRouteService;
public class TravelRouteServiceImpl implements TravelRouteService {
private TravelRouteDao travelRouteDao;
@Override
publicvoidaddTravelRoute(TravelRoute travelRoute) {
travelRouteDao.add(travelRoute);
}
publicvoidsetTravelRouteDao(TravelRouteDao travelRouteDao) {
this.travelRouteDao =travelRouteDao;
}
}
package com.pz.web.study.ssm.dao;
import java.util.List;
import com.pz.web.study.ssm.domain.TravelRoute;
/**
*
* @author pangzi
*
*/
public interface TravelRouteDao {
/**
* 添加线路
* @param travelRoute
*/
void add(TravelRoute travelRoute);
/**
* 按id删除记录
* @param travelRouteId
*/
void deleteById(Long travelRouteId);
/**
* 更新
* @param travelRoute
*/
void updateById(TravelRoute travelRoute);
/**
* 分页查询TravelRoute列表
* @param startRow
* @param endRow
* @return
*/
List<TravelRoute>queryTravelByPage(Long startRow,Long endRow);
/**
* 根据Id查询
* @param travelRouteId
* @return
*/
TravelRoute queryTravelById(LongtravelRouteId);
/**
* 分页查询TravelRoute列表
* @param travelRouteName
* @return
*/
List<TravelRoute> queryTravelByName(StringtravelRouteName);
/**
* 模糊查询TravelRoute列表
* @param travelRoute
* @return
*/
List<TravelRoute>queryTravelByQuery(TravelRoute travelRoute);
/**
* 模糊查询TravelRoute列表
* @param travelRoute
* @return
*/
List<TravelRoute>queryTravelByChooseQuery(TravelRoute travelRoute);
/**
* 根据travelRouteIdList返回列表
* @param travelRouteIdList
* @return
*/
List<TravelRoute>queryTravelByForEach(List<Long> travelRouteIdList);
}
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTDMapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pz.web.study.ssm.dao.TravelRouteDao">
<resultMap id="TravelRouteType"type="TravelRoute" >
<result column = "travel_route_id"property="travelRouteId"/>
<result column = "travel_route_name"property="travelRouteName"/>
<result column = "travel_route_price"property="travelRoutePrice"/>
<result column = "travel_route_introduce"property="travelRouteIntroduce"/>
<result column = "travel_route_flag"property="travelRouteFlag"/>
<result column = "travel_route_date"property="travelRouteDate"/>
<result column = "isThemeTour"property="isThemeTour"/>
<result column = "travel_route_count"property="travelRouteCount"/>
<result column = "travel_route_cid"property="travelRouteCid"/>
<result column = "travel_route_image"property="travelRouteImage"/>
<result column ="travel_route_seller_id"property="travelRouteSellerId"/>
</resultMap>
<insert id="add"parameterType="com.pz.web.study.ssm.domain.TravelRoute">
INSERT
INTO travel_route
(travel_route_name,travel_route_price,travel_route_introduce,travel_route_flag,travel_route_date,isThemeTour,travel_route_count,travel_route_cid,travel_route_image,travel_route_seller_id)
values(#{travelRouteName},#{travelRoutePrice},#{travelRouteIntroduce},#{travelRouteFlag},#{travelRouteDate},#{isThemeTour},#{travelRouteCount},#{travelRouteCid},#{travelRouteImage},#{travelRouteSellerId})
<selectKey resultType="Long"keyProperty="travelRouteId" order="AFTER">
SELECT @@identity
</selectKey>
</insert>
<delete id="deleteById"parameterType="Long">
delete from travel_route where travel_route_id=#{travelRouteId}
</delete>
<update id="updateById">
update
travel_route
set
<if test="travelRouteName!= null and travelRouteName != ''">
travel_route_name=#{travelRouteName},
</if>
<if test="travelRoutePrice!= null and travelRoutePrice >0 ">
travel_route_price=#{travelRoutePrice},
</if>
<if test="travelRouteIntroduce!= null and travelRouteIntroduce != ''">
travel_route_introduce=#{travelRouteIntroduce},
</if>
<if test="travelRouteFlag!= null ">
travel_route_flag=#{travelRouteFlag},
</if>
<if test="travelRouteFlag!= null ">
travel_route_date=#{travelRouteDate},
</if>
<if test="travelRouteFlag!= null ">
isThemeTour=#{isThemeTour},
</if>
<if test="travelRouteCount!= null and travelRouteCount > 0 ">
travel_route_count=#{travelRouteCount},
</if>
<if test="travelRouteCid!= null and travelRouteCid > 0 ">
travel_route_cid=#{travelRouteCid},
</if>
<if test="travelRouteImage!= null and travelRouteImage != ''">
travel_route_image=#{travelRouteImage},
</if>
<if test="travelRouteSellerId!= null and travelRouteSellerId >0 ">
travel_route_seller_id=#{travelRouteSellerId}
</if>
where travel_route_id =#{travelRouteId}
</update>
<select id="queryTravelByPage"resultMap="TravelRouteType" parameterType="java.util.Map">
select
travel_route_id ,
travel_route_name ,
travel_route_price ,
travel_route_introduce ,
travel_route_flag ,
travel_route_date ,
isThemeTour ,
travel_route_count ,
travel_route_cid ,
travel_route_image ,
travel_route_seller_id
from travel_route order by travel_route_id desc limit #{startRow},#{endRow}
</select>
<select id="queryTravelById"resultMap="TravelRouteType" parameterType="Long">
select
travel_route_id ,
travel_route_name ,
travel_route_price ,
travel_route_introduce ,
travel_route_flag ,
travel_route_date ,
isThemeTour ,
travel_route_count,
travel_route_cid ,
travel_route_image ,
travel_route_seller_id
from travel_route where travel_route_id =#{travelRouteId}
</select>
<select id="queryTravelByName"resultType="TravelRoute" parameterType="String">
select
travel_route_id as travelRouteId,
travel_route_name as travelRouteName,
travel_route_price as travelRoutePrice,
travel_route_introduce astravelRouteIntroduce,
travel_route_flag as travelRouteFlag,
travel_route_date as travelRouteDate,
isThemeTour as isThemeTour,
travel_route_count as travelRouteCount,
travel_route_cid as travelRouteCid,
travel_route_image as travelRouteImage,
travel_route_seller_id astravelRouteSellerId
from travel_route where travel_route_name like '%' #{travelRouteName} '%'
</select>
<select id="queryTravelByQuery"resultMap="TravelRouteType" parameterType="TravelRoute">
select
travel_route_id ,
travel_route_name ,
travel_route_price ,
travel_route_introduce ,
travel_route_flag ,
travel_route_date ,
isThemeTour ,
travel_route_count,
travel_route_cid ,
travel_route_image ,
travel_route_seller_id
from travel_route
<where>
<if test="travelRouteName!= null and travelRouteName != ''">
and travel_route_name like '%' #{travelRouteName} '%'
</if>
<if test="travelRoutePrice!= null and travelRoutePrice >0 ">
and <![CDATA[travel_route_price >= #{travelRoutePrice}]]>
</if>
</where>
</select>
<select id="queryTravelByChooseQuery"resultMap="TravelRouteType" parameterType="TravelRoute">
select
travel_route_id ,
travel_route_name ,
travel_route_price ,
travel_route_introduce ,
travel_route_flag ,
travel_route_date ,
isThemeTour ,
travel_route_count,
travel_route_cid ,
travel_route_image ,
travel_route_seller_id
from travel_route
<where>
<choose>
<when test="travelRouteName!= null and travelRouteName != ''">
and travel_route_name like '%' #{travelRouteName} '%'
</when>
<when test="travelRoutePrice!= null and travelRoutePrice >0 ">
andtravel_route_price>#{travelRoutePrice}
</when>
<otherwise>
1 >2
</otherwise>
</choose>
</where>
</select>
<select id="queryTravelByForEach"resultMap="TravelRouteType" >
select
<include refid="commonselect"/>
from travel_route
<where>
<choose>
<when test="list != nulland list.size>0">
travel_route_id IN
<foreach collection="list"open="(" close=")"item="id" separator=",">
#{id}
</foreach>
</when>
<otherwise>
1 >2
</otherwise>
</choose>
</where>
</select>
<sql id = "commonselect">
travel_route_id ,
travel_route_name ,
travel_route_price ,
travel_route_introduce ,
travel_route_flag ,
travel_route_date ,
isThemeTour ,
travel_route_count,
travel_route_cid ,
travel_route_image ,
travel_route_seller_id
</sql>
</mapper>
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算