国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 幫助中心 > 正文

Mybatis如何通過(guò)注解開(kāi)啟使用二級(jí)緩存

2024-07-09 22:41:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了Mybatis基于注解開(kāi)啟使用二級(jí)緩存,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

本文主要是補(bǔ)充一下Mybatis中基于注解的二級(jí)緩存的開(kāi)啟使用方法。

1.在Mybatis的配置文件中開(kāi)啟二級(jí)緩存

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"    "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>  <settings>    <!--開(kāi)啟全局的懶加載-->    <setting name="lazyLoadingEnabled" value="true"/>    <!--<!–關(guān)閉立即加載,其實(shí)不用配置,默認(rèn)為false–>-->    <!--<setting name="aggressiveLazyLoading" value="false"/>-->    <!--開(kāi)啟Mybatis的sql執(zhí)行相關(guān)信息打印-->    <setting name="logImpl" value="STDOUT_LOGGING" />    <!--默認(rèn)是開(kāi)啟的,為了加強(qiáng)記憶,還是手動(dòng)加上這個(gè)配置-->    <setting name="cacheEnabled" value="true"/>  </settings>  <typeAliases>    <typeAlias type="com.example.domain.User" alias="user"/>    <package name="com.example.domain"/>  </typeAliases>  <environments default="test">    <environment >      <!--配置事務(wù)-->      <transactionManager type="jdbc"></transactionManager>      <!--配置連接池-->      <dataSource type="POOLED">        <property name="driver" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql://localhost:3306/test1"/>        <property name="username" value="root"/>        <property name="password" value="123456"/>      </dataSource>    </environment>  </environments>  <mappers>    <package name="com.example.dao"/>  </mappers></configuration>

開(kāi)啟緩存 <setting name="cacheEnabled" value="true"/>,為了查看Mybatis中查詢的日志,添加 <setting name="logImpl" value="STDOUT_LOGGING" />開(kāi)啟日志的配置。

2.領(lǐng)域類以及Dao

public class User implements Serializable{  private Integer userId;  private String userName;  private Date userBirthday;  private String userSex;  private String userAddress;  private List<Account> accounts;  省略get和set方法...... }import com.example.domain.User;import org.apache.ibatis.annotations.*;import org.apache.ibatis.mapping.FetchType;import java.util.List;@CacheNamespace(blocking = true)public interface UserDao {  /**   * 查找所有用戶   * @return   */  @Select("select * from User")  @Results(id = "userMap",value = {@Result(id = true,column = "id",property = "userId"),      @Result(column = "username",property = "userName"),      @Result(column = "birthday",property = "userBirthday"),      @Result(column = "sex",property = "userSex"),      @Result(column = "address",property = "userAddress"),      @Result(column = "id",property = "accounts",many = @Many(select = "com.example.dao.AccountDao.findAccountByUid",fetchType = FetchType.LAZY))  })  List<User> findAll();  /**   * 保存用戶   * @param user   */  @Insert("insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})")  void saveUser(User user);  /**   * 更新用戶   * @param user   */  @Update("update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}")  void updateUser(User user);  /**   * 刪除用戶   * @param id   */  @Delete("delete from user where id=#{id}")  void deleteUser(Integer id);  /**   * 查詢用戶根據(jù)ID   * @param id   * @return   */  @Select("select * from user where id=#{id}")  @ResultMap(value = {"userMap"})  User findById(Integer id);  /**   * 根據(jù)用戶名稱查詢用戶   * @param name   * @return   *///  @Select("select * from user where username like #{name}")  @Select("select * from user where username like '%${value}%'")  List<User> findByUserName(String name);  /**   * 查詢用戶數(shù)量   * @return   */  @Select("select count(*) from user")  int findTotalUser();}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 九江市| 徐汇区| 玉环县| 江都市| 永靖县| 南宁市| 遵义县| 交口县| 牟定县| 岳普湖县| 宿松县| 盐边县| 莲花县| 武清区| 抚州市| 顺义区| 肃宁县| 万宁市| SHOW| 安陆市| 沂南县| 屯门区| 石狮市| 万安县| 耿马| 广河县| 曲阳县| 寿光市| 灵川县| 孝感市| 吴川市| 民丰县| 邛崃市| 满城县| 扶余县| 观塘区| 广平县| 龙泉市| 花莲县| 东安县| 讷河市|