请选择 进入手机版 | 继续访问电脑版
收起左侧

对数据库操作的流程-基于小鹿项目基础上

[复制链接]

509

主题

526

帖子

1万

积分

管理员

站长

Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20

积分
15970

灌水之王

QQ
发表于 2018-11-30 15:50:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
首先建表
[SQL] 纯文本查看 复制代码
DROP TABLE IF EXISTS `hot_search`;
CREATE TABLE `hot_search` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '热门关键词',
  `isuse` tinyint(4) unsigned NOT NULL DEFAULT '1' COMMENT '是否启用 1是 0否',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='热门搜索';


插入两条数据::
[SQL] 纯文本查看 复制代码
INSERT INTO `hot_search` VALUES ('1', '马丁靴', '1');
INSERT INTO `hot_search` VALUES ('2', '最新款羽绒服', '1');


一下新建流程不分先后:
1、在resources/mybatis/mapper 下新建HotSearchMapper.xml 文件:
内容如下:
[HTML] 纯文本查看 复制代码
<?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.xiaolu.dao.mapper.HotSearchMapper" >
  <resultMap id="BaseResultMap" type="com.xiaolu.common.entity.HotSearch" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="isuse" property="isuse" jdbcType="TINYINT" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, isuse
  </sql>
  <select id="getAllList" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from hot_search
  </select>

</mapper>



里面包含一条最简单的SQL语句,查询全部数据

2、再在com.xiaolu.common.entity下建立实体文件:HotSearch.java
内容如下:
[HTML] 纯文本查看 复制代码
package com.xiaolu.common.entity;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

public class HotSearch implements Serializable {
    @ApiModelProperty("id,编辑时必填")
    private Integer id;
    @ApiModelProperty("热门关键词")
    private String name;
    @ApiModelProperty("是否启用")
    private Byte isuse;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Byte getIsuse() {
        return isuse;
    }

    public void setIsuse(Byte isuse) {
        this.isuse = isuse;
    }

    public static enum  isuse{
        use(Byte.valueOf("1")),
        del(Byte.valueOf("0"));

        private Byte code;

        public Byte getCode() {
            return code;
        }

        public void setCode(Byte code) {
            this.code = code;
        }

        isuse(Byte code) {
            this.code = code;
        }

    }
}



3、再在com.xiaolu.dao.mapper 下建立HotSearchMapper.java 文件:
内容如下:
[Java] 纯文本查看 复制代码
package com.xiaolu.dao.mapper;

import com.xiaolu.common.entity.HotSearch;

import java.util.List;

public interface HotSearchMapper {

    List<HotSearch> getAllList();

}



一个简单的接口-获取全部热门列表数据

4、再在com.xiaolu.portal.api 下建立控制器HotSearchController:
代码如下:
[Java] 纯文本查看 复制代码
package com.xiaolu.portal.api;

import com.xiaolu.common.utils.JsonResultEntity;
import com.xiaolu.portal.service.HotSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangmaocheng
 */
@RestController
@Api(description = "热门搜索管理模块")
@RequestMapping("/api/hotSearch")
public class HotSearchController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    ResponseEntity<JsonResultEntity> result = null;

    @Autowired
    private HotSearchService hotSearchService;

    @ApiOperation(value = "获取所有热门搜索", notes = "获取所有热门搜索")
    @RequestMapping(value = "hotSearchList", method = RequestMethod.GET)
    public ResponseEntity<JsonResultEntity> hotSearchList(){
        result = hotSearchService.hotSearchList();

        return result;
    }
}




一个简单的api接口,获取全部热门搜索数据

5、再在com.xiaolu.portal.service.impl 下建立 HotSearchServicelmpl 文件:
代码如下:
[Java] 纯文本查看 复制代码
package com.xiaolu.portal.service.impl;

import com.xiaolu.common.entity.AdBanner;
import com.xiaolu.common.entity.HotSearch;
import com.xiaolu.common.enums.MessageEnum;
import com.xiaolu.common.utils.JsonResultEntity;
import com.xiaolu.common.utils.JsonResultUtils;
import com.xiaolu.dao.mapper.HotSearchMapper;
import com.xiaolu.portal.service.HotSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author wwangmaocheng
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HotSearchServiceImpl implements HotSearchService {

    @Autowired
    private HotSearchMapper hotSearchMapper;

    @Override
    public ResponseEntity<JsonResultEntity> hotSearchList(){
        List<HotSearch> hotSearch=hotSearchMapper.getAllList();
        return ResponseEntity.ok(JsonResultUtils.success(hotSearch, MessageEnum.COMMON_STATUS_OK.getCode(), MessageEnum.COMMON_STATUS_OK.getMessage()));
    }

}
[img=1258,811]http://qn.fologde.com/2018-11-30/15:49:39.jpg[/img]



啥意思我现在也不是太懂,反正需要

6、再在com.xiaolu.poeral.service 下建立service HotSearchService.java文件:
代码如下:
[Java] 纯文本查看 复制代码
package com.xiaolu.portal.service;

import com.xiaolu.common.utils.JsonResultEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;


@Service
public interface HotSearchService {

    ResponseEntity<JsonResultEntity> hotSearchList();

}




经过以上六步,一个简单的查询就流程就建立成功了!
模拟请求:










QQ:2424004764
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|客户端下载|先锋者IT论坛 ( 湘ICP备16007032号湘公网安备 43030202001052号

GMT+8, 2019-12-14 05:26 , Processed in 0.142698 second(s), 14 queries , File On.

Powered by Discuz! X3.3

© 2017 bbs.fologde.com. Template By Gstudio

快速回复 返回顶部 返回列表