使用mybatis-plus注意事项

首先先从maven仓库:https://mvnrepository.com/ 找到相关依赖,项目中引入,此处是springBoot整合mybatis-plus

一、导入依赖

<dependencies>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    
    <dependency>
    	<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-testartifactId>
		<scope>testscope>
	dependency>
	
	<dependency>
		<groupId>com.baomidougroupId>
		<artifactId>mybatis-plus-boot-starterartifactId>
        <version>3.4.3.4version>
	dependency>

    
    <dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-jdbcartifactId>
	dependency>
	
	<dependency>
		<groupId>mysqlgroupId>
		<artifactId>mysql-connector-javaartifactId>
		<scope>runtimescope>
	dependency>
	
	<dependency>
		<groupId>com.alibabagroupId>
		<artifactId>druidartifactId>
		<version>1.1.9version>
	dependency>
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-extensionartifactId>
        <version>3.3.1version>
    dependency>
	
	<dependency>
		<groupId>org.projectlombokgroupId>
		<artifactId>lombokartifactId>
		<optional>trueoptional>
	dependency>
	<dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-amqpartifactId>
    dependency>
     <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-aopartifactId>
    dependency>
dependencies>

二、编写application.yml(注:mysql8,必须要指定时区serverTimezone=UTC)

server:
  port: 8086
 
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
 
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: org.spring.springboot.entity
 

#showSql
logging:
  level:
    com:
      example:
        mapper : debug

三、创建实体和数据库字段时在实体上使用@TableName(“xxxx”)注解对数据库表映射,数据库字段可以多余实体属性,但是当实体中属性多余数据库字段时,需要在指定字段使用@TableFiled(exis=false)
(与JPA不同,JPA则是@Transient注解)例如:
使用mybatis-plus注意事项_第1张图片
四、mybatis-plus是封装很多方法,像分页,crud等等,提供QueryWrapper和UpdateWrapper,免去频繁书写mapper.xml,简化sql。例如:
在这里插入图片描述
使用mybatis-plus注意事项_第2张图片

五、mybatis-plus支持调用同一链接的不同库,但是,当所建的数据库名称中包含“-”,那么再调用该库时,应注意使用反单引号括住,此符号一般在键盘的esc键下,tab键上。例如:

	<select id="selectAll" resultType="cn.infisa.sms.notification.entity.Article">
        select
            a.id as id,
            a.title as title,
            a.pic_url as picUrl,
            a.subtitle as subtitle,
            a.type as type,
            sac.name as articleType
        from `health-care`.article as a
        left join `health-care`.article_classification as ac
        on a.id=ac.article_id
        left join `health-care`.sys_article_classification as sac
        on ac.classification_id=sac.id
        where 1=1
        <if test="article.title!= '' and article.title!=null">
            and  a.title like concat("%",#{article.title},"%")
        if>
        <if test="article.type!= '' and article.type!=null">
            and  a.type=#{article.type}
        if>
        <if test="article.articleType!= '' and article.articleType!=null">
            and  sac.name=#{article.articleType}
        if>
        limit #{pageNum},#{pageSize}
    select>

你可能感兴趣的:(mybatis-plus,mybatis,java,spring,boot)