<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.5version>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.4.5version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>2.4.5version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.21version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
<scope>providedscope>
dependency>
dependencies>
表名:table
设置id主键自增
id | name |
---|---|
主键 | 姓名 |
yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #数据源配置
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&characterEncoding=utf-seSSL=false
username: root
password: root
mybatis:
mapper-locations: classpath:mybatis/*.xml
type-aliases-package: com.zhanghp.entity
<configuration>
<property name="LOG_HOME" value="${catalina.base}/logs/" />
<appender name="Stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
pattern>
layout>
appender>
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/server.%d{yyyy-MM-dd}.logFileNamePattern>
<MaxHistory>30MaxHistory>
rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
pattern>
layout>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MBMaxFileSize>
triggeringPolicy>
appender>
<root level="info">
<appender-ref ref="Stdout" />
root>
<logger name="com.zhanghp.dao" level="DEBUG">logger>
configuration>
- collection="" 遍历的集合或者是数组,参数是数组,collection中名字指定为array,参数是List集合,collection中名字指定为list
参数是Map集合,collection名字指定为map
- separator="" 多个元素取出的时候 用什么文字分隔
- open="" 以什么开头
- close="" 以什么结尾
- item="" 中间变量名
- index="" 集合中元素迭代时的索引
insert into person (id, name) values (1,'张三'), (2,'李四')
@Mapper
public interface PersonMapper {
int insertBatch(List<Person> list);
int insertBatch2(List<String> list);
int insertBatchForMap(@Param("map")Map<Integer, String> map);
}
<mapper namespace="com.zhanghp.dao.PersonMapper">
<insert id="insertBatch">
insert into person (id, name ) values
<foreach collection="list" separator="," item="it">
(#{it.id},#{it.name})
foreach>
insert>
<insert id="insertBatch2">
insert into person (name) values
(#{it})
foreach>
insert>
<insert id="insertBatchForMap">
insert into person (id,name) values
(#{key},#{val})
foreach>
insert>
mapper>
@SpringBootTest(classes = BatchApplication.class)
public class TestDataBase {
@Autowired
private PersonMapper testMapper;
@Test
public void test2(){
List<Person> list = new ArrayList<>();
list.add(new Person(null,"1"));
list.add(new Person(null,"2"));
int i = testMapper.insertBatch(list);
if (i>0) {
System.out.println("yeah");
}
}
@Test
public void test3(){
List<String> list = new ArrayList<>();
list.add("test1");
list.add("test2");
int i = testMapper.insertBatch2(list);
if (i>0) {
System.out.println("yeah");
}
}
@Test
public void insertMap(){
Map<Integer, String> map = new HashMap<>();
map.put(null,"map123");
map.put(null,"map222");
int i = testMapper.insertBatchForMap(map);
if (i>0) {
System.out.println("yeah");
}
}
}
insert into person (id,name) values (1,'2'),(2,'3') on duplicate key update name=values(name);
int updateForNew(List<Person> personList);
<update id="updateForNew">
insert into person (id,name) values
<foreach collection="list" separator="," item="it">
(#{it.id},#{it.name})
foreach>
on duplicate key update name=values(name);
update>
@Test
public void updateForNew(){
List<Person> list = new ArrayList<>();
list.add(new Person(1,"1"));
list.add(new Person(2,"2"));
int i = testMapper.updateForNew(list);
if (i>0) {
System.out.println("yeah");
}
}
int deleteBatch(List<Integer> idList);
<delete id="deleteBatch">
delete from person where id in
<foreach collection="list" item="it" separator="," open="(" close=")">
#{it}
foreach>
delete>
@Test
public void deleteBatch(){
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
int i = testMapper.deleteBatch(list);
if (i>0) {
System.out.println("yeah");
}
}