插入,查询显示到页面

在这里插入图片描述
PersonController

package com.example.demo.controller;

import com.example.demo.entity.Person;
import com.example.demo.service.PersonService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author :努力吧小朱.
 * @version : Java1.8
 * @date :Created in 15:14 2019/12/12
 * @ Description:${description}
 * @ Modified By:
 */
@RestController
@RequestMapping("person")
public class PersonController {
    @Resource
    private PersonService personService;
    @RequestMapping("insert")
    public String insert(HttpServletRequest request){
                Person person = new Person();
                person.setName(request.getParameter("name"));
                person.setAge(Integer.valueOf(request.getParameter("age")));
                boolean insert = personService.insert(person);
                if (insert){
                    return "成功";
                }else {
                    return "失败";
        }
    }
    @RequestMapping("findAll")
    public List<Person> findAll(){
        return personService.findAll();
    }
}

PersonService

package com.example.demo.service;

import com.example.demo.dao.PersonDao;
import com.example.demo.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :努力吧小朱.
 * @version : Java1.8
 * @date :Created in 15:11 2019/12/12
 * @ Description:${description}
 * @ Modified By:
 */
@Service
public class PersonService {
    @Resource
    private PersonDao personDao;

    public boolean insert(Person person){
        return personDao.insertPerson(person);
    }

    public List<Person> findAll(){
        return personDao.findAll();  // List<Person>
    }
}

PersonDao

package com.example.demo.dao;

import com.example.demo.entity.Person;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * @author :努力吧小朱.
 * @version : Java1.8
 * @date :Created in 15:05 2019/12/12
 * @ Description:${description}
 * @ Modified By:
 */
@Mapper
public interface PersonDao {
    boolean insertPerson(Person person);

    List<Person> findAll();
}

personDao.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.example.demo.dao.PersonDao">
    <insert id="insertPerson" parameterType="Person">
        insert into person (name, age) values (#{name},#{age})
    </insert>

    <select id="findAll" resultType="Person">
        select * from person
    </select>
</mapper>

test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../jquery-2.1.4.js"></script>
</head>

<div id="app">
    <input type="text" id="name" placeholder="请输入姓名"></input>
    <input type="number" id="age" placeholder="请输入年龄"></input>
    <button onclick="submit()">提交</button>
</div>
<script>
   function submit() {
       var name = document.getElementById("name").value;
       var age = document.getElementById("age").value;
       $.ajax({
           type: "POST",
           url: "../person/insert",
           data: {
               name: name,
               age: age
           },
           success:function (data) {
               location.href="../page/personList"
           },
           error:function () {
               alert("12")
           }
       });
   }

</script>
</body>
</html>

personList.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../jquery-2.1.4.js"></script>
</head>
<body>
<div id="app">

</div>
<script>
   $(function () {
       $.ajax({
           type: "POST",
           url: "../person/findAll",
           success:function (data) {
               for (var i = 0; i < data.length; i++) {
                   $("#app").append('' +
                       '序号:<p>'+data[i].id+'</p>,名字:<p>'+data[i].name+'</p>,年龄:<p>'+data[i].age+'</p><br>')
               }
           },
           error:function () {
               alert("异常")
           }
       });
   })
</script>
</body>
</html>

PageController

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author :努力吧小朱.
 * @version : Java1.8
 * @date :Created in 15:22 2019/12/12
 * @ Description:${description}
 * @ Modified By:
 */
@Controller
@RequestMapping("page")
public class PageController {
    @RequestMapping("test")
    public String test(){
        return "test";
    }
    @RequestMapping("personList")
    public String personList(){
        return "personList";
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

德玛西亚!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值