一. 内容简介
实现java后端用户管理接口,数据库使用msyql。
二. 软件环境
2.1 java 1.8
2.2 mysql Ver 8.0.13 for Win64 on x86_64 (MySQL Community Server - GPL)
2.3 IDEA ULTIMATE 2019.3
2.4d代码地址
https://gitee.com/JJW_1601897441/competitionAssistant.git
三.主要流程
3.1 创建数据库,创建数据表
3.2 开始编写接口,并测试
四.具体步骤
4.1 创建数据库,创建数据表,添加数据
数据表创建
DROP TABLE IF EXISTS `index_img`;
CREATE TABLE `index_img` (
`img_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主键',
`img_url` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '图片 图片地址',
`prod_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '竞赛id',
`seq` int(11) NOT NULL COMMENT '轮播图展示顺序 轮播图展示顺序,从小到大',
`status` int(11) NOT NULL COMMENT '是否展示:1表示正常显示,0表示下线 是否展示,1:展示 0:不展示',
`create_time` datetime(0) NOT NULL COMMENT '创建时间 创建时间',
`update_time` datetime(0) NOT NULL COMMENT '更新时间 更新',
PRIMARY KEY (`img_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '轮播图 ' ROW_FORMAT = Compact;
数据用假数据,也可以去另外一个博客下载我的数据,然后存入数据库中。
4.2 开始编写接口,测试
逆向工程生成实体类,
然后编写服务代码,IndexImgServiceImpl
package com.ca.service.impl;
import com.ca.dao.IndexImgMapper;
import com.ca.entity.IndexImg;
import com.ca.entity.Users;
import com.ca.service.IndexImgService;
import com.ca.vo.ResultVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
@Service
public class IndexImgServiceImpl implements IndexImgService {
@Resource
private IndexImgMapper indexImgMapper;
@Override
public ResultVO listIndexImgs(int length) {
Example example = new Example(IndexImg.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("status",1);
RowBounds rowBounds = new RowBounds(0, length); // 从第0条开始,返回10条数据
List<IndexImg> indexImg = indexImgMapper.selectByExampleAndRowBounds(example, rowBounds);
return new ResultVO(10000,"查询成功",indexImg);
}
}
api编写,IndexController
package com.ca.controller;
import com.ca.service.IndexImgService;
import com.ca.service.UserService;
import com.ca.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/index")
// value是详细说明,tags是标签
@Api(value = "提供用首页数据",tags = "首页数据")
// 允许跨域
@CrossOrigin
public class IndexController {
@Resource
private IndexImgService imgService;
@ApiOperation("首页轮播图数据")
@ApiImplicitParams({
@ApiImplicitParam(dataType = "int",name = "len", value =
"轮播图长度",required = true),
})
@RequestMapping(value = "/indeximg",method = RequestMethod.GET)
public ResultVO login(int len){
return imgService.listIndexImgs(len);
}
}