MyBatis-Plus框架搭建

引入依赖


mysql连接

		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.4</version>
        </dependency>
  • 8.x 版本
		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>

mybatis-plus 依赖

Spring Boot2

		<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>

Spring Boot3

		<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
            <version>3.5.5</version>
        </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&useSSL=false
    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);
    }
}

分页插件


@Configuration
@MapperScan("com.example.mybatisplusdemo.mapper")
public class MybatisPlusConfig {

    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
        //interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
        return interceptor;
    }
}

附录


批量插入或修改

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>

插入数据,获取新数据的id

	<insert id="insertAddress" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO address ( province, city, mobile ) VALUES ( #{province}, #{city}, #{mobile} )
    </insert>
@Repository
public interface AddressMapper extends BaseMapper<Address> {

    int insertAddress(Address address);

}
	@Test
    void contextLoads() {
        Address address = new Address();
        address.setMobile("123456");
        address.setCity("武汉");
        address.setProvince("湖北省");
        addressMapper.insertAddress(address);
        System.out.println("address.getId() = " + address.getId());
    }

@TableId(value = "id", type = IdType.AUTO)
实体类id字段要指定此注解

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值