看过之前的蜕变系列文章,相信你对SpringMVC 、Spring、 Mybatis的整合有了一定的心得,学会了搭建属于自己的开发框架。今天我们就在这个基础上写一个demo程序,演示下怎么在框架下进行开发。
猿蜕变同样是一个原创系列文章,帮助你从一个普通的小白,开始掌握一些行业内通用的框架技术知识以及锻炼你对系统设计能力的提升,完成属于你的蜕变,更多精彩内容,敬请大家关注公主号猿人工厂,点击猿人养成获取!
使用框架编写代码,有利于让代码保持相对稳定的方式进行开发,模式也比较固定,一套框架产出的代码也十分便于后期的维护。
我们搭建的框架中,对代码进行了分层处理。Controller负责和页面进行交互,关注web层后端的逻辑,Service关心功能层面的业务逻辑,为web层的功能点,提供业务层面的功能实现。Dao层,供service调用,为具体的业务服务提供数据支撑。
接下来我们通过一个小demo来感受下怎么使用SpringMVC、Spring、Mybatis框架进行开发。
第一步 编写你的页面
addTravelRoute.jsp
<%@page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"isELIgnored="false"%>
ssm框架测试页面
线路名称:
价格:
线路介绍:
线路日期:
第二步 编写你的web层
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
queryTravelByPage(Long startRow,Long endRow);
/**
* 根据Id查询
* @param travelRouteId
* @return
*/
TravelRoute queryTravelById(LongtravelRouteId);
/**
* 分页查询TravelRoute列表
* @param travelRouteName
* @return
*/
List
queryTravelByName(StringtravelRouteName);
/**
* 模糊查询TravelRoute列表
* @param travelRoute
* @return
*/
List
queryTravelByQuery(TravelRoute travelRoute);
/**
* 模糊查询TravelRoute列表
* @param travelRoute
* @return
*/
List
queryTravelByChooseQuery(TravelRoute travelRoute);
/**
* 根据travelRouteIdList返回列表
* @param travelRouteIdList
* @return
*/
List
queryTravelByForEach(List travelRouteIdList);
}
第五步 编写你的Mapper
TravelRouteMapper.xml
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
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})
SELECT @@identity
delete from travel_route where travel_route_id=#{travelRouteId}
update
travel_route
set
travel_route_name=#{travelRouteName},
travel_route_price=#{travelRoutePrice},
travel_route_introduce=#{travelRouteIntroduce},
travel_route_flag=#{travelRouteFlag},
travel_route_date=#{travelRouteDate},
isThemeTour=#{isThemeTour},
travel_route_count=#{travelRouteCount},
travel_route_cid=#{travelRouteCid},
travel_route_image=#{travelRouteImage},
travel_route_seller_id=#{travelRouteSellerId}
where travel_route_id =#{travelRouteId}
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
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
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
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
and travel_route_name like '%' #{travelRouteName} '%'
and = #{travelRoutePrice}]]>
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
and travel_route_name like '%' #{travelRouteName} '%'
andtravel_route_price>#{travelRoutePrice}
1 >2
select
from travel_route
travel_route_id IN
#{id}
1 >2
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
我建了一个技术群,群里有很多高手,加小编微信,备注:学习。带你见识更多的高手,帮你快速成长。