Mybatis(五)——Mybatis注解开发

MyBatis注解开发

注解方式比较简单,但是实际开发不推荐使用注解,使用配置文件的方式,不需要改源代码。

@Insert:添加
@Update:修改
@Delete:删除
@Select:查询
@Result:实现结果集封装
@Results:可以和@Result一起使用,封装多个结果集
@One:实现一对一和多对一的结果集封装
@Many:实现一对多结果级封装

一、使用注解完成CRUD

1. SqlMapConfig.xml配置文件

 <mappers>
        <!--第一种方式:class引入接口,只能引入一个接口-->
        <mapper class="com.qcby.dao.UserAnnoDao"/>
   
        <!--第二种方式:针对com.qcby.dao包下的所有的接口-->
        <package name="com.qcby.dao"/>
</mappers>

2. UserDao接口方法和注解的编写

import com.qcby.entity.User;
import org.apache.ibatis.annotations.*;

import javax.jws.soap.SOAPBinding;
import java.util.List;

public interface UserDao {
   
    //查询所有
    @Select("select * from user")
    @Results(id="userMap",value = {
   
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "address",column = "address")
    })
    public List<User> findAll();
    
    //通过ID查询
    @Select("select * from user where id = #{id}")
    @ResultMap(value = "userMap")
    public User findById(int id);

    //增加
    @Insert("insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})")
    @SelectKey(statement="select last_insert_id()",keyColumn = "id",keyProperty = "id",before =false,resultType =Integer.class)
    public int insert(User user);

    //更新
    @Update
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值