SpringBoot校园导航图后台代码实现
⼀、前⾔
数据结构课设写⼀个校园导航图,正好舍友会安卓,于是我就⽤SpringBoot和他合作写了⼀个前后端交互的项⽬,由于时间之后3天,所以就没有加⼀些⽤户模块、权限模块,也就简单实现了景点信息、路径信息的增删改查。
⼆、所需技术
SpringBoot搭建项⽬的整体框架
景点数据、路径数据采⽤MySQL数据库
查询数据添加缓存使⽤Redis数据库
DAO层使⽤MyBatis框架
都是⼀些基本的开发框架,也没有涉及到分布式⽅⾯的东西,⽐较简单。
三、数据库构建
由于功能简单,数据库设计的也⽐较简单
spot景点表结构:
path表结构:
四、代码展⽰
1.相关bean类以及相应⽤法
有些bean类实现了Serializable接⼝,这是因为在springboot整合Redis时,需要加⼊缓存的对象序列化。
(由于代码较多,⼀些⽆关紧要的代码就会省略)
DealResult.java
⽤来封装返回的数据,⾥⾯记录了返回是否成功、返回信息、返回数据。
public class DealResult {
private boolean isSucceed;
private String resultInfo;
private Object data;
public DealResult(){}
public DealResult(boolean isSucceed, String resultInfo){
this.isSucceed = isSucceed;
}
}
Coord.java
⽤来封装⼀个景点的坐标类,可以将数据库查询到的路径中经过的景点坐标进⾏封装。public class Coord implements Serializable {
private double x;
private double y;
public Coord(){
}
public Coord(double x,double y){
this.x = x;
this.y = y;
}
Spot.java
景点类,⽤来封装景点信息
public class Spot implements Serializable {
// 景点编号
private Integer spotId;
// 景点名称
private String spotName;
// 景点横坐标
private double coordX;
// 景点纵坐标
private double coordY;
// 景点信息
private String spotInfo;
// 连接景点数组
private Coord[] coords;
public Spot(){}
}
Path.java
路径类,记录该条路径的起点、终点、长度信息
public class Path implements Serializable {
// 路径编号
private Integer pathId;
// 路径起点
private Integer startSpotId;
// 路径终点
private Integer endSpotId;
// 路径长度
private Double pathLength;
public Path(){}
public Path(Integer startSpotId, Integer endSpotId, Double pathLength){
this.startSpotId = startSpotId;
this.pathLength = pathLength;
}
}
2.springboot整合Redis配置类
在使⽤Redis时,需要注意的如果同类⾥⾯的⽅法需要调⽤另外⼀个⽅法的缓存,那么需要使⽤新的代理对象调⽤该⽅法才能实现数据的缓存,后⾯会遇到这种情况。
RedisConfig.java
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory){
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(60))
.disableCachingNullValues();
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.transactionAware()
.build();
}
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory){
StringRedisTemplate template =new StringRedisTemplate(factory);
RedisSerializer keySerializer =new StringRedisSerializer();// 设置key序列化类,否则key前⾯会多了⼀些乱码
springboot结构template.setKeySerializer(keySerializer);
setValueSerializer(template);//设置value序列化
template.afterPropertiesSet();
template.setEnableTransactionSupport(true);
return template;
}
private void setValueSerializer(StringRedisTemplate template){
@SuppressWarnings({"rawtypes","unchecked"})
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om =new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
}
}
3.景点相关功能
1. Controller层
SpotServlet.java
@RestController
public class SpotServlet {
@Autowired
SpotService spotService;
@GetMapping("/querySpotInfo")
public DealResult querySpotInfo(){
DealResult dealResult =new DealResult();
try{
List<Spot> spotList = spotService.querySpots();
if(spotList != null){
dealResult.setSucceed(true);
dealResult.setData(spotList);
}
}catch(Exception e){
dealResult.setSucceed(false);
dealResult.setResultInfo("查询景点信息失败!");
}
return dealResult;
}
@GetMapping("/querySpot")
public Spot querySpotById(Integer id){
return spotService.querySpotById(id);
}
}
2. Service层
SpotServiceImpl.java
@Service
public class SpotServiceImpl implements SpotService {
@Autowired
SpotMapper spotMapper;
@Autowired
PathMapper pathMapper;
@Autowired
SpotService spotService;
/**
* 查询景点
* 将该景点为起点的路经的终点坐标封装为Coord存储到 coords[] 数组中
*/
@Cacheable(cacheNames ="spots")
@Override
public List<Spot>querySpots(){
List<Spot> spotList = spotMapper.querySpots();
for(Spot spot :
spotList){
List<Path> nextSpotsList = pathMapper.SpotId());
spot.setCoords(nextSpotsList.size());
Coord[] coords = Coords();
int i =0;
for(Path path :
nextSpotsList){
Spot spot1 = spotService.EndSpotId());
coords[i++]=new CoordX(), CoordY());
}
}
return spotList;
}
/**
* 根据编号查询景点
*/
@Cacheable(cacheNames ="spot")
@Override
public Spot querySpotById(Integer id){
return spotMapper.querySpotById(id);
}
}
可以在⽅法querySpots()中看到需要调⽤下⾯的⽅法querySpotById(),那么就将本类对象注⼊,然后调⽤才能将查询结果加⼊缓存。
3. Mapper层
因为没有复杂的SQL语句,就没有使⽤xml⽂件,使⽤了注解SQL
SpotMapper.java
@Mapper
@Component
public interface SpotMapper {
@Select("select * from spot where spotName=#{spotName}")
Spot querySpotByName(String spotName);
@Select("select * from spot")
List<Spot>querySpots();
@Select("select * from spot where spotId=#{spotId}")
Spot querySpotById(int spotId);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论