源码:
链接: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测试