由于事务不能自动提交,所以需要主动提交或者在获取sqlsession时打开自动提交
手动提交:sqlSession.commit();
自动提交:SqlSession sqlSession = sqlSessionFactory.openSession(true);
@Test
public void testAdd() throws IOException {
//接收变量
int status = 1;
String companyname = "波导手机";
String brandname = "波导";
String description = "手机中的战斗机";
int ordered = 100;
//String brandname = "";
//处理数据
//companyname = "%" + companyname + "%";
//brandname = "%" + brandname + "%";
//封装类
Brand brand = new Brand();
brand.setCompany_name(companyname);
brand.setBrand_name(brandname);
brand.setStatus(status);
brand.setOrdered(ordered);
brand.setDescription(description);
/* Map map = new HashMap();
map.put("status",status);
map.put("companyname",companyname);
map.put("brandname",brandname);*/
//1、获取sqlSessionFactory
String rescource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(rescource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2、获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//openSession(true)可以自动提交,可以不用commit
//3、获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4、执行方法
brandMapper.add(brand);
sqlSession.commit();
//
//List<Brand> brands = brandMapper.selectBycondition(map);
System.out.println(brand);
//5、释放资源
sqlSession.close();
}
}
sql映射文件
<insert id="add">
insert into tb_brand(brand_name,company_name,ordered,description,status)
values(#{brand_name},#{company_name},#{ordered},#{description},#{status} );
</insert>

void add(Brand brand) ;
主键返回
<insert useGeneratedKeys="true" keyProperty="id" id="add">


修改
全部修改

修改动态字段
使用set标签
mysql映射文件
<update id="update">
update tb_brand
<set>
<if test="brand_name!=null and brand_name!=''">
brandname = #{brand_name},
</if>
<if test="company_name!=null and company_name!=''">
companyname = #{company_name},
</if>
<if test="description!=null and description!=''">
description = #{description},
</if>
<if test="status !=null">
status = #{status},
</if>
<if test="ordered!=null">
ordered = #{ordered},
</if>
</set>
where id = #{id};
</update>

执行文件
@Test public void testedit() throws IOException { //接收变量 String description = "波导手机,手机中的战斗机"; int ordered = 200; int id = 5; //String brandname = ""; //处理数据 //companyname = "%" + companyname + "%"; //brandname = "%" + brandname + "%"; //封装类 Brand brand = new Brand(); brand.setOrdered(ordered); brand.setDescription(description); brand.setId(id); /* Map map = new HashMap(); map.put("status",status); map.put("companyname",companyname); map.put("brandname",brandname);*/ //1、获取sqlSessionFactory String rescource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(rescource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2、获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //3、获取Mapper接口的代理对象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); //4、执行方法 int count = brandMapper.update(brand); sqlSession.commit(); // //List<Brand> brands = brandMapper.selectBycondition(map); System.out.println(count); //5、释放资源 sqlSession.close(); }
接口文件
Integer update(Brand brand);

浙公网安备 33010602011771号