第五次课
This commit is contained in:
parent
8929204ee9
commit
64e77dcfd3
|
@ -23,12 +23,15 @@ public class PersonController {
|
|||
}
|
||||
//todo 用网页表格显示列表
|
||||
@RequestMapping("personlist")
|
||||
public String personlist(Model model){
|
||||
model.addAttribute("persons",personService.getPersons());
|
||||
public String personlist(Model model,String name){
|
||||
if(name!=null&&!name.equals("")){
|
||||
model.addAttribute("persons",personService.searchPersonsbyName(name));
|
||||
}else {
|
||||
model.addAttribute("persons", personService.getPersons());
|
||||
}
|
||||
return "personlist.html";
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("addpersonpage")
|
||||
public String addPersonPage(){
|
||||
return "addperson.html";
|
||||
|
@ -39,4 +42,22 @@ public class PersonController {
|
|||
personService.addPerson(person);
|
||||
return "redirect:personlist";
|
||||
}
|
||||
|
||||
@RequestMapping("deleteperson")
|
||||
public String deleteperson(int id){
|
||||
personService.deletePerson(id);
|
||||
return "redirect:personlist";
|
||||
}
|
||||
|
||||
@RequestMapping("updateperson")
|
||||
public String updateperson(Person person,Model model){
|
||||
//todo 传到一个新的页面更新
|
||||
model.addAttribute("person",person);
|
||||
return "updateperson.html";
|
||||
}
|
||||
@RequestMapping("updatepersoncommit")
|
||||
public String updatepersoncommit(Person person){
|
||||
personService.updatePerson(person);
|
||||
return "redirect:personlist";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.hnucm.springboot.springboot1.dao;
|
||||
|
||||
import com.hnucm.springboot.springboot1.model.Person;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
//Java定义方法 实现-》写到配置文件中
|
||||
|
||||
@Mapper
|
||||
public interface PersonIdcardMapper {
|
||||
public List<Person> getAllPersons();
|
||||
}
|
|
@ -9,6 +9,10 @@ import java.util.List;
|
|||
|
||||
@Mapper
|
||||
public interface PersonMapper {
|
||||
public List<Person> searchPersonsbyName(String name);
|
||||
|
||||
public List<Person> getPersons();
|
||||
public int addPerson(Person person);
|
||||
public int deletePerson(int id);
|
||||
public int updatePerson(Person person);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package com.hnucm.springboot.springboot1.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class IdCard {
|
||||
private int id;
|
||||
private String stuid;
|
||||
private String classname;
|
||||
}
|
|
@ -7,4 +7,5 @@ public class Person {
|
|||
private String name;
|
||||
private int age;
|
||||
private int id;
|
||||
private IdCard idCard;
|
||||
}
|
||||
|
|
|
@ -7,4 +7,7 @@ import java.util.List;
|
|||
public interface PersonService {
|
||||
public List<Person> getPersons();
|
||||
public int addPerson(Person person);
|
||||
public int deletePerson(int id);
|
||||
public int updatePerson(Person person);
|
||||
public List<Person> searchPersonsbyName(String name);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.hnucm.springboot.springboot1.service.impl;
|
||||
|
||||
import com.hnucm.springboot.springboot1.dao.PersonIdcardMapper;
|
||||
import com.hnucm.springboot.springboot1.dao.PersonMapper;
|
||||
import com.hnucm.springboot.springboot1.model.Person;
|
||||
import com.hnucm.springboot.springboot1.service.PersonService;
|
||||
|
@ -13,13 +14,34 @@ public class PersonServiceImpl implements PersonService {
|
|||
|
||||
@Autowired
|
||||
private PersonMapper personMapper;
|
||||
|
||||
@Autowired
|
||||
private PersonIdcardMapper personIdcardMapper;
|
||||
|
||||
@Override
|
||||
public List<Person> getPersons() {
|
||||
return personMapper.getPersons();
|
||||
return personIdcardMapper.getAllPersons();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int addPerson(Person person) {
|
||||
return personMapper.addPerson(person);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deletePerson(int id) {
|
||||
return personMapper.deletePerson(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updatePerson(Person person) {
|
||||
return personMapper.updatePerson(person);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> searchPersonsbyName(String name) {
|
||||
return personMapper.searchPersonsbyName(name);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
<?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.hnucm.springboot.springboot1.dao.PersonIdcardMapper">
|
||||
<!-- 数据库字段名字和类型Person 完全一致 resultType -->
|
||||
<!--不一致 就用resultMap-->
|
||||
<select id="getAllPersons" resultMap="PersonMap">
|
||||
SELECT person.*,idcard.stuid,idcard.classname
|
||||
from person,idcard
|
||||
WHERE person.idcardid = idcard.id
|
||||
</select>
|
||||
<!--property 实体类的字段名字 column数据库字段名字 -->
|
||||
<resultMap id="PersonMap" type="com.hnucm.springboot.springboot1.model.Person">
|
||||
<id column="id" property="id"/>
|
||||
<result column="name" property="name"/>
|
||||
<result column="age" property="age"/>
|
||||
<association property="idCard" javaType="com.hnucm.springboot.springboot1.model.IdCard">
|
||||
<id column="idcardid" property="id"/>
|
||||
<result column="stuid" property="stuid"/>
|
||||
<result column="classname" property="classname"/>
|
||||
</association>
|
||||
|
||||
</resultMap>
|
||||
</mapper>
|
|
@ -5,9 +5,23 @@
|
|||
<select id="getPersons" resultType="com.hnucm.springboot.springboot1.model.Person">
|
||||
select * from person;
|
||||
</select>
|
||||
<!--1对1 1对多 多对多 查询(复杂) 增删 修 (单表) -->
|
||||
<!--person表 idcard表 -->
|
||||
|
||||
|
||||
<insert id="addPerson" parameterType="com.hnucm.springboot.springboot1.model.Person">
|
||||
insert into person(name,age) values(#{name},#{age});
|
||||
</insert>
|
||||
|
||||
|
||||
<delete id="deletePerson" parameterType="Integer">
|
||||
delete from person where id = #{id};
|
||||
</delete>
|
||||
<update id="updatePerson" parameterType="com.hnucm.springboot.springboot1.model.Person">
|
||||
update person set name = #{name},age = #{age} where id = #{id};
|
||||
</update>
|
||||
|
||||
<select id="searchPersonsbyName" parameterType="String" resultType="com.hnucm.springboot.springboot1.model.Person">
|
||||
select * from person where name like '%${name}%';
|
||||
</select>
|
||||
</mapper>
|
|
@ -7,6 +7,13 @@
|
|||
<body>
|
||||
<h1>人员列表页面</h1>
|
||||
|
||||
<form th:action="@{/personlist}" method="post">
|
||||
<input type="text" name="name" placeholder="请输入用户姓名">
|
||||
<input type="submit" value="搜索用户"></input>
|
||||
</input>
|
||||
</form>
|
||||
|
||||
|
||||
<a th:href="@{/addpersonpage}">添加Person</a>
|
||||
|
||||
<table border="1">
|
||||
|
@ -14,11 +21,23 @@
|
|||
<td>id</td>
|
||||
<td>年龄</td>
|
||||
<td>姓名</td>
|
||||
<td>学号</td>
|
||||
<td>班级</td>
|
||||
<td>删除操作</td>
|
||||
<td>更新操作</td>
|
||||
</tr>
|
||||
<tr th:each="person:${persons}">
|
||||
<td th:text="${person.id}"></td>
|
||||
<td th:text="${person.age}"></td>
|
||||
<td th:text="${person.name}"></td>
|
||||
<td th:text="${person.idCard.stuid}"></td>
|
||||
<td th:text="${person.idCard.classname}"></td>
|
||||
<td >
|
||||
<a th:href="@{'/deleteperson?id='+${person.id}}">删除</a>
|
||||
</td>
|
||||
<td >
|
||||
<a th:href="@{/updateperson(id=${person.id},age=${person.age},name=${person.name})}">更新</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>更新人员页面</h1>
|
||||
|
||||
<form th:action="@{/updatepersoncommit}" method="post">
|
||||
<input type="id" name="id" th:value="${person.id}" hidden="hidden"><br/>
|
||||
<input type="text" name="name" placeholder="姓名" th:value="${person.name}"><br/>
|
||||
<input type="text" name="age" placeholder="年龄" th:value="${person.age}"><br/>
|
||||
<input type="submit" value="更新用户"></input>
|
||||
</input>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue