目录
1.在若依数据库中添加测试表
2.在若依系统添加实例代码
在ruoyi-system\src\main\java\com\ruoyi\system添加实例代码
在包domain下添加User类
在包mapper中添加UserMapper接口
在service下添加UserService接口
在包impl中添加UserServiceimpl类
、
在ruoyi-system\src\main\resources\mapper\system这个路径下添加UserMapper.xml
ruoyi-system\src\main\java\com\ruoyi\system在此路径下添加HelloController
3.具体代码
3.1User
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;
}
}
3.2UserMapper接口
package com.ruoyi.system.mapper;
import com.ruoyi.system.domain.User;
import java.util.List;
public interface UserMapper {
public List<User> selectAllUser();
}
3.3UserService
package com.ruoyi.system.service;
public interface UserService {
}
3.4UserServiceimpl
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();
}
}
3.5UserMapper.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>
3.6HelloController
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();
}
}
4.开放接口
添加102行代码
5.连接数据库
输入自己的用户名及密码,后点击确认
6.测试接口
启动redis 并启动ruoyi后端
获得对象,则表示成功