介绍 | RuoYi
一、准备工作
1.IDEA准备
将下载好的若依项目导入IDEA,导入后配置MAVEN环境,等待下载完成。重点关注ruoyi-admin和ruoyi-system两个文件夹,前者放controller,后者放实体类、mapper、service
2.打开VS Code前端编写
①打开文件夹选择若依前端项目(ruoyi-ui)文件夹
②信任此作者进入
③重点关心src文件夹下的api和views两个文件夹
3.右键以管理员身份启动redis-server服务
上图表示成功启动
4.IDEA启动
5.VS Code启动
①新建终端
②下方黑窗口输入口令,只要没有error就算启动成功,报错后可尝试重启电脑,理论上环境变量自动生成
npm install --registry=https://registry.npmmirror.com
③启动服务
npm run dev
自动弹出若依管理系统表示项目已经启动成功。
二、后台代码编写
1.在ruoyi-system中创建自己的包basic
①编写实体类、mapper以及service层代码
实体类
public class Channel extends BaseEntity {
private String id; //32位主键ID
private String channelName; //栏目名
private Integer isShow; //是否显示
//private Date updateTime; //更新时间,父类中有子类删掉即可
//手动生成getter和setter
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getChannelName() {
return channelName;
}
public void setChannelName(String channelName) {
this.channelName = channelName;
}
public Integer getIsShow() {
return isShow;
}
public void setIsShow(Integer isShow) {
this.isShow = isShow;
}
}
mapper
public interface ChannelMapper {
/**
* 添加栏目
* @param channel
* @return
*/
public int insertChannel(Channel channel);
/**
* 根据ID删除栏目
* @param id
* @return
*/
public int deleteChannelById(String id);
/**
* 修改栏目
* @param channel
* @return
*/
public int updateChannel(Channel channel);
/**
* 根据ID查询栏目
* @param id
* @return
*/
public Channel selectChannelById(String id);
/**
* 根据条件,查询栏目列表
* @param channel
* @return
*/
public List<Channel> selectChannelList(Channel channel);
}
mapper.xml(basic和system不能同级可以选择打开文件根目录,在mapper文件夹中新建basic文件夹,回到项目刷新即可)
<?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>
where id=#{id}
</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>
service层
public interface IChannelService {
/**
* 添加栏目
* @param channel
* @return
*/
public int insertChannel(Channel channel);
/**
* 根据ID删除栏目
* @param id
* @return
*/
public int deleteChannelById(String id);
/**
* 修改栏目
* @param channel
* @return
*/
public int updateChannel(Channel channel);
/**
* 根据ID查询栏目
* @param id
* @return
*/
public Channel selectChannelById(String id);
/**
* 根据条件,查询栏目列表
* @param channel
* @return
*/
public List<Channel> selectChannelList(Channel channel);
}
service层实现类文章来源:https://www.toymoban.com/news/detail-402530.html
@Service
public class ChannelServiceImpl implements IChannelService {
@Autowired(required = false)
private ChannelMapper channelMapper;
@Override
public int insertChannel(Channel channel) {
//id不是自增,需要生成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);
}
}
2.在ruoyi-admin中创建自己的包basic
controller文章来源地址https://www.toymoban.com/news/detail-402530.html
@RestController
@RequestMapping("/basic/channel")
public class ChannelController extends BaseController {
@Autowired
private IChannelService channelService;
@PostMapping("/add")
public AjaxResult add(Channel channel){
int rows = channelService.insertChannel(channel);
return toAjax(rows);//如果rows > 0就是成功,否则失败
}
@DeleteMapping("/remove")
public AjaxResult remove(String id){
int rows = channelService.deleteChannelById(id);
return toAjax(rows);//如果rows > 0就是成功,否则失败
}
@PutMapping("/edit")
public AjaxResult edit(Channel channel){
int rows = channelService.updateChannel(channel);
return toAjax(rows);//如果rows > 0就是成功,否则失败
}
@GetMapping("/get_info")
public AjaxResult grtInfo(String id){
return AjaxResult.success(channelService.selectChannelById(id));
// Channel channel = channelService.selectChannelById(id);
// return AjaxResult.success(channel);
}
@GetMapping("/list")
public TableDataInfo list(Channel channel){
startPage();//分页函数,注意:该函数必须和select语句写在一起
List<Channel> channels = channelService.selectChannelList(channel);
return getDataTable(channels);
}
}
到了这里,关于【若依】框架:第01讲前后端分离项目的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!