在真正开发过程中对于一些复杂的SQL和多表联查就需要我们自己去编写代码和SQL语句,这个时候可以使用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>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
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.5.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
dependencies>
第二步: 在IDEA的Database
中设置参数连接本地的MySQL数据库
第三步: 使用MyBatisX插件设置生成表相关的mapper,service,映射文件的存放位置
查看mapper接口,mapper接口的映射文件,service接口及其实现类的最终生成位置
自定义方法和SQL
根据mapper接口中的自定义的模板方法名在SQL映射文件中生成对应的增删改查的SQL语句,自动将mapper接口中的方法与映射文件的SQL语句相关联
insert,select,update,delete
为前缀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();
}
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 id="Base_Column_List">
uid,user_name,age,email,sex,is_deleted
sql>
<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>
<delete id="deleteByUidAndUserName">
delete from t_user where uid = #{uid,jdbcType=NUMERIC} AND user_name = #{userName,jdbcType=VARCHAR}
delete>
<update id="updateAgeAndSexByUid">
update t_user set age = #{age,jdbcType=NUMERIC},sex = #{sex,jdbcType=NUMERIC} where uid = #{uid,jdbcType=NUMERIC}
update>
<select id="selectAgeAndSexByAgeBetween" resultMap="BaseResultMap">
select age, sex from t_user where age between #{beginAge,jdbcType=INTEGER} and #{endAge,jdbcType=INTEGER}
select>
<select id="selectAllOrderByAgeDesc" resultMap="BaseResultMap">
select <include refid="Base_Column_List"/> from t_user order by age desc
select>
mapper>