create table idcard(
id int not null primary key AUTO_INCREMENT,
stuid varchar(15),
classname varchar(15)
);
create table person(
id int not null primary key AUTO_INCREMENT,
age int,
name varchar(15),
idcard int not null,
foreign key(idcard) references idcard(id)
);
INSERT INTO idcard VALUES(1,2,"一班"),(2,3,"二班"),(3,4,"三班");
INSERT INTO person VALUES(1,18,"小红",1),(2,19,"小华",2),(3,20,"小明",3);
package com.hnucm.springboot.pojo;import lombok.Data;@Data
public class Idcard {private int id;private String stuid;private String classname;}
package com.hnucm.springboot.pojo;import lombok.Data;@Data
public class Person {private int id;private int age;private String name;private Idcard idcardid;
}
12.dao层
package com.hnucm.springboot.dao;import com.hnucm.springboot.pojo.Idcard;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface IdcardMapper {public Idcard findIdcardById(Integer id);public int addIdcard(Idcard idcard);public int deleteIdcardById(Integer id);
}
package com.hnucm.springboot.dao;import com.hnucm.springboot.pojo.Person;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface PersonMapper {public List findAllPersonIdcard();public List findAllPersonIdcard2();public List findAllPerson();public int deletePersonById(Integer id);public int addPerson(Person person);public int updatePerson(Person person);
}
13. service层
package com.hnucm.springboot.service;import com.hnucm.springboot.pojo.Person;import java.util.List;public interface PersonService {public List findAllPerson();public int deletePersonById(Integer id,Integer idcardid);public int addPerson(Person person);public int updatePerson(Person person);
}
package com.hnucm.springboot.service;import com.hnucm.springboot.dao.IdcardMapper;
import com.hnucm.springboot.dao.PersonMapper;
import com.hnucm.springboot.pojo.Person;
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{@AutowiredPersonMapper personMapper;@AutowiredIdcardMapper idcardMapper;
// @Override
// public List findAllPerson() {
// return personMapper.findAllPerson();
// }@Transactional@Overridepublic int deletePersonById(Integer id,Integer idcardid) {idcardMapper.deleteIdcardById(idcardid);return personMapper.deletePersonById(id);}@Overridepublic int addPerson(Person person) {int result1= idcardMapper.addIdcard(person.getIdcardid());int result2= personMapper.addPerson(person);if(result2>0&&result1>0){return result1;}return 0;}@Overridepublic int updatePerson(Person person) {return personMapper.updatePerson(person);}@Overridepublic List findAllPerson(){return personMapper.findAllPersonIdcard2();}
}
insert into idcard(stuid,classname) values (#{stuid},#{classname})delete from idcard where id =#{id}
delete from person where id=#{id}
insert into person(name,age,idcardid) values (#{name},#{age},#{idcardid.id})update personset name=#{name},age=#{age}where id=#{id}