目录
添加数据库
添加 CSMD 对应文件
Controller
在软件包ruoyi-admin/src/main/java/com/ruoyi/web/controller/system下新建Java类HelloController.java
Service
在软件包ruoyi-system/src/main/java/com/ruoyi/system/service下新建Java接口类 UserService.java
在软件包ruoyi-system/src/main/java/com/ruoyi/system/service/impl下新建Java类UserServiceimpl.java
Mapper
在软件包ruoyi-system/src/main/java/com/ruoyi/system/mapper下新建Java接口类UserMapper.java
在软件包ruoyi-system/src/main/resources/mapper/system下新建xml文件UserMapper.xml
Domain
在软件包ruoyi-system/src/main/java/com/ruoyi/system/domain下新建Java类User.java
添加 CSMD 对应文件代码
Controller
HelloController.java
package com.ruoyi.web.controller.system;
import com.ruoyi.system.domain.User;
import com.ruoyi.system.service.impl.UserServiceimpl;
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 HelloController {
@Autowired
private UserServiceimpl userServiceimpl;
@GetMapping("/hello")
public List<User> hello()
{
return userServiceimpl.selectAllUser();
}
}
Service
userservice.java
package com.ruoyi.system.service;
public interface UserService {
}
UserServiceimpl.java
package com.ruoyi.system.service.impl;
import com.ruoyi.system.domain.User;
import com.ruoyi.system.mapper.UserMapper;
import com.ruoyi.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceimpl implements UserService {
@Autowired
private UserMapper userMapper;
public List<User> selectAllUser()
{
return userMapper.selectAllUser();
}
}
Mapper
UserMapper.java
package com.ruoyi.system.mapper;
import com.ruoyi.system.domain.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
public List<User> selectAllUser();
}
UserMapper.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.ruoyi.system.mapper.UserMapper">
<resultMap id="UserResult" type="User">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<result property="createTime" column="create_time" />
</resultMap>
<select id="selectAllUser" resultMap="UserResult">
select * from myuser
</select>
</mapper>
Domain
User.java
package com.ruoyi.system.domain;
public class User {
public int id;
private String name;
private int age;
public int sex;
public String createTime;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}
开放权限
使得不登入ruoyi程序也能访问此数据库表格,进行CURD操作
找到软件包ruoyi-framework/src/main/java/com/ruoyi/framework/config下java文件SecurityConfig.java
在其115行附近加入代码,允许匿名用户访问/hello这个路径
.antMatchers("/hello").anonymous()
使用Postman测试接口
打开redis-server,启动程序