在数据添加成功后,需要获取插入数据库的主键的值
比如:添加订单和订单项
- 添加订单
- 添加订单项,订单项中需要设置所属订单的id
1.编写接口方法:Mapper接口
- 参数:除了id之外的所有数据
- 结果:void
void add(Brand brand);
2.编写SQL语句:SQL映射文件
insert into tb_brand ( brand_name, company_name, ordered, score)
values (#{brandName},#{companyName},#{ordered},#{score});
3.执行方法:测试
@Test
public void testAdd2() throws IOException {
//接收参数
int score = 100;
String companyName = "摇滚之星游戏公司";
String brandName = "Red Dead: RedemptionⅡ";
int ordered = 2;
//封装对象
Brand brand = new Brand();
brand.setScore(score);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);
brand.setOrdered(ordered);
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
//SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.add(brand);
Integer id = brand.getId();
System.out.println(id);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
MyBatis事务:
- openSession():默认开启事务,进行增删改操作后需要使用sqlSession.commit();手动提交事务
- openSession(true):可以设置为自动提交事务(关闭事务)
1.编写接口方法:Mapper接口
- 参数:所有数据
- 结果:void
int update(Brand brand);
2.编写SQL语句:SQL映射文件
update tb_brand
set brand_name = #{brandName},
company_name = #{companyName},
ordered = #{ordered},
score = #{score}
where id = #{id};
3.执行方法:测试
@Test
public void testUpdate() throws IOException {
//接收参数
int score = 100;
String companyName = "摇滚之星游戏公司";
String brandName = "Red Dead: Redemption";
int ordered = 3;
int id = 6;
//封装对象
Brand brand = new Brand();
brand.setScore(score);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);
brand.setOrdered(ordered);
brand.setId(id);
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
//SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
int count = brandMapper.update(brand);
System.out.println(count);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
修改动态字段
1.编写接口方法:Mapper接口(同上)
2.编写SQL语句:SQL映射文件
update tb_brand
brand_name = #{brandName},
company_name = #{companyName},
ordered = #{ordered},
score = #{score}
where id = #{id};
3.执行方法:测试(同上)
删除 删除一个1.编写接口方法:Mapper接口
- 参数:id
- 结果:void
void deleteById(int id);
2.编写SQL语句:SQL映射文件
delete from tb_brand where id = #{id};
3.执行方法:测试
@Test
public void testDeleteById() throws IOException {
//接收参数
int id = 8;
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
//SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.deleteById(id);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
批量删除
1.编写接口方法:Mapper接口
- 参数:id数组
- 结果:void
void deleteByIds(@Param("ids") int[] ids);
2.编写SQL语句:SQL映射文件
delete from tb_brand where id in ( #{id} );
3.执行方法:测试
@Test
public void testDeleteByIds() throws IOException {
//接收参数
int[] ids = {7,9};
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
//SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
brandMapper.deleteByIds(ids);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
MyBatis参数传递
MyBatis接口方法中可以接收各种各样的参数,MyBatis底层对于这些参数进行不同的封装处理方式
- 单个参数:
- POJO类型:直接使用,属性名和参数占位符名称一致
- Map集合:直接使用,键名和参数占位符名称一致
- Collection:封装为Map集合
map.put("collection",collection集合); map.put("arg0",collection集合); - List:封装为Map集合
map.put("collection",list集合); map.put("list",list集合); map.put("arg0",list集合); - Array:封装为Map集合
map.put("array",数组); map.put("arg0",数组); - 其他类型:直接使用
- 多个参数:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
map.put("arg0",参数值1); map.put("param1",参数值1); map.put("arg1",参数值2); map.put("param2",参数值2); ----------@Param("username") map.put("username",参数值1);
MyBatis提供了ParamNameResoiver类来进行参数封装
建议:将来都是用@Param注解来修改Map集合中默认的键名,并使用修改后的名称来获取值,这样可读性更高
注解完成增删改查使用注解开发会比配置文件开发更加方便
@Select("select * from tb_user where id = #{id}")
public User selectById(int id);
- 查询:@Select
- 添加:@Insert
- 修改:@Update
- 删除:@Delect
提示:
- 注解完成简单功能
- 配置文件未完成复杂功能
使用注解 来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,Java注解不仅从心,还会让本就复杂的SQL语句更加混乱不堪。因此,如果需要做一些很复杂的操作,最好用XML来映射语句。
选择何种方式来配置映射,以及认为是否应该要统一映射语句定义的形式,完全取决于程序员自己及其团队。换句话说,永远不要拘泥于一种方式,我们需要轻松的在基于注解和XML的语句映射方式间自由移植和切换。



