初步学习springboot完成对数据库的增查

上学期学的jdbc+servlet+jsp发现逐渐跟不上脚步,于是去学习springboot
入门简单做了对数据库的增加和查询功能

pojo层

public class Login {
    private String name;
    private String password;
    private String role;

    public Login() {
    }

    public Login(String name, String password, String role) {
        this.name = name;
        this.password = password;
        this.role = role;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @Override
    public String toString() {
        return "login{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", role='" + role + '\'' +
                '}';
    }
}

mapper层

@org.apache.ibatis.annotations.Mapper
public interface Mapper {

    @Insert("insert into login values (#{name},#{password},#{role})")
    void add(Login login);

    @Select("select * from login")
    List<Login> getAll();



}

service层

@Service
public class service {
    @Autowired
    private Mapper map;
    public void add(Login login){
        map.add(login);
    }

    public List<Login> selectAll(){
        List<Login> mess=map.getAll();
        return mess;
    }

}

controller层

@Controller
public class controller {
    @Autowired
    private service service;
    @GetMapping("/add")
    public void setService(HttpServletRequest req, HttpServletResponse resp)throws Exception {
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String role = req.getParameter("role");

        Login login = new Login(name, password, role);
        service.add(login);



    }

    @PostMapping("/add")
    public void post(HttpServletRequest req, HttpServletResponse resp)throws Exception {
        this.setService(req, resp);
    }

    @GetMapping("/select")
    @ResponseBody
    public List<Login> select(HttpServletRequest req, HttpServletResponse resp)throws Exception {
        List<Login> mess=service.selectAll();
        return mess;


    }

    @PostMapping("/select")
    public void postSelect(HttpServletRequest req, HttpServletResponse resp)throws Exception {
        this.setService(req, resp);
    }

}

前端就用html,查询在html显示数据是通过后端传输Json数据从而显示出来,用之前jsp的方法就完全不行,在了解后才发现

posted @ 2025-03-30 22:33  茆伟昊  阅读(13)  评论(0)    收藏  举报