MyBatis-Plus框架搭建

引入依赖


mysql连接

		<dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>6.0.4version>
        dependency>

mybatis-plus 依赖

Spring Boot2

		<dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.3.1version>
        dependency>

Spring Boot3

		<dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-spring-boot3-starterartifactId>
            <version>3.5.5version>
        dependency>

修改application.yml配置


server:
  port: 9003
  servlet:
    context-path: /mp


mybatis-plus:
  type-aliases-package:     #  别名扫描包
  mapper-locations: classpath*:mapper/**/*.xml          # mapper.xml
  configuration:
    map-underscore-to-camel-case: true  # 开启下划线和驼峰的映射
    cache-enabled: false   # 是否开启二级缓存
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      id-type: assign_id  # id为雪花算法生成
      update-strategy: not_null  # 更新策略:只更新非空字段

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mp?serverTimezone=UTC&allowMultiQueries=true
    username: root
    password: 123456

启动类


添加@MapperScan注解

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.example.mybatisplusdemo.mapper")
@SpringBootApplication
public class MybatisplusDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisplusDemoApplication.class, args);
    }
}

附录


批量插入或修改

public interface PersonMapper extends BaseMapper<Person> {

     Integer insertBatch( @Param("list") List<Person> list);

     Integer updateBatch (@Param("list") List<Person> list);
}
	<insert id="insertBatch" >
        insert into tb_user (username,address) values
        <foreach collection="list" item="person" index="index" separator="," >
            (#{person.username}, #{person.address})
        foreach>
    insert>

    <update id="updateBatch">
        <foreach collection="list" index="index" item="person" separator=";">
            update tb_user
              <set>
                  username = #{person.username},
                  address = #{person.address}
              set>
            where id = #{person.id}
        foreach>
    update>

你可能感兴趣的:(mybatis)