Springboot 技术整合--笔记2-2--mybatis注解配置&pageHelper

关键步骤

  • 数据库脚本
  • 表或查询对应的pojo实现
  • 表或查询对应的Mapper
  • 单元测试&运行
  • 表或查询的Mapper对应的Service(@Service)
  • web api对应的Controller

数据库准备

用powerDesigner设计表nw_robotInfo


powerDesigner设计表

运行脚本

drop table if exists robotInfo;

/*==============================================================*/
/* Table: robotInfo                                             */
/*==============================================================*/
create table robotInfo
(
   pluginisActvie       int comment '插件是否激活 1:激活;0:未激活',
   pluginVersionCode    varchar(32) comment '插件版本号,如:1905161446 ',
   systemVersion        varchar(32) binary comment '系统版本号Android或ios的系统版本',
   imei                 varchar(255) not null comment '手机imei,如果没用授权则为:imei88888888',
   xposedIsInstall      int comment 'xposed框架是否安装 1:已安装、0 未安装',
   wechatIsInstall      int comment '手机是否安装微信 ',
   wechatVersionCode    varchar(50) binary comment '微信的版本号 如:6.7.3',
   wechatStatus         int comment '微信登录状态  :0:未登录;1:登录;2:封号或异常',
   supportCurWechat     int comment '是否支持当前微信版本',
   nickName             varchar(255) binary comment '微信昵称',
   wechatId             varchar(255) binary not null comment '微信原始id',
   primary key (imei, wechatId)
);

alter table robotInfo comment '微信机器人信息(对应心跳)';

INSERT INTO `nw_robotInfo` VALUES (0, '1905161446', '4.4', '', 1, 1, '6.6.0', 0, 1, '喔喔我', 'wxid_on8oksh88zo22');

image.png

写数据库表对应的pojo实体类nw_robotInfo
采用idea的mybatis插件生成,后来发现根本没用必要



/**
 * 机器人
 * 特别说明:之前采用mybatis的逆向生成对象,发现字段明会与数据库不一致,估计是驼峰转换问题,会造成get 和set方法找不到的问题
 * 设为true表示开启驼峰转换:mybatis.configuration.mapUnderscoreToCamelCase=true
 * 如果用使用@Data 注解需要lombok插件支持,@Data =@Getter +@Setter*
 */
public class Robotinfo {
    /**
     * 手机imei,如果没用授权则为:imei88888888
     */

    private String imei;

    /**
     * 微信原始id
     */

    private String wechatId;

    /**
     * 插件是否激活 1:激活;0:未激活
     */
    private Integer pluginisActvie;

    /**
     * 插件版本号,如:1905161446 
     */
    private String pluginVersionCode;

    /**
     * 系统版本号Android或ios的系统版本
     */
    private String systemVersion;

    /**
     * xposed框架是否安装 1:已安装、0 未安装
     */
    private Integer xposedIsInstall;

    /**
     * 手机是否安装微信 
     */
    private Integer wechatIsInstall;

    /**
     * 微信的版本号 如:6.7.3
     */
    private String wechatVersionCode;

    /**
     * 微信登录状态  :0:未登录;1:登录;2:封号或异常
     */
    private Integer wechatStatus;

    /**
     * 是否支持当前微信版本
     */
    private Integer supportCurWechat;

    /**
     * 微信昵称
     */
    private String nickName;

    public Robotinfo() {
        pluginisActvie =0;
        xposedIsInstall =0;
        wechatIsInstall =0;
        wechatStatus =0;
        supportCurWechat =0;
    }

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public String getWechatId() {
        return wechatId;
    }

    public void setWechatId(String wechatId) {
        this.wechatId = wechatId;
    }

    public Integer getPluginisActvie() {
        return pluginisActvie;
    }

    public void setPluginisActvie(Integer pluginisActvie) {
        this.pluginisActvie = pluginisActvie;
    }

    public String getPluginVersionCode() {
        return pluginVersionCode;
    }

    public void setPluginVersionCode(String pluginVersionCode) {
        this.pluginVersionCode = pluginVersionCode;
    }

    public String getSystemVersion() {
        return systemVersion;
    }

    public void setSystemVersion(String systemVersion) {
        this.systemVersion = systemVersion;
    }

    public Integer getXposedIsInstall() {
        return xposedIsInstall;
    }

    public void setXposedIsInstall(Integer xposedIsInstall) {
        this.xposedIsInstall = xposedIsInstall;
    }

    public Integer getWechatIsInstall() {
        return wechatIsInstall;
    }

    public void setWechatIsInstall(Integer wechatIsInstall) {
        this.wechatIsInstall = wechatIsInstall;
    }

    public String getWechatVersionCode() {
        return wechatVersionCode;
    }

    public void setWechatVersionCode(String wechatVersionCode) {
        this.wechatVersionCode = wechatVersionCode;
    }

    public Integer getWechatStatus() {
        return wechatStatus;
    }

    public void setWechatStatus(Integer wechatStatus) {
        this.wechatStatus = wechatStatus;
    }

    public Integer getSupportCurWechat() {
        return supportCurWechat;
    }

    public void setSupportCurWechat(Integer supportCurWechat) {
        this.supportCurWechat = supportCurWechat;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}

写nw_robotInfo的Mapper RobotinfoMapper

/**
 * Robotinfo采用mybatis的注解方式
 * Mybatis注解的方式比较简单,只要定义一个dao接口,然后sql语句通过注解写在接口方法上。最后给这个接口添加@Mapper注解或者在启动类上添加@MapperScan(“com.springboot.dao”)注解都行。
 */
@Mapper
public interface RobotinfoMapper {
    @Select("SELECT * FROM nw_robotInfo WHERE wechatId = #{wechatId}")
    Robotinfo findByWechatId(@Param("wechatId") String wechatId);

    @Select("SELECT * FROM nw_robotInfo ")
    List<Robotinfo> findAll();

   //错误提示:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'pluginisActvie' in 'class com.younghare.pojo.Robotinfo'
   //原因是需要getPluginisActvie,结果idea自动生成的方法是getPluginisactvie

    @Insert("insert into nw_robotInfo(pluginisActvie,pluginVersionCode,systemVersion,imei,xposedIsInstall" +
            ",wechatIsInstall,wechatVersionCode,wechatStatus,supportCurWechat,nickName" +
            ",wechatId) " +
            "values(#{pluginisActvie},#{pluginVersionCode},#{systemVersion},#{imei},#{xposedIsInstall}" +
            ",#{wechatIsInstall},#{wechatVersionCode},#{wechatStatus},#{supportCurWechat},#{nickName}" +
            ",#{wechatId})")
    int insertByRobotinfo(Robotinfo robotinfo);


    @Update("UPDATE nw_robotInfo SET pluginisActvie=#{pluginisActvie},pluginVersionCode=#{pluginVersionCode},systemVersion=#{systemVersion},imei=#{imei},xposedIsInstall=#{xposedIsInstall}" +
            ",wechatIsInstall=#{wechatIsInstall},wechatVersionCode=#{wechatVersionCode},wechatStatus=#{wechatStatus},supportCurWechat=#{supportCurWechat},nickName=#{nickName}" +
            " WHERE wechatId =#{wechatId}")
    void update(Robotinfo robotinfo);


    @Delete("DELETE FROM nw_robotInfo WHERE wechatId =#{wechatId}")
    void delete(String wechatId);




}

写测试用例进行测试




@SpringBootTest(classes= WechatTaskApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class RobotinfoMapperTest {
    @Autowired
    RobotinfoMapper robotinfoMapper;
    @Test
    public void findRobotInfoByWechatIdTest(){
        Robotinfo robotinfo = robotinfoMapper.findByWechatId("wxid_on8oksh88zo22");
        if (null != robotinfo){
            System.out.println("=========**"+robotinfo.getNickName());
        }else {
            System.out.println("没用找到记录");
        }

    }

    //private String wechatId_new = "wxid_on8oksh88zo33";

    public int InsertRobotInfo(String wechatId){
        Robotinfo robotinfo = new Robotinfo();
        robotinfo.setPluginVersionCode("1905071513");


        robotinfo.setImei(wechatId+"IMEI");
        robotinfo.setNickName(wechatId+"测试昵称");

        robotinfo.setXposedIsInstall(1);

        robotinfo.setWechatId(wechatId);
        int ret = robotinfoMapper.insertByRobotinfo(robotinfo);

        System.out.println("插入结果"+ ret);
        return ret;


    }

    @Test
    public void InsertRobotInfoTest(){
        String wechatId = "wxid_on8oksh88zo44";

        InsertRobotInfo("wxid_on8oksh88zo44");

        Robotinfo robotinfo = robotinfoMapper.findByWechatId(wechatId);

        if (null != robotinfo){
            System.out.println("=====robotinfo2 插入成功====**"+robotinfo.getNickName());
        }else {
            System.out.println("robotinfo2插入失败没用找到记录");
        }


    }

    /**
     * 插入多条记录用与当测试数据
     */

    @Test
    public void InsertRobotInfoTest2(){

        Robotinfo robotinfo = robotinfoMapper.findByWechatId("wxid_ceshi0019");
        if (robotinfo !=null){
            return;
        }

        for (int index =0;index<20;index++){
            String strIndex = String.format("%04d", index);  //数字不足4位前面补0
            String wechatId = "wxid_ceshi"+strIndex;
            InsertRobotInfo(wechatId);
        }

    }



    @Test
    public void updateRobotInfoTest(){
        String wechatId = "wxid_on8oksh88zo44";

        //先查找看记录是
        Robotinfo robotinfo = robotinfoMapper.findByWechatId(wechatId);
        if (null != robotinfo){

            robotinfo.setNickName(robotinfo.getNickName()+"update");
            robotinfoMapper.update(robotinfo);
        }

    }

    @Test
    public void delRobotInfoTest(){
        String wechatId = "wxid_on8oksh88zo55";

        //先查找看记录是
        Robotinfo robotinfo = robotinfoMapper.findByWechatId(wechatId);
        if (null == robotinfo){
            int ret =InsertRobotInfo(wechatId);

            Robotinfo robotinfo2 = robotinfoMapper.findByWechatId(wechatId);
            System.out.println("临时插入的wechatId:"+robotinfo2.getWechatId());
            Assert.assertEquals(wechatId, robotinfo2.getWechatId());
        }

        robotinfoMapper.delete(wechatId);
        Robotinfo robotinfo3 = robotinfoMapper.findByWechatId(wechatId);
        boolean isDel = robotinfo3!=null? false:true;
        System.out.println("删除后的情况记录存在?isDel :"+isDel);
        Assert.assertNull(robotinfo3);


    }
    @Test
    public void findAllTest(){
        int page =2;
        int pageSize =10;

        PageInfo<Robotinfo> info = findAll(page,pageSize);

        System.out.println("打印其实是用于断点查看:"+ JSONObject.toJSONString(info));




    }
    /**
     * 分页查询
     * @param page 当前页数
     * @param pageSize 每页个数
     * @return
     */
    public PageInfo<Robotinfo> findAll(int page ,int pageSize){
        PageHelper.startPage(page, pageSize);//改写语句实现分页查询
        List<Robotinfo> all = robotinfoMapper.findAll();
        PageInfo<Robotinfo> info = new PageInfo<>(all);
        return info;


    }



}


运行删除记录的测试用例结果


image.png

在application.properties添加mybatis的sql打印

#将mapper接口所在包的日志级别改成debug,可以在控制台打印sql
logging.level.com.younghare.mapper:debug
mybatis sql 日志打印

运行findAllTest单元测试

运行findAllTest单元测试

输出json字符串格式化

findAllTest输出

看pagehelper是如何查询数据库--分步骤查询

pagehelper其实是分次查询

编写RoboinfoService (注意注解@Service)

@Service
public class RobotinfoService {
    @Autowired
    private RobotinfoMapper robotinfoMapper;

    /**
     * 查询机器人信息
     * @param wechatId
     * @return
     */
    public Robotinfo findByWechatId(String wechatId){
        return robotinfoMapper.findByWechatId(wechatId);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    public PageInfo<Robotinfo> findAll(int page,int pageSize){
        PageHelper.startPage(page, pageSize);//改写语句实现分页查询
        List<Robotinfo> all = robotinfoMapper.findAll();
        PageInfo<Robotinfo> info = new PageInfo<>(all);
        return info;
    }

    /**
     * 插入
     * @param robotinfo
     * @return
     */
    public int insert(Robotinfo robotinfo ){
        return robotinfoMapper.insertByRobotinfo(robotinfo);
    }

    /**
     * 更新记录
     * @param robotinfo
     */
    public void update(Robotinfo robotinfo){
        robotinfoMapper.update(robotinfo);
    }

    /**
     * 插入或更新记录
     * @param robotinfo
     */
    public void insertOrUpdate(Robotinfo robotinfo){
        Robotinfo robotinfo_tmp = robotinfoMapper.findByWechatId(robotinfo.getWechatId());
        if (null == robotinfo_tmp){
            robotinfoMapper.insertByRobotinfo(robotinfo);
        }else {
            robotinfoMapper.update(robotinfo);
        }

    }

    /**
     * 删除记录
     * @param wechatId
     */
    public void delete(String wechatId){
        robotinfoMapper.delete(wechatId);
    }





}

RobotinfoController


@RestController
@RequestMapping("wechatTask")
public class RobotinfoController {
    @Autowired
    private RobotinfoService robotinfoService;

    @RequestMapping("/robot/save")
    public IJSONResult saveRobotInfo(String wechatId,String imei,String robotJson){

        Robotinfo robotinfo = null;
        try {
            RobotApp robotApp =JSONObject.parseObject(robotJson,new TypeReference<RobotApp>(){});
            //robotinfo = JSONObject.parseObject(robotJson,new TypeReference<Robotinfo>(){});
            if (null != robotApp){
                robotinfo = RobotApp2Robotinfo(wechatId,robotApp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (null == robotinfo){
            return IJSONResult.errorMsg("robotJson非法");
        }

        robotinfoService.insertOrUpdate(robotinfo);
        return IJSONResult.ok("保存成功");

    }



    @RequestMapping("/robot/delete")
    public IJSONResult deleteUser(String wechatId) {
        if (null ==robotinfoService.findByWechatId(wechatId)){
            return IJSONResult.errorMsg("该robot不存在!");
        }

        robotinfoService.delete(wechatId);
        return IJSONResult.ok("删除成功");
    }
    @RequestMapping("/robot/queryByWechatId")
    public IJSONResult queryByWechatId(String wechatId) {

        return IJSONResult.ok(robotinfoService.findByWechatId(wechatId));
    }


    @RequestMapping("/robot/queryPaged")  //查询分页
    public IJSONResult queryPaged(Integer page,Integer pageSize) {

        if (page == null) {
            page = 1;
        }
        if (pageSize == null){
            pageSize =10;
        }

        if (pageSize >50){
            return IJSONResult.errorMsg("每次请求记录数不允许超过50条记录!");
        }
        if (pageSize<1){
            return IJSONResult.errorMsg("每页请求记录数不合法!");
        }

        PageInfo<Robotinfo> pageInfo = robotinfoService.findAll(page,pageSize);

        return IJSONResult.ok(pageInfo);
    }


    /**
     * App发送过来json对象转化为数据库对应pojo对象
     * @param robotApp
     * @return
     */
    private Robotinfo RobotApp2Robotinfo(String wechatId,RobotApp robotApp){
        Robotinfo robotinfo = new Robotinfo();
        robotinfo.setPluginisActvie(robotApp.isPluginisActive()? 1:0);
        robotinfo.setPluginVersionCode(robotApp.getPluginVersionCode());

        robotinfo.setImei(robotApp.getImei());
        robotinfo.setXposedIsInstall(robotApp.isWechatIsInstall()? 1:0);
        robotinfo.setWechatIsInstall(robotApp.isXposedIsInstall()? 1:0);
        robotinfo.setWechatStatus(robotApp.getWechatStatus());
        robotinfo.setSupportCurWechat(robotApp.isSupportCurWechat()? 1:0);
        robotinfo.setNickName(robotApp.getNickName());

        robotinfo.setWechatId(wechatId);

        return robotinfo;
    }


}

运行效果

Mybatis注解方式insert时获取返回的自增主键

    //Mybatis注解方式insert时获取返回的自增主键
    //@SelectKey(statement="select last_insert_id()",before=false,keyProperty="snsItemInfo.db_id",resultType=Integer.class,keyColumn="id")
    //或
    @Options(useGeneratedKeys = true, keyProperty = "snsItemInfo.db_id")

    int insertBySnsItemInfo(SnsItemInfo snsItemInfo,String updateWechatId);

关于rabbitmq的文章收录

Spring Boot中使用MyBatis注解配置详解-今日头条
Spring Boot中使用MyBatis注解配置详解--推荐itcast
mybatis使用注解,多参数增删改查-含测试用例
Springboot集成Mybatis+PageHelper
SpringBoot-15-之整合MyBatis-注解篇+分页
如果让你写一个MyBatis分页插件,你准备怎么实现

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容