首先先从maven仓库:https://mvnrepository.com/ 找到相关依赖,项目中引入,此处是springBoot整合mybatis-plus
一、导入依赖
<dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot test依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MyBatis-Plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<!-- Spring Boot jdbc依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- Spring Boot mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.3.1</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</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是封装很多方法,像分页,crud等等,提供QueryWrapper和UpdateWrapper,免去频繁书写mapper.xml,简化sql。例如:
五、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>