|
@@ -0,0 +1,55 @@
|
|
|
+package com.lovecoding.test;
|
|
|
+
|
|
|
+import com.lovecoding.mapper.BrandMapper;
|
|
|
+import com.lovecoding.pojo.Brand;
|
|
|
+import org.apache.ibatis.io.Resources;
|
|
|
+import org.apache.ibatis.session.SqlSession;
|
|
|
+import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+public class InsertBrand {
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t1() throws IOException {
|
|
|
+ //mybatis 的配置档
|
|
|
+ String res = "mybatis-config.xml";
|
|
|
+ //使用 mybatis 的 Resources 把配置档转成数据流
|
|
|
+ InputStream resourceAsStream = Resources.getResourceAsStream(res);
|
|
|
+ SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
|
|
|
+ SqlSession sqlSession = build.openSession();
|
|
|
+ BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
|
|
|
+
|
|
|
+ ArrayList<Brand> brands = new ArrayList<Brand>();
|
|
|
+
|
|
|
+ Brand brand1 = new Brand();
|
|
|
+ brand1.setBrandName("翠花酸菜");
|
|
|
+ brand1.setCompanyName("康师傅");
|
|
|
+ brand1.setDescription("好吃再来一桶");
|
|
|
+ brand1.setOrdered(22);
|
|
|
+ brand1.setStatus(1);
|
|
|
+
|
|
|
+ Brand brand2 = new Brand();
|
|
|
+ brand2.setBrandName("翠花羊肉卷");
|
|
|
+ brand2.setCompanyName("康师傅");
|
|
|
+ brand2.setDescription("好吃再来一桶");
|
|
|
+ brand2.setOrdered(22);
|
|
|
+ brand2.setStatus(1);
|
|
|
+
|
|
|
+ brands.add(brand1);
|
|
|
+ brands.add(brand2);
|
|
|
+
|
|
|
+ int i = mapper.insertBatch(brands);
|
|
|
+
|
|
|
+ sqlSession.commit(true);
|
|
|
+
|
|
|
+ System.out.println( i );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|