上课代码
This commit is contained in:
parent
64e77dcfd3
commit
d7c566794e
|
@ -60,17 +60,34 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
<!--<dependency>-->
|
||||
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
|
||||
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
|
||||
<!-- <version>3.0.3</version>-->
|
||||
<!--</dependency>-->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.25</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||
<version>3.5.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-generator</artifactId>
|
||||
<version>3.5.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
<version>2.3.31</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package com.hnucm.springboot.springboot1;
|
||||
|
||||
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
|
||||
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FastAutoGenerator.create("jdbc:mysql://106.53.194.250:63306/mybatis2004712?serverTimezone=UTC"
|
||||
, "2004712",
|
||||
"@hnucm1254")
|
||||
.globalConfig(builder -> builder
|
||||
.author("张三")
|
||||
.outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
|
||||
.commentDate("yyyy-MM-dd")
|
||||
)
|
||||
.packageConfig(builder -> builder
|
||||
.parent("com.hnucm.springboot.springboot1")
|
||||
.entity("model")
|
||||
.mapper("dao")
|
||||
.service("service")
|
||||
.serviceImpl("service.impl")
|
||||
.xml("mapper.xml")
|
||||
)
|
||||
.strategyConfig(builder -> builder
|
||||
.entityBuilder()
|
||||
.enableLombok()
|
||||
)
|
||||
.templateEngine(new FreemarkerTemplateEngine())
|
||||
.execute();
|
||||
}
|
||||
}
|
|
@ -1,9 +1,11 @@
|
|||
package com.hnucm.springboot.springboot1;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
// @MapperScan("com.hnucm.springboot.springboot1.dao")
|
||||
public class Springboot1Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.hnucm.springboot.springboot1.controller;
|
||||
|
||||
import com.hnucm.springboot.springboot1.model.IdCard;
|
||||
import com.hnucm.springboot.springboot1.model.Person;
|
||||
import com.hnucm.springboot.springboot1.service.PersonService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -38,14 +39,21 @@ public class PersonController {
|
|||
}
|
||||
|
||||
@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){
|
||||
personService.deletePerson(id);
|
||||
public String deleteperson(int id,int idcardid){
|
||||
personService.deletePerson(id,idcardid);
|
||||
return "redirect:personlist";
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.hnucm.springboot.springboot1.dao;
|
||||
|
||||
import com.hnucm.springboot.springboot1.model.IdCard;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface IdcardMapper {
|
||||
public IdCard getIdcard(int id);
|
||||
public int deleteIdcard(int id);
|
||||
public int addIdcard(IdCard idcard);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.hnucm.springboot.springboot1.dao;
|
||||
|
||||
import com.hnucm.springboot.springboot1.model.IdCard;
|
||||
import com.hnucm.springboot.springboot1.model.Order;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface OrderMapper {
|
||||
public List<Order> getOrderByUserId(int id);
|
||||
}
|
|
@ -10,4 +10,5 @@ import java.util.List;
|
|||
@Mapper
|
||||
public interface PersonIdcardMapper {
|
||||
public List<Person> getAllPersons();
|
||||
public List<Person> getAllPersons1();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package com.hnucm.springboot.springboot1.dao;
|
||||
|
||||
import com.hnucm.springboot.springboot1.model.Order;
|
||||
import com.hnucm.springboot.springboot1.model.Product;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ProducatMapper {
|
||||
public List<Product> getProductByOrderId(int id);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.hnucm.springboot.springboot1.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class Order {
|
||||
private int id;
|
||||
private int userid;
|
||||
private int orderprice;
|
||||
private String orderinfo;
|
||||
private List<Product> productList;
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
package com.hnucm.springboot.springboot1.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
//set+get
|
||||
@Data
|
||||
public class Person {
|
||||
|
@ -8,4 +11,5 @@ public class Person {
|
|||
private int age;
|
||||
private int id;
|
||||
private IdCard idCard;
|
||||
private List<Order> orderList;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package com.hnucm.springboot.springboot1.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Product {
|
||||
private int id;
|
||||
private int price;
|
||||
private String productname;
|
||||
}
|
|
@ -7,7 +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 deletePerson(int id,int idcardid);
|
||||
public int updatePerson(Person person);
|
||||
public List<Person> searchPersonsbyName(String name);
|
||||
}
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package com.hnucm.springboot.springboot1.service.impl;
|
||||
|
||||
import com.hnucm.springboot.springboot1.dao.IdcardMapper;
|
||||
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;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -18,21 +20,28 @@ public class PersonServiceImpl implements PersonService {
|
|||
@Autowired
|
||||
private PersonIdcardMapper personIdcardMapper;
|
||||
|
||||
@Autowired
|
||||
private IdcardMapper idcardMapper;
|
||||
@Override
|
||||
public List<Person> getPersons() {
|
||||
return personIdcardMapper.getAllPersons();
|
||||
return personIdcardMapper.getAllPersons1();
|
||||
}
|
||||
|
||||
//保证一致性 事务
|
||||
@Transactional
|
||||
@Override
|
||||
public int deletePerson(int id,int idcardid) {
|
||||
idcardMapper.deleteIdcard(idcardid);
|
||||
return personMapper.deletePerson(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public int addPerson(Person person) {
|
||||
idcardMapper.addIdcard(person.getIdCard());
|
||||
return personMapper.addPerson(person);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deletePerson(int id) {
|
||||
return personMapper.deletePerson(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updatePerson(Person person) {
|
||||
return personMapper.updatePerson(person);
|
||||
|
|
|
@ -14,8 +14,8 @@ spring.datasource.username=2004712
|
|||
spring.datasource.password=@hnucm1254
|
||||
#??????????MyBatis??
|
||||
#??Mybatis?Mapper??
|
||||
mybatis.mapper-locations=classpath:mapper/*.xml
|
||||
mybatis-plus.mapper-locations=classpath:mapper/*.xml
|
||||
#??Mybatis?????
|
||||
mybatis.type-aliases-package=com.hnucm.springboot.springboot1.model
|
||||
mybatis-plus.type-aliases-package=com.hnucm.springboot.springboot1.model
|
||||
# ??
|
||||
logging.level.com.hnucm.springboot.springboot1 = debug
|
|
@ -0,0 +1,18 @@
|
|||
<?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.IdcardMapper">
|
||||
<select id="getIdcard" resultType="com.hnucm.springboot.springboot1.model.IdCard">
|
||||
select * from idcard where id=#{id};
|
||||
</select>
|
||||
|
||||
<delete id="deleteIdcard" parameterType="Integer">
|
||||
delete from idcard where id = #{id};
|
||||
</delete>
|
||||
<!--useGeneratedKeys增加数据完成后立刻返回增加的主键-->
|
||||
<insert id="addIdcard"
|
||||
useGeneratedKeys="true"
|
||||
keyProperty="id"
|
||||
parameterType="com.hnucm.springboot.springboot1.model.IdCard">
|
||||
insert into idcard(stuid,classname) values(#{stuid},#{classname});
|
||||
</insert>
|
||||
</mapper>
|
|
@ -0,0 +1,19 @@
|
|||
<?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.OrderMapper">
|
||||
<select id="getOrderByUserId" resultMap="OrderMap">
|
||||
select * from orders where userid=#{id};
|
||||
</select>
|
||||
|
||||
<resultMap id="OrderMap" type="com.hnucm.springboot.springboot1.model.Order">
|
||||
<id column="id" property="id"/>
|
||||
<result column="userid" property="userid"/>
|
||||
<result column="orderprice" property="orderprice"/>
|
||||
<result column="orderinfo" property="orderinfo"/>
|
||||
<collection property="productList"
|
||||
ofType="com.hnucm.springboot.springboot1.model.Product"
|
||||
select="com.hnucm.springboot.springboot1.dao.ProducatMapper.getProductByOrderId"
|
||||
column="id">
|
||||
</collection>
|
||||
</resultMap>
|
||||
</mapper>
|
|
@ -1,6 +1,28 @@
|
|||
<?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">
|
||||
|
||||
<select id="getAllPersons1" resultMap="PersonMap1">
|
||||
select * from person;
|
||||
</select>
|
||||
|
||||
<resultMap id="PersonMap1" 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"
|
||||
column="idcardid"
|
||||
select="com.hnucm.springboot.springboot1.dao.IdcardMapper.getIdcard"
|
||||
javaType="com.hnucm.springboot.springboot1.model.IdCard">
|
||||
</association>
|
||||
<!--private List<Order> orderList; id -》 person查出来的数据字段传到 getOrderByUserId-->
|
||||
<collection property="orderList"
|
||||
column="id"
|
||||
select="com.hnucm.springboot.springboot1.dao.OrderMapper.getOrderByUserId"
|
||||
ofType="com.hnucm.springboot.springboot1.model.Order">
|
||||
</collection>
|
||||
</resultMap>
|
||||
|
||||
<!-- 数据库字段名字和类型Person 完全一致 resultType -->
|
||||
<!--不一致 就用resultMap-->
|
||||
<select id="getAllPersons" resultMap="PersonMap">
|
||||
|
@ -18,6 +40,5 @@
|
|||
<result column="stuid" property="stuid"/>
|
||||
<result column="classname" property="classname"/>
|
||||
</association>
|
||||
|
||||
</resultMap>
|
||||
</mapper>
|
|
@ -7,16 +7,18 @@
|
|||
</select>
|
||||
<!--1对1 1对多 多对多 查询(复杂) 增删 修 (单表) -->
|
||||
<!--person表 idcard表 -->
|
||||
|
||||
|
||||
<insert id="addPerson" parameterType="com.hnucm.springboot.springboot1.model.Person">
|
||||
insert into person(name,age) values(#{name},#{age});
|
||||
<!--person表 orders表 1对多 在多对1方加一个额外的字段记录1的一方主键 -->
|
||||
<!--多对多 orders表 product表 多对多 需要增加一张中间表-->
|
||||
<insert id="addPerson"
|
||||
parameterType="com.hnucm.springboot.springboot1.model.Person">
|
||||
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.hnucm.springboot.springboot1.model.Person">
|
||||
update person set name = #{name},age = #{age} where id = #{id};
|
||||
</update>
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?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.ProducatMapper">
|
||||
<select id="getProductByOrderId" resultType="com.hnucm.springboot.springboot1.model.Product">
|
||||
select * from product where id in (
|
||||
select productid from orders_product where orderid=#{id}
|
||||
)
|
||||
</select>
|
||||
|
||||
</mapper>
|
|
@ -10,6 +10,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="classname" placeholder="班级"><br/>
|
||||
<input type="submit" value="增加用户"></input>
|
||||
</input>
|
||||
</form>
|
||||
|
|
|
@ -23,21 +23,39 @@
|
|||
<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>
|
||||
<div th:if="${person.idCard} !=null">
|
||||
<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>
|
||||
</div>
|
||||
|
||||
<div th:if="${person.orderList} !=null ">
|
||||
<td >
|
||||
<ul th:each="order:${person.orderList}">
|
||||
<li>
|
||||
<div th:text="${order.orderinfo}"></div>
|
||||
<div th:each="product:${order.productList}">
|
||||
<div><span th:text="${product.productname}"></span></div>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</td>
|
||||
</div>
|
||||
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="mybatis2004712@106.53.194.250" uuid="49fb1155-cceb-4aaa-b5a0-e4b94ae99142">
|
||||
<driver-ref>mysql.8</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<imported>true</imported>
|
||||
<remarks>$PROJECT_DIR$/../springboot1/src/main/resources/application.properties</remarks>
|
||||
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:mysql://106.53.194.250:63306/mybatis2004712?serverTimezone=UTC</jdbc-url>
|
||||
<jdbc-additional-properties>
|
||||
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||
</jdbc-additional-properties>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
Loading…
Reference in New Issue