创建新表
在ruoyi的数据库里新建一个user表
在表中添加几条数据
创建CSMD相应包和相关代码
Controller
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
package com.ruoyi.system.service;
public interface UserService {
}
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
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();
}
<?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
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;
}
}
权限开放
介于若依的SpringSecurity,在访问时如果需要屏蔽,我们需要添加在controller的方法或类上添加@Anonymous注解进行屏蔽
在SecurityConfig文件的115行添加以下代码
.antMatchers("/hello").anonymous()
Postman测试
启动redis-server和ruoyi程序
没毛病,成功~
遇到问题
总是拒绝连接本地端口
检查了一下,发现之前在ruoyi里改过端口,改过来就完事了