Springboot整合mybatis

源码:

链接:https://pan.baidu.com/s/1WG1HXwdP-IsQvLBdd5QOog 
提取码:cy68 
 

实验任务1:建库建表

在mysql中创建mybatis数据库,并创建customer表,设置id,主键自增,添加sname,jobs,phone字段,如图。

实验任务2:创建项目,引入相关依赖

实验任务3:创建application.properties配置文件

实验任务4:创建customer的pojo类 

package com.hxci.jcy.pojo;

public class Customer {
    private Integer id;
    private String sname;
    private String jobs;
    private String phone;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getJobs() {
        return jobs;
    }

    public void setJobs(String jobs) {
        this.jobs = jobs;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

实验任务5:创建customerDao接口

通过@Param注解传值

package com.hxci.jcy.dao;

import com.hxci.jcy.pojo.Customer;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface CustomerDao {
    //dao--->mapper.xml
    public List<Customer> query(@Param("customer") Customer customer,@Param("pageNo") Integer pageNo);

    public void add(Customer customer);
}

实验任务6:创建customerMapper.xml配置文件

<!--1.namespace:接口的一个路径,2.id:接口下抽象方法名
    3. 接口的返回值和resultType对应上,4.接口的参数对应上 -->
<mapper namespace="com.hxci.jcy.dao.CustomerDao">
    
    <select id="query" resultType="com.hxci.jcy.pojo.Customer">
        select * from customer where 1=1
        <if test="customer.sname != null and customer.sname !=''">
            and sname=#{customer.sname}
        </if>
        <if test="customer.jobs != null and customer.jobs !=''">
            and  jobs=#{customer.jobs}
        </if>
        <if test="pageNo !=null and pageNo !=''">
            limit 0,#{pageNo}
        </if>

    </select>

实验任务7:创建CustomerService和CustomerServiceImpl

 

实验任务8:创建CustomerController

使用postman测试

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值