栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis入门案例 通用Mapper XML写法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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 {

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

    // 模糊查询
    //@Select("select * from tmp_customer where cname like #{cname}")
    public List 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





    
        select * from tmp_customer where cname like #{cname}
    


    
        insert into tmp_customer value(#{cid},#{cname},#{password},#{telephone},#{money},#{version},#{createTime},#{updateTime})
    

    
        delete from tmp_customer where cid = #{cid}
    

        
        update tmp_customer set password = #{password} , money = #{money} where cid = #{cid};
    

 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 list = customerMapper.findAll();
        list.forEach(System.out::println);
    }


    @Test
    public void testFindByCname(){

        String cname = "%o%";
        List 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);
    }




}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/970449.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号