发表于: 2019-05-28 15:53:24
1 618
编写Mbatis-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.jnshu.Model.Banner" alias="Banner"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--reasonable:分页合理化参数,默认值为false。
当该参数设置为 true 时,pageNum<=0 时会查询第一页,
pageNum>pages(超过总数时),会查询最后一页。
默认false 时,直接根据参数进行查询。-->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
</configuration>
我的banner接口和实现类。
package com.jnshu.Dao;
import com.jnshu.Model.Banner;
import java.sql.SQLException;
import java.util.List;
/**
* @ClassName BannerMapper
* @Description TODO
* @Author liweichuan banner的接口
* @Date 2019/5/27 19:55
* @Version 1.0
**/
public interface BannerMapper {
//新增banner图
boolean addBanner(Banner banner)throws SQLException;
//修改banner图
boolean updateBanner(Banner banner)throws SQLException;
//删除banner图
boolean deleteBannerByImg(String img)throws SQLException;
//按照状态查询banner图
List<Banner> selectByStatus(long status)throws SQLException;
//按照创建人查询banner图
List<Banner> selectByCreateBy(String create_by)throws SQLException;
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jnshu.Dao.BannerMapper">
<resultMap id="bannerResultMap" type="Banner">
<id property="id" column="id" javaType="java.lang.Long"></id>
<result property="img" column="img" javaType="java.lang.String"></result>
<result property="create_by" column="create_by" javaType="java.lang.String"></result>
<result property="url" column="url" javaType="java.lang.String"></result>
<result property="status" column="status" javaType="java.lang.Long"></result>
<result property="create_time" column="create_time" javaType="java.lang.Long"></result>
<result property="update_by" column="update_by" javaType="java.lang.String"></result>
<result property="update_time" column="update_time" javaType="java.lang.Long"></result>
</resultMap>
<!-- 新增banner-->
<insert id="addBanner" paramterType="com.jnshu.Model.Banner" >
insert into banner values(id=#{id},img=#{img},create_by=#{create_by},url=#{url},status=#{status},
create_time=#{create_time},update_by=#{update_by},update_time=#{update_time})
</insert>
<!--更新banner-->
<update id="updateBanner" paramterType="com.jnshu.Model.Banner" >
update banner set img=#{img},create_by=#{create_by},url=#{url},status=#{status},
create_time=#{create_time},update_by=#{update_by},update_time=#{update_time} where id=#{id}
</update>
<!-- 删除banner,但是这个删除后,不会导致序号改变,也就是序号会空出来一个-->
<delete id="deleteBannerByImg" paramterType="java.lang.String">
delete from banner where img=#{img}
</delete>
<!-- 查询banner靠状态-->
<select id="selectByStatus" paramterType="java.lang.Long" resultMap="bannerResultMap">
select * from banner
<where>
<if test="status!=null">
status=#{status}
</if>
</where>
</select>
<!--查询banner靠创建人-->
<select id="selectByCreateBy" paramterType="java.lang.String" resultMap="bannerResultMap">
select * from banner
<where>
<if test="create_by!=null and create_by!=''">
create_by=#{create_by}
</if>
</where>
</select>
</mapper>
service和实现类
package com.jnshu.Service;
import com.jnshu.Model.Banner;
import java.sql.SQLException;
import java.util.List;
public interface BannerService {
//新增banner图
boolean addBanner(Banner banner)throws SQLException;
//修改banner图
boolean updateBanner(Banner banner)throws SQLException;
//删除banner图
boolean deleteBannerByImg(String img)throws SQLException;
//按照状态查询banner图
List<Banner> selectByStatus(long status)throws SQLException;
//按照创建人查询banner图
List<Banner> selectByCreateBy(String create_by)throws SQLException;
}
package com.jnshu.Service;
import com.jnshu.Dao.BannerMapper;
import com.jnshu.Model.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;
/**
* @ClassName BannerServiceImpl
* @Description TODO
* @Author liweichuan
* @Date 2019/5/28 14:39
* @Version 1.0
**/
@Service
public class BannerServiceImpl implements BannerService {
@Autowired
private BannerMapper bannerMapper;
//实现插入banner
@Override
public boolean addBanner(Banner banner) throws SQLException {
if (bannerMapper.addBanner(banner)){
return true;
}else {
return false;
}
}
//实现更新banner
@Override
public boolean updateBanner(Banner banner) throws SQLException {
if (bannerMapper.updateBanner(banner)){
return true;
}else {
return false;
}
}
//实现根据图像来删除
@Override
public boolean deleteBannerByImg(String img) throws SQLException {
if (bannerMapper.deleteBannerByImg(img)){
return true;
}else {
return false;
}
}
//实现依靠状态来查询
@Override
public List<Banner> selectByStatus(long status) throws SQLException {
return bannerMapper.selectByStatus(status);
}
//实现依靠作者的名字来查询
@Override
public List<Banner> selectByCreateBy(String create_by) throws SQLException {
return bannerMapper.selectByCreateBy(create_by);
}
}
编写接口文档
接口文档分为:
请求地址:在写接口文档时不用带域名或ip地址 uri:以/a开头,如果需要登录才能调用的接口(如新增、修改;前台的用户个人信息,资金信息等)后面需要加/u,即:/a/u;中间一般放表名或者能表达这个接口的单词;get方法,如果是后台通过搜索查询列表,那么以/search结尾,如果是前台的查询列表,以/list结尾
请求方式:主要是http的四大方法,GET,POST,PUT,DELETE。
参数:主要是
我们通过上面的表格来约定需要前端提供的数据名,数据的类型,是否必须提供和数据的介绍(类似约定0=商品上架,1=商品下架,方便阅读)。
返回值:,我们使用json来返回值。
接口的第五要素----访问协议(例:http、https等)一般默认为http
banner模块
新增banner
请求地址:post /a/u/banner
请求参数:
字段 | 说明 | 类型 | 是否必填 | 备注 |
---|---|---|---|---|
img | 图片存储路径 | String | 是 | |
create_by | 创建人 | String | 是 | |
url | 跳转外链 | String | 是 | |
status | 状态 | number | 是 | 0是下架,1是上架默认取值0 |
create_time | 创建时间 | number | 是 |
字段 | 说明 | 类型 |
---|---|---|
code | 状态码 | number |
message | 消息 | string |
2.编辑banner
请求地址:put /a/u/banner/{id}
字段 | 说明 | 类型 | 是否必填 | 备注 |
---|---|---|---|---|
img | 图片存储路径 | String | 是 | |
create_by | 创建人 | String | 是 | |
url | 跳转外链 | String | 是 | |
status | 状态 | number | 是 | 0是下架,1是上架默认取值0 |
create_time | 创建时间 | number | 是 | |
update_by | 修改人 | String | 是 | |
update_time | 修改时间 | number | 是 |
字段 | 说明 | 类型 |
---|---|---|
code | 状态码 | number |
message | 消息 | string |
3.删除banner
请求地址: delete /a/u/banner/{id}
请求参数:
字段 | 说明 | 类型 | 是否必填 | 备注 |
---|---|---|---|---|
id | 图片存储路径 | number | 是 |
返回参数:
字段 | 说明 | 类型 |
---|---|---|
code | 状态码 | number |
message | 消息 | string |
评论