一.演示:
1.SQL语句:源代码
-- 插入(新增)数据-->有的数据比如有自增效果,可填可不填
insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)
values ('Tom','汤姆','1','1.png',1,'2005-01-01',1,now(),now());
2.Mybatis语句-->需要注解@Insert:(代表插入数据)
上述图片的数据是写死的,为了复用性,要改为动态的:
方案1:把每个参数一一写入形参(但较繁琐);
方案2:把每个参数封装成一个对象,使用时直接传递对象即可:源代码
EmpMapper接口:
package com.itheima.mapper;
import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmpMapper {
//根据ID删除数据-->需要注解@Delete
/* 删除的id不确定,因此需要定义为动态的,在调用接口里的方法时要用到id,
所以要传递一个参数表示id。由于是动态的,还需要
Mybatis里提供的参数占位符即#{参数名字}
*/
@Delete("delete from emp where id = #{id}")
public int delete(Integer id); //有返回值时代表一共操作了几条记录
//新增员工
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
" values (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime});")
//下划线要换成驼峰命名
public void insert(Emp emp);//形参是pojo里的Emp类对象
}
图片:
测试类里:
package com.itheima;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate;
import java.time.LocalDateTime;
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//注入接口对象
@Autowired
private EmpMapper empMapper;
@Test
public void testDelete(){
int delete = empMapper.delete(16);
System.out.println(delete);//运行结果为0
/* 因为刚才已经把id为17的员工删除了,此时就无法删除了,
故操作了0条数据
*/
}
@Test //@Test可以写多个
public void testInsert(){
//构造员工对象
Emp emp=new Emp();
emp.setUsername("Tom");
emp.setName("汤姆");
emp.setImage("1.jpg");
emp.setGender((short)1 );
emp.setJob((short)1);
emp.setEntrydate(LocalDate.of(2000,1,1));
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
//执行新增员工信息操作
empMapper.insert(emp);
}
}
图片:
运行结果:
二.总结:
三.主键返回:
四.演示主键返回:
1.代码:不加注解@Options时
a.EmpMapper接口里:
b.测试类里:
package com.itheima;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate;
import java.time.LocalDateTime;
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//注入接口对象
@Autowired
private EmpMapper empMapper;
@Test
public void testDelete(){
int delete = empMapper.delete(16);
System.out.println(delete);//运行结果为0
/* 因为刚才已经把id为17的员工删除了,此时就无法删除了,
故操作了0条数据
*/
}
@Test //@Test可以写多个
public void testInsert(){
//构造员工对象
Emp emp=new Emp();
emp.setUsername("Tom2");
emp.setName("汤姆2");
emp.setImage("1.jpg");
emp.setGender((short)1 );
emp.setJob((short)1);
emp.setEntrydate(LocalDate.of(2000,1,1));
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
//执行新增员工信息操作
empMapper.insert(emp);
System.out.println(emp.getId());//运行结果为null
}
}
c.运行结果:
-->发现并没有拿到主键值-->默认情况下进行基础的插入操作是不会把主键值返回的
为了拿到主键值,应该加一个注解@Options
2.代码:加注解@Options时
a.EmpMapper接口里:
package com.itheima.mapper;
import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
@Mapper
public interface EmpMapper {
//根据ID删除数据-->需要注解@Delete
/* 删除的id不确定,因此需要定义为动态的,在调用接口里的方法时要用到id,
所以要传递一个参数表示id。由于是动态的,还需要
Mybatis里提供的参数占位符即#{参数名字}
*/
@Delete("delete from emp where id = #{id}")
public int delete(Integer id); //有返回值时代表一共操作了几条记录
//新增员工
@Options(useGeneratedKeys = true,keyProperty = "id")
//useGeneratedKeys = true代表需要拿到生成的主键值,keyProperty = "id"代表获取的主键最终会封装到Emp对象的id属性当中
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
" values (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime});")
//下划线要换成驼峰命名
public void insert(Emp emp);//形参是pojo里的Emp类对象
}
b.测试类里:
package com.itheima;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate;
import java.time.LocalDateTime;
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//注入接口对象
@Autowired
private EmpMapper empMapper;
@Test
public void testDelete(){
int delete = empMapper.delete(16);
System.out.println(delete);//运行结果为0
/* 因为刚才已经把id为17的员工删除了,此时就无法删除了,
故操作了0条数据
*/
}
@Test //@Test可以写多个
public void testInsert(){
//构造员工对象
Emp emp=new Emp();
emp.setUsername("Tom3");
emp.setName("汤姆3");
emp.setImage("1.jpg");
emp.setGender((short)1 );
emp.setJob((short)1);
emp.setEntrydate(LocalDate.of(2000,1,1));
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
//执行新增员工信息操作
empMapper.insert(emp);
System.out.println(emp.getId());//运行结果为21,代表id为21
}
}