Mybatis入门案例 通用Mapper XML写法

 Customer类

package com.czxy.domain;


import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "customer",keepGlobalPrefix = true) //表名
public class Customer {

    //1.随机一个字符串
    //@TableId(type = IdType.ASSIGN_UUID)

    //2.随机一个数字(Long)雪花算法
    //@TableId(type = IdType.ASSIGN_ID)

    //3.获得自动增长列信息
    @TableId(type = IdType.AUTO)
    private Integer cid;
    private String cname;
    private String password;
    private String telephone;
    private Double money;

    @Version
    @TableField(fill = FieldFill.INSERT)
    private Integer version;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;


    //@TableLogic
    //@TableField(fill = FieldFill.INSERT)
    //private Integer deleted;


    // exist 是否存储到数据库(是否是临时数据)
    //@TableField(exist = false)
    //private List<Integer> ids;


}

CustomerMapper  (dao层)

package com.czxy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.czxy.domain.Customer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

// 编写dao层
@Mapper
public interface CustomerMapper extends BaseMapper<Customer> {

    // 查询所有
    //@Select("select * from tmp_customer")
    public List<Customer> findAll();

    // 模糊查询
    //@Select("select * from tmp_customer where cname like #{cname}")
    public List<Customer> findByCname(@Param("cname")String cname);

    // 添加
    //@Insert("insert into tmp_customer value(8,'测试555','12123','555',8000,0,null,null)")
    //@Insert("insert into tmp_customer (a,b,v) values(a,b,c)")
    public void addCustomer(Customer customer);

    // 删除
    //@Delete("delete from tmp_customer where cid = 7")
    public void deleteCustomer(@Param("cid")Integer cid);

    // 修改
    //@Update("update tmp_customer set password = 2323 , money = 555 where cname = '测试333'")
    public void editCustomer(Customer customer);


}

Xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czxy.mapper.CustomerMapper">

    <select id="findAll" resultType="customer">
        select * from tmp_customer
    </select>

    <select id="findByCname" parameterType="String" resultType="customer">
        select * from tmp_customer where cname like #{cname}
    </select>

<!--  insert into tmp_customer(cname,password,telephone,create_time) values(#{cname},#{password},#{telephone},#{createTime})  -->
    <insert id="addCustomer" parameterType="Customer">
        insert into tmp_customer value(#{cid},#{cname},#{password},#{telephone},#{money},#{version},#{createTime},#{updateTime})
    </insert>

    <delete id="deleteCustomer" parameterType="Integer">
        delete from tmp_customer where cid = #{cid}
    </delete>

    <update id="editCustomer" parameterType="Customer">    
        update tmp_customer set password = #{password} , money = #{money} where cid = #{cid};
    </update>

</mapper>

 yml:

测试类: 

package com.czxy;


import com.czxy.domain.Customer;
import com.czxy.mapper.CustomerMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringRunner.class)  //spring整合Junit测试
@SpringBootTest(classes = MybatisPlusApplication.class) // spring boot整合Junit测试
public class TestCustomerMapperDome06_xml {


    @Resource
    private CustomerMapper customerMapper;

    @Test
    public void testXml(){
        List<Customer> list = customerMapper.findAll();
        list.forEach(System.out::println);
    }


    @Test
    public void testFindByCname(){

        String cname = "%o%";
        List<Customer> byCname = customerMapper.findByCname(cname);
        byCname.forEach(System.out::println);

    }

    @Test
    public void testAdd(){
//        Customer customer = new Customer(7,"测试555","12123","555",8000d,0,null,null,null,null);
//        customerMapper.addCustomer(customer);

    }

    @Test
    public void testDelete(){
        customerMapper.deleteCustomer(8);
    }

    @Test
    public void testEdit(){
        Customer customer = new Customer();
        customer.setPassword("520");
        customer.setMoney(520d);
        customer.setCid(7);
        customerMapper.editCustomer(customer);
    }




}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值