• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

前后端分离若依框架手搓基本功能

武飞扬头像
我不是码农admin
帮助1

成果如下,非自动生成代码,实现手搓的增删改查,导出等自定义功能。

学新通
  1. 建库建表

CREATE TABLE `test` (

`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',

`name` varchar(255) DEFAULT NULL COMMENT '姓名',

`age` varchar(255) DEFAULT NULL COMMENT '年龄',

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;

  1. ruoyi-admin模块下写后端代码

一。Controller层注意事项

  1. @RestController

  1. @RequestMapping("/test") 第一层接口路径

3.查询所有,返回list集合,传domain中的对象,直接调用封装的分页方法,并将service层由数据库查询到的list集合数据封装,并传给getDataTable方法(这个是BaseController中的方法 getDataTable响应请求分页数据)返回给前端。

  1. 通过id查询一个: get请求 @GetMapping(value = "/tests/{id}"),返回AjaxResult,传参id

  1. 新增/修改: 新增Post请求,修改Put请求,都是传json类型的对象@RequestBody Test test,返回AjaxResult

将数据库查到的数据传给toAjax方法中

  1. 删除/批量删除 : del请求 @DeleteMapping("/tests/{ids}"),返回AjaxResult ,传参 long类型的数组,返回AjaxResult.

代码如下

package com.ruoyi.test.controller;

import com.ruoyi.common.core.controller.BaseController;

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

import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.test.domain.Test;

import com.ruoyi.test.service.TestsService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

@RequestMapping("/test")

public class TestsController extends BaseController {

@Autowired

private TestsService testService;

/**

* 查询测试接口表 分页

*/

@GetMapping("/tests/list")

public TableDataInfo list(Test test){

//封装的分页功能

startPage();

//返回个list集合

List<Test> testList = testService.selectAll(test);

//TableDataInfo dataTable = getDataTable(testList);

/**

* 这个是BaseController中的方法 getDataTable响应请求分页数据

* 把查询到的list集合数据,放到这个方法中

*/

return getDataTable(testList);

}

/**

* 查询一个测试接口

* 通过id查询一条对象

*/

@GetMapping(value = "/tests/{id}")

public AjaxResult getInfo(@PathVariable("id") Long id){

return success(testService.selectOne(id));

}

/**

* 新增测试接口表

*/

@PostMapping(value = "/tests")

public AjaxResult insert(@RequestBody Test test){

return toAjax(testService.insert(test));

}

/**

* 修改测试接口表

*/

@PutMapping(value = "/tests")

public AjaxResult update(@RequestBody Test test){

return toAjax(testService.update(test));

}

/**

* 批量删除测试接口表

*/

@DeleteMapping("/tests/{ids}")

public AjaxResult removeByIds(@PathVariable Long[] ids){

return toAjax(testService.deleteTestByIds(ids));

}

}

二。Service注意事项

serviceimpl要加@Service注解

(1)查询所有,返回list集合,传参为对象

(2)查询单个,返回对象,传参为id

(3) 新增,修改都返回int, 传参为对象

(4)删除 , 返回int 类型,传参为Long id或者Long[] ids.

service代码如下

package com.ruoyi.test.service.impl;

import com.ruoyi.test.domain.Test;

import com.ruoyi.test.mapper.TestsMapper;

import com.ruoyi.test.service.TestsService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class TestsServiceImpl implements TestsService {

@Autowired

private TestsMapper testMapper;

@Override

public List<Test> selectAll(Test test) {

return testMapper.selectAll(test);

}

@Override

public Test selectOne(Long id){

return testMapper.selectTestById(id);

}

@Override

public int insert(Test test) {

return testMapper.insert(test);

}

@Override

public int update(Test test) {

return testMapper.update(test);

}

@Override

public int deleteTestByIds(Long[] ids) {

return testMapper.deleteByIds(ids);

}

@Override

public int deleteTestById(Long id) {

return testMapper.deleteTestById(id);

}

}

三。mapper层注意事项

  1. 在启动类或者配置yml加扫描。MapperScan

# MyBatis配置

mybatis:

# 搜索指定包别名

typeAliasesPackage: com.ruoyi.**.domain

# 配置mapper的扫描,找到所有的mapper.xml映射文件

mapperLocations: classpath*:mapper/**/*Mapper.xml

# 加载全局的配置文件

configLocation: classpath:mybatis/mybatis-config.xml

  1. Testmapper.xml

要跟mapper的包名,方法名,返回类型对应上

代码如下

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"" target="_blank">http://mybatis.org/dtd/mybatis-3-mapper.dtd"" target="_blank">>

<mapper namespace="com.ruoyi.test.mapper.TestsMapper">

<resultMap type="Test" id="TestResult">

<result property="id" column="id" />

<result property="name" column="name" />

<result property="age" column="age" />

</resultMap>

<sql id="selectTestVo">

select id, name,age from test

</sql>

<select id="selectAll" parameterType="Test" resultMap="TestResult">

<include refid="selectTestVo"/>

<where>

<if test="id != null "> and id = #{id}</if>

<if test="name != null and name != ''"> and name = #{name}</if>

<if test="age != null and age != ''"> and age = #{age}</if>

</where>

</select>

<select id="selectTestById" parameterType="Long" resultMap="TestResult">

<include refid="selectTestVo"/>

where id = #{id}

</select>

<insert id="insert" parameterType="Test" useGeneratedKeys="true" keyProperty="id">

insert into test

<trim prefix="(" suffix=")" suffixOverrides=",">

<if test="id != null">id,</if>

<if test="name != null">name,</if>

<if test="age !=null"> age,</if>

</trim>

<trim prefix="values (" suffix=")" suffixOverrides=",">

<if test="id !=null">#{id},</if>

<if test="name !=null">#{name},</if>

<if test="age !=null">#{age},</if>

</trim>

</insert>

<update id="update" parameterType="Test">

update test

<trim prefix="SET" suffixOverrides=",">

<if test="id != null">id = #{id},</if>

<if test="name != null">name = #{name},</if>

<if test="age !=null"> age= #{age}</if>

</trim>

where id = #{id}

</update>

<delete id="deleteTestById" parameterType="Long">

delete from test where id = #{id}

</delete>

<delete id="deleteByIds" parameterType="String">

delete from test where id in

<foreach item="id" collection="array" open="(" separator="," close=")">

#{id}

</foreach>

</delete>

</mapper>

注意事项;新增的mapper.xml

学新通

前端:

  1. 在api-test-xx.js中写接口,api路径不对,报module build failed错误

学新通
  1. 在views-test-xx中写vue组件,注意包名要对上,否则控制台会报该错误。(如何判断地址是否正确,在菜单管理中查询)

学新通
  1. 这个路径对应view的路径

学新通

4.菜单sql运行一下

这样就可以不用自动生成代码,也能添加自己的业务需求了

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfhkag
系列文章
更多 icon
同类精品
更多 icon
继续加载