MybatisPlus的使用

本文介绍了如何使用MyBatis-Plus进行数据库操作,包括添加依赖、创建实体类、定义Mapper接口、实现Service及ServiceImpl、使用QueryWrapper构造查询条件,以及在控制器中响应数据。在配置文件中还设置了打印SQL日志。通过这个实例,展示了MyBatis-Plus的简单使用流程。
摘要由CSDN通过智能技术生成

首先需要添加依赖

 <!--添加mybatisplus包-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

其次 需要一个能接受数据的实体类

/**
 * @description 接受表Studyweb数据的实体类
 * @author wangshiqin
 */
@Data//lombok依赖
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)//开启链式加载
@TableName("studyweb") //表名与pojo类的关系
public class StudyWeb_Do {
    @TableId(type = IdType.AUTO)//主键自增
    private Integer id;
    private String name;
    private String version;
    private String addr;
    private String bankcard;
    private String username;
    private String password;
    private String logaddr;
    private String db1;
    private String db1user;
    private String db1password;
    private String db2;
    private String db2user;
    private String db2password;
}

接着创建一个JdbcManager接口 用于继承mybatisplus的CRUD基础类

/**
 * @description 对数据库连接的处理
 * @author wangshiqin
 */
@Mapper
public interface JdbcMapper extends BaseMapper<StudyWeb_Do>{
}

创建service 接口 并创建实现类 serviceimpl继承 service接口

/**
 * @author wangshiqin
 */
public interface PersonService  {
    List<StudyWeb_Do> findStudyweb();
}


**
 * @description 继承JDBC的接口方法,重写service接口中的方法
 * @author wangshiqin
 */
@Service
public class ServiceImpl implements PersonService {
    @Autowired
    private JdbcMapper mapper;

    /**
     *  查询studyweb表所有数据
     * @return
     */
    @Override
    public List<StudyWeb_Do> findStudyweb() {
        return  mapper.selectList(null);
    }
}

QueryWrapper<> 条件构造器 用来拼接where条件

  • 常见逻辑运算符: = eq, > gt , < lt * >= ge, <= le.like(“name”, “精”);
  • //%在左侧选择likeleft,
  • 在右侧选择likeRright.orderByDesc(“id”)依据id倒序
  • .in()批量查询

创建控制层拦截请求,响应数据

/**
 * @description 拦截页面请求
 * @author wangshiqin
 */
@Controller
@Slf4j
@RequestMapping("")
public class PersonController {
    @Autowired
    private PersonService service;

    /**
     * @Description: 初始页面
     * @Param: [model]
     * @return: java.lang.String
     * @Author: wangshiqin
     * @Date: 2021/6/7
     */
    @RequestMapping("/studyweb")
    public String firstPage(String version, Model model){
        List<StudyWeb_Do> studyweb = service.findStudyweb();
        model.addAttribute("list", studyweb);
        return "first";
    }
}

最后在配置文件中 定义 打印sql 语句和结果

#Mybatis-pulus 配置日志 查看sql
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#驼峰映射=关闭(自动将personName 变成 person_name)
mybatis-plus.configuration.map-underscore-to-camel-case=false
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值