首页 > 其他分享 >Maven使用Interface增删改查

Maven使用Interface增删改查

时间:2023-03-21 16:14:21浏览次数:34  
标签:session 改查 worker Maven Worker WorkerMapper Interface import id

package com.niu.test;

import com.niu.dao.WorkerMapper;
import com.niu.pojo.*;
import com.niu.utils.MybatisUtils;

import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;

import java.util.*;

public class MybatisTest {
//    public static void main(String[] args) {
//        SqlSession session = MybatisUtils.getSession();
//        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
//        Worker w = new Worker();
//        w.setAge(20);
//        w.setName("22222");
//        w.setSex("男");
//        w.setWorker_id(110);
//        w.setId(2);
////        mapper.addWorker(w);
//        mapper.updateWorker(w);
//        session.commit();
//    }
    public static void main(String[] args) {
        SqlSession session = MybatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        Worker w = mapper.findByIdAndName(4,"c");
        System.out.println(w);
        //mapper.deleteWorker(2);
        //session.commit();
    }
}

 

在com.niu中创建一个包dao再创建一个WorkerMapper类型选Interface。

package com.niu.dao;

import com.niu.pojo.Worker;
import org.apache.ibatis.annotations.*;

public interface WorkerMapper {
    @Select("select * from tb_worker where id=#{id}")
    Worker findById(int id);
    @Insert("insert into tb_worker(name, age, sex ,worker_id) values(#{name},#{age},#{sex},#{worker_id})")
    void addWorker(Worker w);
    @Update("update tb_worker set name=#{name},age=#{age},sex=#{sex},worker_id=#{worker_id} where id=#{id}")
    void updateWorker(Worker w);
    @Delete("delete from tb_worker where id=#{id}")
    void deleteWorker(int id);
    @Select("select * from tb_worker where id=#{workerId} and name=#{workName}")
    Worker findByIdAndName(@Param("workerId") int workerId,@Param("workName") String workName);
}

 

标签:session,改查,worker,Maven,Worker,WorkerMapper,Interface,import,id
From: https://www.cnblogs.com/TokaiTeio32/p/17240345.html

相关文章