数据库DateTime类型的字段select查询时封装Java对象的Long类型属性中

一、代码链接

薛雄辉/springbootdemohttps://gitee.com/xuexionghui/springbootdemo/tree/master/mybatis/mybatis__type_handler

二、需求

数据库DateTime类型的字段select查询时封装Java对象的Long类型属性中

三、数据库

/*
 Navicat Premium Data Transfer

 Source Server         : 121.37.245.47
 Source Server Type    : MySQL
 Source Server Version : 50741
 Source Host           : 121.37.245.47:3306
 Source Schema         : test

 Target Server Type    : MySQL
 Target Server Version : 50741
 File Encoding         : 65001

 Date: 14/03/2023 20:54:53
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for dog
-- ----------------------------
DROP TABLE IF EXISTS `dog`;
CREATE TABLE `dog`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `birthday` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of dog
-- ----------------------------
INSERT INTO `dog` VALUES (1, '阿旺', '2023-03-14 20:40:34');
INSERT INTO `dog` VALUES (2, '阿旺', '2023-03-14 20:52:34');

SET FOREIGN_KEY_CHECKS = 1;

三、controller

package com.daxiong.controller;


import com.daxiong.entity.Dog;
import com.daxiong.service.DogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DogController {

    @Autowired
    private DogService dogService;

    @GetMapping("/addDog")
    public   String   addDog(){
        dogService.addDog();
        return  "success";
    }

    @GetMapping("/listDog")
    public List<Dog>  listDog(){
        return   dogService.listDog();
    }
}

四、service

package com.daxiong.service;

import com.daxiong.entity.Dog;

import java.util.List;

public interface DogService {
    void addDog();

    List<Dog> listDog();
}
package com.daxiong.service.impl;

import com.daxiong.dao.DogDao;
import com.daxiong.entity.Dog;
import com.daxiong.service.DogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.ServiceMode;
import java.util.Date;
import java.util.List;

@Service
public class DogServiceImpl implements DogService {

    @Autowired
    private DogDao dogDao;
    @Override
    public void addDog() {
        Dog dog = new Dog();
        dog.setBirthday(new Date());
        dog.setName("阿旺");
        dogDao.addDog(dog);
    }


    @Override
    public List<Dog> listDog() {
        return dogDao.listDog();
    }
}

五、dao

package com.daxiong.dao;

import com.daxiong.entity.Dog;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface DogDao {
    void addDog(Dog dog);

    List<Dog> listDog();
}

六、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.daxiong.dao.DogDao">

    <insert id="addDog">
        insert into  dog(name,birthday) values (#{name},#{birthday})
    </insert>

    <select id="listDog" resultType="com.daxiong.entity.Dog">
         select id,name ,unix_timestamp(birthday) as birthdayLong from dog
    </select>
</mapper>

unix_timestamp(数据库字段)将datetime类型的数据库字段转为Java对象的Long类型属性。

七、entity

package com.daxiong.entity;

import java.util.Date;

public class Dog {

    private  Integer id;
    private  String  name;
    private Date  birthday;
    private  Long  birthdayLong;

    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 Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Long getBirthdayLong() {
        return birthdayLong;
    }

    public void setBirthdayLong(Long birthdayLong) {
        this.birthdayLong = birthdayLong;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值