import java.util.Map;
public interface BrandMapper {
// 添加
void add(Brand brand);
}
2.配置sql映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<insert id="add" useGeneratedKeys="true" keyProperty="id">
insert into tb_brand1 (brand_name, company_name, ordered, description, status)
values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>
```
测试方法:
//添加
@Test
public void testAdd() throws IOException {
//存入参数
int status = 1;
String companyName = "firm4";
String brandName = "某某品牌";
String description = "天下第一";
int ordered = 300;
Brand brand = new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);
brand.setStatus(status);
brand.setOrdered(ordered);
brand.setDescription(description);
//1.加载mybatis的核心配置文件,获取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 mapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
mapper.auto();
//5.传入band类
mapper.add(brand);
//释放资源
sqlSession.close();
}
Brand类:
public class Brand {
private Integer id;
private String brandName ;
private String