Kotlin服务端Spring Boot系列干货教程3 - 代码编写

2017-12-14
483
0
0

温馨提示:本文最后更新于 2023-07-07,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Dao接口、实体类、sql都已经生成好了,我们只需要编写Service、Controller即可!

timg (1).jpg

1. 编写Controller

    @RequestMapping("/")
    fun selectMessageList(pageNum: Int?,type: Int?,model: ModelMap): String {
        var pageNum = pageNum?:1
        var type = type?:1
        var pageSize = 10
        PageHelper.startPage<Any>(pageNum, pageSize)
        var para = HashMap<Any,Any>()
        if (type==1){
            para.put("sorts","time")
        }else{
            para.put("sorts","praise")
        }
        var messageList = messageMapper!!.selectListByMap(para)
        var pageInfo = PageInfo(messageList)
        model.put("type",type)
        model.put("page",pageInfo)
        return "/msg"
    }


    @RequestMapping("/addMessage.json")
    @ResponseBody
    fun addMessage(nick_name: String, content: String, face: String):R {
        return messageService!!.addMessage(nick_name, content, face)
    }

2. 编写Service代码

    @Autowired
    private val messageMapper: MessageMapper? = null

    @Transactional
    fun addMessage(nick_name: String, content: String, face: String): R{
        var msg = Message()
        msg.nick_name = nick_name
        msg.content = content
        msg.face = face
        msg.add_time = Date()
        messageMapper!!.insertSelective(msg)
        return R(1,"")
    }

这里只截取了部分代码做演示,完成源码已上传

效果图

QQ截图20171214202500.png

详细的日志

QQ截图20171214202916.png


GitHub源码地址:https://github.com/kecikeci/springboot4kt

Gitee码云地址:https://gitee.com/kecikeci/springboot4kt


评论

Polar.
最重要的,是拥有跟随内心与直觉的勇气

文章目录