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";
}
}