1.添加
void add(Brand brand);//添加
<insert id="add" useGeneratedKeys="true" keyProperty="id"> -- 设置两个参数后,才能获取主键
INSERT into tb_brand(brand_name, company_name, ordered, description, status)
VALUES (#{brandName},#{companyName},#{ordered},#{description},#{status})
</insert>
@Test
public void add() throws IOException {
//接受参数
String brandName = "vivo";
String companyName = "维沃移动通信有限公司";
int ordered = 1000;
String description = "致力于为消费者打造拥有极致拍照、畅快游戏、Hi-Fi音乐的智能手机产品";
int status = 1;
//封装对象
Brand brand = new Brand();
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);
//1.加载核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取对应的SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
// SqlSession sqlSession = sqlSessionFactory.openSession(false);//关闭自动提交,默认
// SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交
//3获取userMapper接口的代理对象(接口的实现类对象)
BrandMapper BrandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行sql
BrandMapper.add(brand);
int id = brand.getId();
System.out.println(id);
//5.提交事务
sqlSession.commit();
//6.释放资源
sqlSession.close();
}
2.动态修改
int update(Brand brand);//修改,返回影响的行数
<update id="update">
UPDATE tb_brand
<set>
<if test="brandName != null and brandName != ''">
brand_name = #{brandName},
</if>
<if test="companyName != null and companyName != ''">
company_name = #{companyName},
</if>
<if test="ordered != null and ordered != ''">
ordered = #{ordered},
</if>
<if test="description != null and description != ''">
description = #{description},
</if>
<if test="status != null and status != ''">
status = #{status}
</if>
</set>
where
id = #{id}
</update>
@Test
public void testUpdate() throws IOException {
//接受参数
String brandName = "vivo";
String companyName = "维沃移动通信有限公司";
int ordered = 300;
String description = "致力于为消费者打造拥有极致拍照、畅快游戏、Hi-Fi音乐的智能手机产品";
int status = 1;
int id = 4;
//封装对象
Brand brand = new Brand();
// brand.setBrandName(brandName);
// brand.setCompanyName(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
// brand.setStatus(status);
brand.setId(id);
//1.加载核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取对应的SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交
//3获取userMapper接口的代理对象(接口的实现类对象)
BrandMapper BrandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行sql
int count = BrandMapper.update(brand);
System.out.println(count);
//5.释放资源
sqlSession.close();
}
3.删除
3.1删除一个
void deleteById(int id);//根据id删除数据
<delete id="deleteById">
DELETE from tb_brand WHERE id = #{id}
</delete>
@Test
public void testDeleteById() throws IOException {
//接受参数
int id = 5;
//1.加载核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取对应的SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交
//3获取userMapper接口的代理对象(接口的实现类对象)
BrandMapper BrandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行sql
BrandMapper.deleteById(id);
//5.释放资源
sqlSession.close();
}
3.2批量删除
void deleteByIds(@Param("ids") int[] ids);//批量删除数据
//void deleteByIds(int[] ids);//批量删除数据
<delete id="deleteByIds">
DELETE FROM tb_brand WHERE id in (
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
)
</delete>
<!--<delete id="deleteByIds">-->
<!--DELETE FROM tb_brand WHERE id in (-->
<!--<foreach collection="array" item="id" separator=",">-->
<!--#{id}-->
<!--</foreach>-->
<!--)-->
<!--</delete>-->
@Test
public void testDeleteByIds() throws IOException {
//接受参数
int[] ids = {6,7};
//1.加载核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取对应的SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交
//3获取userMapper接口的代理对象(接口的实现类对象)
BrandMapper BrandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行sql
BrandMapper.deleteByIds(ids);
//5.释放资源
sqlSession.close();
}