一、创建一张数据表,并传入相关数据:
二、增加操作(insert)
三、修改操作(update)
四、删除操作(delete)
五、Mybatis练习
1.创建表
create table poet
(
p_id int auto_increment
primary key,
name varchar(20) null,
gender tinyint null,
dynasty varchar(20) null,
title varchar(100) null,
style varchar(200) null
);
2.poet实体类:
package edu.wust.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Poet {
private Integer id; //id(主键)
private String name; //姓名
private Short gender; //性别
private String dynasty; //朝代
private String title; //头衔
private String style; //风格
}
3.mapper接口中创建一个插入方法
@Insert("insert into tlias.poet(name, gender, dynasty, title, style) " +
"values (#{name},#{gender},#{dynasty},#{title},#{style})")
public void insert(Poet poet);
4.mapper接口中创建一个删除方法
@Delete("delete from tlias.poet where p_id = #{id}")
public void delete(Integer id);
@Test
public void testDelete(){
poetMapper.delete(8);
}
5.mapper接口中创建一个修改方法
@Update("update tlias.poet set name=#{name},gender=#{gender},dynasty=#{dynasty}," +
"title=#{title},style=#{style} where p_id=#{id}")
public void update(Poet poet);
6.mapper接口中创建一个查询方法(根据id进行查询)
@Select("select * from tlias.poet where p_id=#{id}")
public Poet select(Integer id);