idea创建模块
创建项目
create new project --maven–创建–把src删除–点击项目右键–new–module–spring initializr
–勾选如下jar包
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ySjC1DUJ-1583218902060)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1573190984369.png)]
然后就完成了
导入swagger两个jar包
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
编写application.yml文件
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/itripdb?serverTimezone=UTC
username: root
password: root
mybatis:
type-aliases-package: com.szxs.entity
mapper-locations: classpath:mapping/*.xml
编写实体类–实现序列化
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BBs3tzb3-1583218902061)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1573191468926.png)]
mapper接口–打上注解@Mapper
package com.szxs.mapper;
import com.szxs.entity.ItripUser;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ItripUserMapper {
ItripUser queryItripUser(ItripUser itripUser);
}
sql语句–创建mapping文件夹–编写***.xml文件
<?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.szxs.mapper.ItripUserMapper">
<select id="queryItripUser" resultType="ItripUser">
SELECT id,userCode,userType,flatId,userName,activated
FROM itrip_user
where userCode=#{userCode} and userPassword=#{userPassword}
</select>
</mapper>
service接口
package com.szxs.service;
import com.szxs.entity.ItripUser;
import com.szxs.vo.VoToken;
public interface ItripUserService {
/**
* 用户登录
* @param itripUser userCode userPassword
* @return null登录失败 否则登录成功
*/
VoToken login(ItripUser itripUser);
}
service实现类–打上注解@Service
package com.szxs.service.impl;
import com.alibaba.fastjson.JSON;
import com.szxs.entity.ItripUser;
import com.szxs.mapper.ItripUserMapper;
import com.szxs.service.ItripUserService;
import com.szxs.util.DateUtil;
import com.szxs.util.EmptyUtil;
import com.szxs.util.RedisUtil;
import com.szxs.vo.VoToken;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;
@Service
public class ItripUserServiceImpl implements ItripUserService {
@Resource
private ItripUserMapper itripUserMapper;
@Resource
private RedisUtil redisUtil;
@Override
public VoToken login(ItripUser itripUser) {
itripUser.setUserPassword(DigestUtils.md5DigestAsHex(itripUser.getUserPassword().getBytes()));
ItripUser user = itripUserMapper.queryItripUser(itripUser);
if(EmptyUtil.isEmpty(user)){
//登录失败
return null;
}
//登录成功
VoToken v=new VoToken();
v.setExpTime(new Date());
v.setGenTime(DateUtil.getExipDate(v.getExpTime()));
String token=generateToken(user);
//保存令牌和用户信息
redisUtil.set(token, JSON.toJSONString(user));
v.setToken(token);
return v;
}
public String generateToken(ItripUser user){
StringBuilder sb=new StringBuilder();
sb.append("token:");
sb.append("PC-");
sb.append(DigestUtils.md5DigestAsHex(user.getUserCode().getBytes())+"-");
sb.append(user.getId()+"-");
sb.append(DateUtil.getCurrentDate("yyyyMMddHHmmss")+"-");
sb.append(UUID.randomUUID().toString().substring(0,6));
return sb.toString();
}
}
编写controller,进行测试即可完成!
package com.szxs.controller;
import com.szxs.dto.Dto;
import com.szxs.service.ItripAreaDicService;
import com.szxs.util.DtoUtil;
import com.szxs.util.EmptyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Api(tags = "城市查询")
@RequestMapping("/api")
public class ItripAreaDicController {
@Resource
private ItripAreaDicService itripAreaDicService;
@ApiOperation(value = "热点城市查询",produces = "application/json",
response = Dto.class,notes = "查询国内国外热门城市")
@ApiImplicitParams({
@ApiImplicitParam(value = "国内国外",name = "type",required = true,dataType = "Integer")
})
@GetMapping("/hotel/queryhotcity/{type}")
public Dto queryhotcity(@PathVariable Integer type){
if(EmptyUtil.isEmpty(type)){
return DtoUtil.getFail("10201","不能为空");
}
try{
return DtoUtil.getSuccess(itripAreaDicService.queryHotelByisChina(type));
}catch (Exception e){
return DtoUtil.getFail("10202","系统异常");
}
}
}
}
try{
return DtoUtil.getSuccess(itripAreaDicService.queryHotelByisChina(type));
}catch (Exception e){
return DtoUtil.getFail("10202","系统异常");
}
}
}