SpringBoot加MybatisPlus快速开发项目

相关依赖

首先从在maven项目下pom.xml文件中添加如下依赖

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!--<version>8.0.11</version>-->
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>1.0.5</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.3</version>
        </dependency>

application.properties相关配置

#mysql url
spring.datasource.url=jdbc:mysql://${database.ip}:${database.port}/${database.name}?useUnicode=true&characterEncoding=utf8
#用户名
spring.datasource.username=root    
# 密码
spring.datasource.password=
database.ip=rm-8vb1eolh0mz91q63w1o.mysql.zhangbei.rds.aliyuncs.com
database.name=personal_website
database.port=3306
#mybatis plus相关配置,更多配置请查看官方文档
#xml文件地址
mybatis-plus.mapper-locations[0]=classpath*:**/xml/*.xml
@SpringBootApplication
//开启事务管理器
@EnableTransactionManagement
//mapper所在的包路径,用于mybatisplus扫描
@MapperScan("com.guojunhui.aboutme.mapper")
public class AboutMeApplication {

    private static ProjectBuilder projectBuilder;

    @Autowired
    public void setProjectBuilder(ProjectBuilder projectBuilder) {
        AboutMeApplication.projectBuilder = projectBuilder;
    }

    public static void main(String[] args) {
        SpringApplication.run(AboutMeApplication.class, args);
    }


}

业务接口,业务接口实现类,Dao接口,实体类相关

实体类,需要指定对应表名,指定Id字段

//指定对应表名
@TableName("website_user")
public class WebsiteUser implements Serializable {

	private static final long serialVersionUID = 1L;

	/*
	  使用该注解时表示该属性对应数据表的id,数据类型应对应推荐使用包装类型
	  指定id字段的列名,
      type:指定id生成的方式,mybatisplus支持的方式
      
      AUTO(0, "数据库ID自增"),
      INPUT(1, "用户输入ID"),
      以下2种类型、只有当插入对象ID 为空,才自动填充。 
      ID_WORKER(2, "全局唯一ID"),
      UUID(3, "全局唯一ID"), 
      NONE(4, "该类型为未设置主键类型"),
      ID_WORKER_STR(5, "字符串全局唯一ID");
      
*/
	@TableId(value = "id", type = IdType.AUTO)
	private Integer id;

	/*
	* user_code
	* @TableFiel 表示属性与数据库列名对应,当属性名与列名一致时可不使用该注解,
	* 驼峰命名时只要全局配置好之后也可不添加该注解,
	*/
	@TableField(value = "user_code")
	private String userCode;


}

更多描述请看下列注释

/**
 * <p>
 * 表字段标识
 * </p>
 *
 * @author hubin sjy tantan
 * @since 2016-09-09
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TableField {

    /**
     * <p>
     * 字段值(驼峰命名方式,该值可无)
     * </p>
     */
    String value() default "";

    /**
     * <p>
     * 当该Field为类对象时, 可使用#{对象.属性}来映射到数据表.
     * </p>
     * <p>
     * 支持:@TableField(el = "role, jdbcType=BIGINT)<br>
     * 支持:@TableField(el = "role, typeHandler=com.baomidou.springcloud.typehandler.PhoneTypeHandler")
     * </p>
     */
    String el() default "";

    /**
     * <p>
     * 是否为数据库表字段
     * </p>
     * <p>
     * 默认 true 存在,false 不存在
     * </p>
     */
    boolean exist() default true;

    /**
     * <p>
     * 字段 where 实体查询比较条件
     * </p>
     * <p>
     * 默认 `=` 等值
     * </p>
     */
    String condition() default SqlCondition.EQUAL;

    /**
     * <p>
     * 字段 update set 部分注入, 该注解优于 el 注解使用
     * </p>
     * <p>
     * 例如:@TableField(.. , update="%s+1") 其中 %s 会填充为字段
     * 输出 SQL 为:update 表 set 字段=字段+1 where ...
     * </p>
     * <p>
     * 例如:@TableField(.. , update="now()") 使用数据库时间
     * 输出 SQL 为:update 表 set 字段=now() where ...
     * </p>
     */
    String update() default "";

    /**
     * <p>
     * 字段验证策略
     * </p>
     * <p>
     * 默认 非 null 判断
     * </p>
     */
    FieldStrategy strategy() default FieldStrategy.NOT_NULL;

    /**
     * <p>
     * 字段自动填充策略
     * </p>
     */
    FieldFill fill() default FieldFill.DEFAULT;
}

Dao接口,自己的mapper接口需继承 BaseMapper并且泛型为对应的实体类

public interface WebsiteUserMapper extends BaseMapper<WebsiteUser> {

}

业务接口类需继承IService接口,泛型为相对应的Dao接口

public interface IWebsiteUserService extends IService<WebsiteUserMapper>{

}

业务接口实现类需继承ServiceImpl接口,此时实现类的泛型为两个,第一个为相对应的Dao接口,第二个为对应的实体类

@Service
public class WebsiteUserServiceImpl extends ServiceImpl<WebsiteUserMapper, WebsiteUser> implements IWebsiteUserService {

    private final WebsiteUserMapper websiteUserMapper;

    public WebsiteUserServiceImpl(WebsiteUserMapper websiteUserMapper) {
        this.websiteUserMapper = websiteUserMapper;
    }

}

遵循相关规定后整合完成了,此时业务接口就可使用mybatisplus的一系列方法了

在这里插入图片描述
测试,引入相关业务接口调用mybatis-plus提供的方法
在这里插入图片描述

更多方法使用请查看 mybatis-plus官方文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值