1. 程式人生 > 其它 >mybatis04-基於註解開發

mybatis04-基於註解開發

有四個註解
@select @insert @update @delete

package com.kcl.dao;

import com.kcl.pojo.User;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 專案名: mybatis_heima_01
 * 包名:    com.kcl
 * 檔名   UserDao
 * 建立者
 * 建立時間: 2022/4/2 11:29 PM
 * 描述  使用者持久層介面
 */
public interface UserDao {


    @Select("select * from user")
    List<User> findAllUser();

    @Insert("insert into user(username,address,sex,birthday) values (#{username},#{address},#{sex},#{birthday})")
    void saveUser(User user);

    @Update("update user set username = #{username},address = #{address},sex=#{sex},birthday=#{birthday} where id = #{id}")
    void updateUser(User user);

    @Delete("delete from user where id = #{id}")
    void deleteById(Integer id);
    
    @Select("select * from user where id = #{id}")
    User getById(Integer id);
    
    //使用: findUserByName("%王%");
    @Select("select * from user where username like #{username}")
    List<User> findUserByName(String username);
    
    @Select("select count(*) from user")
    int getCount();
}