第六次课

This commit is contained in:
zhangping 2024-09-11 16:09:17 +08:00
parent bf8cd781ef
commit 0a6fb94cd3
20 changed files with 275 additions and 47 deletions

View File

@ -6,7 +6,7 @@
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
@ -16,4 +16,4 @@
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.7/apache-maven-3.9.7-bin.zip
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip

2
springboot/mvnw vendored
View File

@ -8,7 +8,7 @@
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an

2
springboot/mvnw.cmd vendored
View File

@ -8,7 +8,7 @@
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM https://www.apache.org/licenses/LICENSE-2.0
@REM http://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an

View File

@ -8,12 +8,11 @@
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zp.spring</groupId>
<artifactId>springboot</artifactId>
<groupId>com.c202101080126</groupId>
<artifactId>task1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springboot</name>
<description>springboot</description>
<name>task1</name>
<description>task1</description>
<url/>
<licenses>
<license/>
@ -21,9 +20,6 @@
<developers>
<developer/>
</developers>
<modules>
<module>springboot</module>
</modules>
<scm>
<connection/>
<developerConnection/>
@ -75,7 +71,6 @@
<version>8.0.25</version>
</dependency>
</dependencies>
<build>

View File

@ -7,7 +7,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}

View File

@ -1,5 +1,6 @@
package com.zp.spring.springboot.controller;
import com.zp.spring.springboot.model.IdCard;
import com.zp.spring.springboot.model.Person;
import com.zp.spring.springboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
@ -25,21 +26,58 @@ public class PersonController {
}
//todo用网页展示
@RequestMapping("personlist")
public String personlist(Model model)
// 搜索页面
public String personlist(Model model,String name)
{
model.addAttribute("persons",personService.getPersons());
// 搜索到就展示搜索结果
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";
}
@RequestMapping("addperson")
public String addPerson(Person person)
public String addPerson(String name,int age,String stuid,String classname)
{
Person person=new Person();
person.setAge(age);
person.setName(name);
IdCard idCard=new IdCard();
idCard.setStuid(stuid);
idCard.setClassname(classname);
person.setIdCard(idCard);
personService.addPerson(person);
return "redirect:personlist";
}
}
// 删除
@RequestMapping("deleteperson")
public String deletePerson(int id,int idcardid)
{
personService.addPerson(person);
personService.deletePerson(id,idcardid);
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";
}
}

View File

@ -0,0 +1,15 @@
package com.zp.spring.springboot.dao;
import com.zp.spring.springboot.model.IdCard;
import com.zp.spring.springboot.model.Person;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
//java方法定义
@Mapper
public interface IdcardMapper {
public IdCard getIdcard(int id);
public int deleteIdcard(int id);
public int addIdcard(IdCard idcard);
}

View File

@ -0,0 +1,12 @@
package com.zp.spring.springboot.dao;
import com.zp.spring.springboot.model.Person;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
//java方法定义
@Mapper
public interface PersonIdcardMapper {
public List<Person> getAllPersons1();
}

View File

@ -10,4 +10,6 @@ import java.util.List;
public interface PersonMapper {
public List<Person> getPersons();
public int addPerson(Person person);
public int deletePerson(int id);
public int updatePerson(Person person);
}

View File

@ -0,0 +1,11 @@
package com.zp.spring.springboot.model;
import lombok.Data;
//set+get//
@Data
public class IdCard {
private String stuid;
private int id;
private String classname;
}

View File

@ -7,5 +7,6 @@ public class Person {
private String name;
private int age;
private String id;
private IdCard idCard;
}

View File

@ -6,8 +6,12 @@ import java.util.List;
public interface PersonService {
public List<Person> getPersons();
int addPerson(Person person);
public List<Person> getPersons();//
public int addPerson(Person person);//
public int deletePerson(int id,int idcardid);//
public int updatePerson(Person person);//
public List<Person> searchPersonsbyName(String name);//搜索
}

View File

@ -1,27 +1,62 @@
package com.zp.spring.springboot.service.impl;
import com.zp.spring.springboot.dao.IdcardMapper;
import com.zp.spring.springboot.dao.PersonIdcardMapper;
import com.zp.spring.springboot.dao.PersonMapper;
import com.zp.spring.springboot.model.Person;
import com.zp.spring.springboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class PersonServiceImpl implements PersonService {
//自动注入
@Autowired
private PersonMapper personMapper;
@Autowired
private PersonIdcardMapper personIdcardMapper;
@Autowired
private IdcardMapper idcardMapper;
//
@Override
public List<Person> getPersons() {
return personMapper.getPersons();
return personIdcardMapper.getAllPersons1();
}
//
@Transactional
@Override
public int addPerson(Person person) {
idcardMapper.addIdcard(person.getIdCard());
return personMapper.addPerson(person);
}
@Override
public int addPerson(Person person) {
return personMapper.addPerson(person);
public int deletePerson(int id, int idcardid) {
idcardMapper.deleteIdcard(idcardid);
return personMapper.deletePerson(id);
}
//
// 更新
@Override
public int updatePerson(Person person)
{
return personMapper.UpdatePerson(person);
}
// 搜索
@Override
public String searchPersonbyName(String name)
{
return personMapper.searchPersonbyName(name);
}
}

View File

@ -1,21 +1 @@
spring.application.name=springboot
spring.web.resources.static-locations=classpath:/static/
spring.web.resources.static-location=file:D:/static/,classpath:/static/
# ??????
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# ?????
spring.datasource.name=defaultDataSource
# ???????
spring.datasource.url=jdbc:mysql://106.53.194.250:63306/mybatis202101080126?serverTimezone=UTC
# ??????&???
spring.datasource.username=202101080126
spring.datasource.password=@hnucm1254
#??????????MyBatis??
#??Mybatis?Mapper??
mybatis.mapper-locations=classpath:mapper/*.xml
#??Mybatis?????
mybatis.type-aliases-package=com.zp.spring.springboot.model;
logging.level.com.zp.spring.springboot = debug

View File

@ -0,0 +1,24 @@
<?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.zp.spring.springboot.dao.IdcardMapper">
<!-- id 方法名字 resultType返回值类型-->
<!-- 1对1 1对多 多对多 查询(复杂) 增删修(单表)-->
<!-- person表 idcard表-->
<!-- 查询-->
<select id="getIdcard" resultType="com.zp.spring.springboot.model.IdCard">
select * from idcard where id=#{id};
</select>
<delete id="deleteIdcard" parameterType="Integer">
delete from idcard where id=#{id};
</delete>
<insert id="addIdcard"
useGeneratedKeys="true"
keyProperty="id"
parameterType="com.zp.spring.springboot.model.IdCard">
insert into idcard(stuid,classname) values(#{stuid},#{classname});
</insert>
</mapper>

View File

@ -0,0 +1,49 @@
<?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.zp.spring.springboot.dao.PersonIdcardMapper">
<!-- id 方法名字 resultType返回值类型-->
<!-- 1对1 1对多 多对多 查询(复杂) 增删修(单表)-->
<!-- person表 idcard表-->
<!-- 查询-->
<select id="getAllPersons1" resultMap="PersonMap1">
SELECT * from person;
</select>
<!-- property 实体类字段名字 column数据库字段名字-->
<resultMap id="PersonMap1" type="com.zp.spring.springboot.model.Person">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="idCard"
column="idcardid"
select="com.zp.spring.springboot.dao.IdcardMapper.getIdcard"
javaType="com.zp.spring.springboot.model.IdCard">
</association>
</resultMap>
<!--增加-->
<insert id="addPerson" parameterType="com.zp.spring.springboot.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.zp.spring.springboot.model.Person">
update person set name =#{name},age=#{age} where id=#{id};
</update>
<!-- 搜索-->
<select id="searchPersonbyName" parameterType="String" resultType="com.zp.spring.springboot.model.Person">
select * from person where name like '%${name}%';
</select>
</mapper>

View File

@ -6,8 +6,32 @@
<select id="getPersons" resultType="com.zp.spring.springboot.model.Person">
select * from person;
</select>
<!-- 1对1 1对多 多对多 查询(复杂) 增删修(单表)-->
<!-- person表 idcard表-->
<!--增加-->
<insert id="addPerson" parameterType="com.zp.spring.springboot.model.Person">
insert into person(name,age) values(#{name},#{age});
insert into person(name,age,idcardid) values(#{name},#{age},#{idCard,id});
</insert>
<!-- 删除-->
<delete id="deletePerson" parameterType="Integer">
delete from person where id=#{id};
</delete>
<!-- 更新-->
<update id="updatePerson" parameterType="com.zp.spring.springboot.model.Person">
update person set name =#{name},age=#{age} where id=#{id};
</update>
<!-- 搜索-->
<select id="searchPersonbyName" parameterType="String" resultType="com.zp.spring.springboot.model.Person">
select * from person where name like '%${name}%';
</select>
</mapper>

View File

@ -9,6 +9,8 @@
<form th:action="@{/addperson}" method="post">
<input type="text" name="name" placeholder="姓名"><br>
<input type="text" name="age" placeholder="年龄"><br>
<input type="text" name="stuid" placeholder="学号"><br>
<input type="text" name="clssname" placeholder="班级"><br>
<input type="submit" value="增加用户"></input>
</input>
</form>

View File

@ -6,17 +6,37 @@
</head>
<body>
<h1>人员列表页面</h1>
<form th:action="@{/searchperson}" method="post">
<input type="text" name="name" placeholder="请输入姓名">
<input type="submit" value="搜索用户"></input>
</form>
<a th:href="@{/addpersonpage}">添加Person</a>
<table border="1">
<tr>
<td>id</td>
<td>年龄</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},idcardid=${person.idCard.id})}">删除</a>
</td>
<td>
<a th:href="@{/updateperson(id=${person.id},age=${person.age},name=${person.name})}">更新</a>
</td>
</tr>
</table>
</body>

View File

@ -0,0 +1,17 @@
<!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>