@Insert
:實現新增@Update
:實現更新@Delete
:實現刪除@Select
:實現查詢@Result
:實現結果集封裝@Results
:可以與@Result 一起使用,封裝多個結果集@ResultMap
:實現引用@Results 定義的封裝@One
:實現一對一結果集封裝@Many
:實現一對多結果集封裝@SelectProvider
: 實現動態 SQL 映射@CacheNamespace
:實現注解二級緩存的使用public class User implements Serializable {
private Integer userId;
private String userName;
private Date userBirthday;
private String userSex;
private String userAddress;public Integer getUserId() {return userId;
}
public void setUserId(Integer userId) {this.userId = userId;
}
.....
public interface IUserDao {
/**
* 查詢所有用戶
* @return
*/
@Select("select * from user")
@Results(id="userMap",value= {@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday")})
List<User> findAll();/**
* 根據 id 查詢一個用戶
* @param userId
* @return
*/
@Select("select * from user where id = #{uid} ")
@ResultMap("userMap")
User findById(Integer userId);/**
* 保存操作
* @param user
* @return
*/
@Insert("insert into user(username,sex,birthday,address)values(#{username},#{sex},#{birthday},#{address})")
int saveUser(User user);/**
* 更新操作
* @param user
* @return
*/
@Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id =#{id} ")
int updateUser(User user);/**
* 刪除用戶
* @param userId
* @return
*/
@Delete("delete from user where id = #{uid} ")
int deleteUser(Integer userId);/**
* 查詢使用聚合函數
* @return
*/
@Select("select count(*) from user ")
int findTotal();/**
* 模糊查詢
* @param name
* @return
*/
@Select("select * from user where username like #{username} ")
List<User> findByName(String name);
}
<!-- 配置映射信息 -->
<mappers><!-- 配置 dao 接口的位置,它有兩種方式
第一種:使用 mapper 標簽配置 class 屬性
第二種:使用 package 標簽,直接指定 dao 接口所在的包
--><package name="cn.myp666.dao"/>
</mappers>
<resultMap>
來實現,在使用注解開發時我們需要借助@Results 注解,@Result 注解,@One 注解,@Many 注解。@Results 注解
<resultMap>
@Result 注解
<id>
標簽和<result>
標簽@One注解
(一對一)
<assocation>
標簽,是多表查詢的關鍵,在注解中用來指定子查詢返回單一對象。@Many注解
(多對一)
<Collection>
標簽,是是多表查詢的關鍵,在注解中用來指定子查詢返回對象集合。public interface IAccountDao {
/**
* 查詢所有賬戶,采用延遲加載的方式查詢賬戶的所屬用戶
* @return
*/
@Select("select * from account")
@Results(id="accountMap",value= {@Result(id=true,column="id",property="id"),@Result(column="uid",property="uid"),@Result(column="money",property="money"),@Result(column="uid",property="user",one=@One(select="cn.myp666.dao.IUserDao.findById",fetchType=FetchType.LAZY)
)
})
List<Account> findAll();
}
public interface IUserDao {
/**
* 查詢所有用戶
* @return
*/
@Select("select * from user")
@Results(id="userMap",value= {@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday")
})
List<User> findAll();/**
* 根據 id 查詢一個用戶
* @param userId
* @return
*/
@Select("select * from user where id = #{uid} ")
@ResultMap("userMap")
User findById(Integer userId);
}
public interface IUserDao {
/**
* 查詢所有用戶
* @return
*/
@Select("select * from user")
@Results(id="userMap",value= {@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday"),@Result(column="id",property="accounts",many=@Many(select="cn.myp666.dao.IAccountDao.findByUid",fetchType=FetchType.LAZY))})List<User> findAll();
}
@Many:
相當于<collection>
的配置LAZY
的值public interface IAccountDao {
/**
* 根據用戶 id 查詢用戶下的所有賬戶
* * @param userId
* @return
*/
@Select("select * from account where uid = #{uid} ")
List<Account> findByUid(Integer userId);
}
Mybatis plus?
<!-- 配置二級緩存 -->
<settings>
<!-- 開啟二級緩存的支持 --><setting name="cacheEnabled" value="true"/>
</settings>
@CacheNamespace(blocking=true)//mybatis 基于注解方式實現配置二級緩存
public interface IUserDao {}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态