create database springboot default character set utf8 collate utf8_general_ci; create user 'spring'@'%' identified by 'spring#123'; create user 'spring'@'localhost' identified by 'spring#123'; grant all privileges on springboot.* to 'spring'@'%' identified by 'spring#123'; grant all privileges on springboot.* to 'spring'@'localhost' identified by 'spring#123'; flush privileges;
drop table if exists `student`; create table `student` ( `sno` int(11) not null auto_increment comment '学号', `sname` varchar(50) character set utf8 collate utf8_general_ci not null comment '姓名', `ssex` varchar(2) character set utf8 collate utf8_general_ci not null comment '性别', primary key (`sno`) using btree ) engine = innodb auto_increment = 1 character set = utf8 collate = utf8_general_ci row_format = dynamic;
insert into `student` values (1, 'KangKang', 'M'); insert into `student` values (2, 'Mike', 'M'); insert into `student` values (3, 'Jane', 'F');
5.2 创建对应实体:
1 2 3 4 5 6 7 8
@Getter @Setter public class Student implements Serializable { private static final long serialVersionUID = -339516038496531943L; private int sno; private String name; private String sex; }
创建一个包含基本CRUD的StudentMapper:
1 2 3 4 5 6 7 8 9 10 11 12
@Component @Mapper public interface StudentMapper {
@Component @Mapper public interface StudentMapper { @Insert("insert into student(sno,sname,ssex) values(#{sno},#{name},#{sex})") int add(Student student);
@Update("update student set sname=#{name},ssex=#{sex} where sno=#{sno}") int update(Student student);
@Delete("delete from student where sno=#{sno}") int deleteBysno(int sno);
public interface StudentService { int add(Student student); int update(Student student); int deleteBysno(int sno); Student queryStudentBySno(int sno); }