un7.10:在IDEA中使用若依框架实现增删改查功能。

7 篇文章 5 订阅

在java的世界中,有很多框架提供我们使用,今天我们就来讲一讲若依框架。

若依框架是一款非常便捷的框架,他相对于spring boot框架而言,显得更为简单,更为便捷,比如:我们在若依框架中再也不用写分页了,它会在AjaxResult中自动生成,而我们只需要继承就可以了,接下来我就来为大家演示一遍如何使用若依框架写出一套完整的增删改查,实现增删改查功能。

所需软件:IDEA

一、我们需要弄明白若依框架的基本组成。

1、需要注意的是,我们只需要用到两个包,一个是ruoyi-admin文件,这个包里边是专门用来写控制器controller的,也会有一些资源,比如application.xml。

2、还有一个是ruoyi-system文件,这个里边是用来写实体类、dao、service层的。

 二、我们需要在com.ruoyi下创建一个包,我的叫basic,然后在basic中创建三个包,我的叫domain、mapper、service。

1、在domain中写好id,channelName、isShow、upadateTime,代码如下:

package com.ruoyi.basic.domain;

import com.ruoyi.common.core.domain.BaseEntity;

public class Channel extends BaseEntity {
    private String id;//32位主键id
    private String channelName;//栏目名称
    private Integer isShow;//是否显示

    public String getId() {
        return id;
    }

    public String getChannelName() {
        return channelName;
    }

    public Integer getIsShow() {
        return isShow;
    }

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

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }

    public void setIsShow(Integer isShow) {
        this.isShow = isShow;
    }
}

2、在接口中声明方法,代码如下:

package com.ruoyi.basic.mapper;

import com.ruoyi.basic.domain.Channel;

import java.util.List;

public interface ChannelMapper {
    /**
     * 添加栏目
     * @param channel
     * @return
     */
    public int insertChannel(Channel channel);
    /**
     * 根据id删除栏目
     */
    public int deleteChannelById(String id);
    /**
     * 修改栏目
     */
    public int updateChannel(Channel channel);
    /**
     * 根据id查看栏目
     */
    public Channel selectChannelById(String id);
    /**
     * 查看栏目列表,根据条件
     */
    public List<Channel> selectChannelList(Channel channel);
}

3、在xml文件中写sql语句,这个在上一篇博客中有写到过,可以在上一篇博客中copy,也可以直接从下面的代码copy,代码如下:

<?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.ruoyi.basic.mapper.ChannelMapper">
    <resultMap id="ChannelResult" type="Channel">
        <result property="id" column="id"></result>
        <result property="channelName" column="channel_name"></result>
        <result property="isShow" column="is_show"></result>
        <result property="updateTime" column="update_time"></result>
    </resultMap>
    <sql id="selectChannelVO">
        select id,channel_name,is_show,update_time from cms_channel
    </sql>
    <select id="selectChannelById" parameterType="String" resultMap="ChannelResult">
        <include refid="selectChannelVO"></include>
    </select>
    <select id="selectChannelList" parameterType="Channel" resultMap="ChannelResult">
        <include refid="selectChannelVO"></include>
    <where>
    <if test="channelName != null and channelName != ''"> and channel_name like concat (#{channelName}, '%')</if>
    <if test="isShow != null">and is_show =#{isShow}</if>
    </where>
    </select>

    <insert id="insertChannel" parameterType="Channel">
        insert into cms_channel(id,channel_name,is_show,update_time)
        values
               (#{id},#{channelName},#{isShow},#{updateTime})
    </insert>

    <delete id="deleteChannelById" parameterType="String">
        delete from cms_channel where id=#{id}
    </delete>

    <update id="updateChannel" parameterType="Channel">
        update cms_channel
        set channel_name=#{channelName},is_show=#{isShow},update_time=#{updateTime}
        where id=#{id}
    </update>
</mapper>

4、在service接口中声明方法,和mapper接口中的差不多,代码如下:

package com.ruoyi.basic.service;

import com.ruoyi.basic.domain.Channel;

import java.util.List;

public interface IChannelService {
    /**
     * 添加栏目
     * @param channel
     * @return
     */
    public int insertChannel(Channel channel);
    /**
     * 根据id删除栏目
     */
    public int deleteChannelById(String id);
    /**
     * 修改栏目
     */
    public int updateChannel(Channel channel);
    /**
     * 根据id查看栏目
     */
    public Channel selectChannelById(String id);
    /**
     * 查看栏目列表,根据条件
     */
    public List<Channel> selectChannelList(Channel channel);
}

5、在serviceImpl中实现,需要注意的是,添加的时候需要生成32位主键id,UUID默认生成的是36位,每四位位一组,每一组只见用“-”链接,我们需要用UUID里边的randomUUID方法,调用toString,然后用replace方法将“-”改为空,这样就成为32位了,代码如下:

package com.ruoyi.basic.service.impl;

import com.ruoyi.basic.domain.Channel;
import com.ruoyi.basic.mapper.ChannelMapper;
import com.ruoyi.basic.service.IChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ChannelServiceImpl implements IChannelService {

    @Autowired(required = false)
    private ChannelMapper channelMapper;
    @Override
    public int insertChannel(Channel channel) {
        //生成32位主键id
        String id = UUID.randomUUID().toString().replace("-","");
        channel.setId(id);
        return channelMapper.insertChannel(channel);
    }

    @Override
    public int deleteChannelById(String id) {
        return channelMapper.deleteChannelById(id);
    }

    @Override
    public int updateChannel(Channel channel) {
        return channelMapper.updateChannel(channel);
    }

    @Override
    public Channel selectChannelById(String id) {
        return channelMapper.selectChannelById(id);
    }

    @Override
    public List<Channel> selectChannelList(Channel channel) {
        return channelMapper.selectChannelList(channel);
    }
}

如此,我们ruoyi-system中的任务就完成了。

三、在ruoyi-admin中创建一个basic,再再basic中创建一个controller,在controller中新建一个ChannelController。

1、先注入接口,实现接口,再用@Autowired(required = false)容错,代码如下:

@Autowired(required = false)
    private IChannelService channelService;

2、我们先完成增删改,和spring boot框架的增删改差不多,只不过在ruoyi框架中,将分页器封装到了AjaxResult中了,所以需要返回的是AjaxResult而不是HttPResult,而且在AjaxResult中,自动判断,所以我们直接返回,把值放入就可以,所以若依的框架目前非常受欢迎。代码如下:

@PostMapping("/add")
    public AjaxResult add(Channel channel){
        int rows=channelService.insertChannel(channel);
        return toAjax(rows);//如果rows>0,成功
    }

    @PostMapping("/remove")
    public AjaxResult remove(String id){
        int rows=channelService.deleteChannelById(id);
        return toAjax(rows);//如果rows>0,成功
    }

    @PostMapping("/edit")
    public AjaxResult edit(Channel channel){
        int rows=channelService.updateChannel(channel);
        return toAjax(rows);//如果rows>0,成功
    }

3、查询一个id,查询一个id的时候,有两种方法进行查询。

a.调用service接口中的查询方法,将id放入,返回一个查询成功的值,代码如下:

         /*
        Channel =channel=channelService.selectChannelById(id);
        return AjaxResult.success(channel);
         */

b.直接返回一个成功的值,将查询的过程放入,代码如下:

return AjaxResult.success(channelService.selectChannelById(id));//如果rows>0,成功

查询一个的代码如下:

@GetMapping("/get_info")
    public AjaxResult get_info(String id){
        /*
        Channel =channel=channelService.selectChannelById(id);
        return AjaxResult.success(channel);
         */
        return AjaxResult.success(channelService.selectChannelById(id));//如果rows>0,成功
    }

4、和查询整个列表,需要注意的是,查询整个列表的时候有分页,所以需要有一个分页函数startPage();分页函数必须和select写在一起,否则的话无法查询成功,代码如下:

@GetMapping ("/list")
    public TableDataInfo list(Channel channel){
        startPage();//分页函数,注意:该函数必须和select查询写在一起,下面一定是查询,否则分页器无效
        List<Channel> channels = channelService.selectChannelList(channel);
        return getDataTable(channels);//如果rows>0,成功
    }

四、启动服务器,启动成功后页面如下:

 如此,我们便实现了如何用若依框架写出一套完整的增删改查了,分享到这里就结束了,希望能够帮到大家。

最后附上controller完整代码,完整代码如下:

package com.ruoyi.basic.controller;

import com.ruoyi.basic.domain.Channel;
import com.ruoyi.basic.service.IChannelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/basic/channel")
public class ChannelController extends BaseController {
    @Autowired(required = false)
    private IChannelService channelService;

    @PostMapping("/add")
    public AjaxResult add(Channel channel){
        int rows=channelService.insertChannel(channel);
        return toAjax(rows);//如果rows>0,成功
    }

    @PostMapping("/remove")
    public AjaxResult remove(String id){
        int rows=channelService.deleteChannelById(id);
        return toAjax(rows);//如果rows>0,成功
    }

    @PostMapping("/edit")
    public AjaxResult edit(Channel channel){
        int rows=channelService.updateChannel(channel);
        return toAjax(rows);//如果rows>0,成功
    }

    @GetMapping("/get_info")
    public AjaxResult get_info(String id){
        //Channel =channel=channelService.selectChannelById(id);return AjaxResult.success(channel);
        return AjaxResult.success(channelService.selectChannelById(id));//如果rows>0,成功
    }

    @GetMapping ("/list")
    public TableDataInfo list(Channel channel){
        startPage();//分页函数,注意:该函数必须和select语句写在一起,下面一定是查询
        List<Channel> channels = channelService.selectChannelList(channel);
        return getDataTable(channels);//如果rows>0,成功
    }
}

  • 4
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小格子衬衫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值