MyBatisX插件
在真正开发过程中对于一些复杂的SQL和多表联查就需要我们自己去编写代码和SQL语句,这个时候可以使用MyBatisX插件帮助我们简化开发
- 安装MyBatisX插件:
File -> Settings -> Plugins -> 搜索MyBatisx插件搜索安装然后重启IDEA
跳转文件功能
由于一个项目中的mapper接口和映射文件有很多找起来很麻烦, 而MyBatisX插件可以快速找到mapper接口对应的映射文件以及映射文件对应的mapper接口
在application.xml
文件中指定SQL映射文件的位置,mybatis-plus的mapper-locations属性可以指定配置文件的位置(默认从类路径下的mapper目录下加载)
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#设置MyBatis-Plus的全局配置
mapper-locations: SQL映射文件的路径
代码生成功能
第一部: 创建一个Spring Boot工程并引入相关依赖, 在application.yml
文件中配置数据源的连接信息
spring:
# 配置数据源信息
datasource:
# 配置数据源类型(Spring boot默认使用的数据源)
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库的各个信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false
username: root
password: 123456
<dependencies>
<dependency>
<!--spring boot的启动器-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<!--spring boot的测试启动器-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!--mybatis-plus的启动器-->
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<!--简化实体类开发的依赖(还需要下载lombok插件)-->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<!--mysql的驱动,Spring boot默认的是8以上的版本-->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
第二步: 在IDEA的Database
中设置参数连接本地的MySQL数据库
第三步: 使用MyBatisX插件设置生成表相关的mapper,service,映射文件的存放位置
查看mapper接口,mapper接口的映射文件,service接口及其实现类的最终生成位置
自定义方法和SQL
根据mapper接口中的自定义的模板方法名在SQL映射文件中生成对应的增删改查的SQL语句,自动将mapper接口中的方法与映射文件的SQL语句相关联文章来源:https://uudwc.com/A/8dBEq
- 自定义增删改查的方法时一般以
insert,select,update,delete
为前缀
文章来源地址https://uudwc.com/A/8dBEq
public interface UserMapper extends BaseMapper<User> {
//添加数据时,如果实体类属性为null则不为该属性对应的字段赋值
int insertSelective(User user);
//根据uid和username删除用户
int deleteByUidAndUserName(@Param("uid") Long uid, @Param("userName") String userName);
//根据uid更新用户的年龄和性别
int updateAgeAndSexByUid(@Param("age") Integer age, @Param("sex") Integer sex, @Param("uid") Long uid);
//查询年龄和性别根据年龄的区间范围
List<User> selectAgeAndSexByAgeBetween(@Param("beginAge") Integer beginAge, @Param("endAge") Integer endAge);
//查询所有字段根据年龄降序
List<User> selectAllOrderByAgeDesc();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mybatisx.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.atguigu.mybatisx.pojo.User">
<id property="uid" column="uid" jdbcType="BIGINT"/>
<result property="userName" column="user_name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="email" column="email" jdbcType="VARCHAR"/>
<result property="sex" column="sex" jdbcType="INTEGER"/>
<result property="isDeleted" column="is_deleted" jdbcType="INTEGER"/>
</resultMap>
<!--SQL片段,记录表中的所有字段-->
<sql id="Base_Column_List">
uid,user_name,age,email,sex,is_deleted
</sql>
<!--int insertSelective(User user);-->
<insert id="insertSelective">
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">uid,</if>
<if test="userName != null">user_name,</if>
<if test="age != null">age,</if>
<if test="email != null">email,</if>
<if test="sex != null">sex,</if>
<if test="isDeleted != null">is_deleted,</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">#{uid,jdbcType=BIGINT},</if>
<if test="userName != null">#{userName,jdbcType=VARCHAR},</if>
<if test="age != null">#{age,jdbcType=INTEGER},</if>
<if test="email != null">#{email,jdbcType=VARCHAR},</if>
<if test="sex != null">#{sex,jdbcType=INTEGER},</if>
<if test="isDeleted != null">#{isDeleted,jdbcType=INTEGER},</if>
</trim>
</insert>
<!--int deleteByUidAndUserName(@Param("uid") Long uid, @Param("userName") String userName);-->
<delete id="deleteByUidAndUserName">
delete from t_user where uid = #{uid,jdbcType=NUMERIC} AND user_name = #{userName,jdbcType=VARCHAR}
</delete>
<!--int updateAgeAndSexByUid(@Param("age") Integer age, @Param("sex") Integer sex, @Param("uid") Long uid);-->
<update id="updateAgeAndSexByUid">
update t_user set age = #{age,jdbcType=NUMERIC},sex = #{sex,jdbcType=NUMERIC} where uid = #{uid,jdbcType=NUMERIC}
</update>
<!--List<User> selectAgeAndSexByAgeBetween(@Param("beginAge") Integer beginAge, @Param("endAge") Integer endAge);-->
<select id="selectAgeAndSexByAgeBetween" resultMap="BaseResultMap">
select age, sex from t_user where age between #{beginAge,jdbcType=INTEGER} and #{endAge,jdbcType=INTEGER}
</select>
<!--List<User> selectAllOrderByAgeDesc();-->
<select id="selectAllOrderByAgeDesc" resultMap="BaseResultMap">
select <include refid="Base_Column_List"/> from t_user order by age desc
</select>
</mapper>