pom引入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.4</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
启动类添加mapper扫描
@MapperScan("com.example.demo.mapper")
mapper
@Repository
public interface vodMapper {
int inserlist(List<vod>lists);
int deleteList(int[] arrs);
int updateList(List<vod> lists);
}
数据库连接配置要加上allowMultiQueries=true,不然不能批量修改
spring.datasource.url=jdbc:mysql://localhost:3306/itv_basic_v022?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
mappers.xml
<!-- 批量添加 -->
<insert id="inserlist" parameterType="java.util.List" useGeneratedKeys="false" >
insert into con_vod ( cp_id, con_name,
first_letter, nike_name, clazz_flag,
update_time, opt_staff_id, statuz,
rtsp_bd, rtsp_hd, size,
busi_type, time_long)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.cpId},
#{item.conName},
#{item.firstLetter},
#{item.nikeName},
#{item.clazzFlag},
#{item.updateTime},
#{item.optStaffId},
#{item.statuz},
#{item.rtspBd},
#{item.rtspHd},
#{item.size},
#{item.busiType},
#{item.timeLong}
)
</foreach>
</insert>
<!-- 批量删除 -->
<delete id="deleteList" parameterType="int[]">
delete from con_vod where con_id in
<foreach collection="array" item="arr" index="no" open="("
separator="," close=")">
#{arr}
</foreach>
</delete>
<!-- 批量更新 -->
<update id="updateList" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update con_vod
<set>
con_name=#{item.conName}
</set>
where con_id = #{item.conId}
</foreach>
</update>
service
@Service
public interface vodService {
public int addlist(List<vod> list);
int deleteList(int[] arrs);
int updateList(List<vod> lists);
}
serviceImpl
@Component
public class vodServiceImpl implements vodService{
@Autowired
vodMapper vodMapper;
public int addlist(List<vod> list) {
return vodMapper.inserlist(list);
}
public int deleteList(int[] arrs){
return vodMapper.deleteList(arrs);
}
public int updateList(List<vod> lists){
return vodMapper.updateList(lists);
}
}
controller
@Controller
public class vodController {
@Autowired
vodService vodService;
@GetMapping("/addvod")
@ResponseBody
public void addvod(){
List<vod> list=new ArrayList<>();
vod vod=new vod(22l,"woshi","saf","23","22","199723-2",22l,"1","http12382034","httwaaskds",1232,"13823748");
vod vod1=new vod(22l,"woshi","saf","23","22","199723-2",22l,"1","http12382034","httwaaskds",1232,"13823748");
list.add(vod);
list.add(vod1);
System.out.println(vodService.addlist(list));
}
//批量删除
@GetMapping("/deletevod")
@ResponseBody
public void deleteList()
{int[] arrs={11124,11125};
System.out.println( vodService.deleteList(arrs));
}
//批量更新
@GetMapping("/updatevod")
@ResponseBody
public void updateList()
{ List<vod> list=new ArrayList<>();
vod vod=new vod(11126l,22l,"abcd","saf","23","22","199723-2",22l,"1","http12382034","httwaaskds",1232,"13823748","232");
vod vod1=new vod(11127l,22l,"abcdes","saf","23","22","199723-2",22l,"1","http12382034","httwaaskds",1232,"13823748","232");
list.add(vod);
list.add(vod1);
System.out.println( vodService.updateList(list));
}
}