Uncode-DAL使用文檔

2018-11-23 18:12 更新

本著不重復(fù)造輪子的原則,基于mybatis、spring jdbc、hibernate、mongo等ORM的通用數(shù)據(jù)訪問層,支持基于datasource的讀寫分離、主備自動(dòng)切換和故障轉(zhuǎn)移,支持簡(jiǎn)單的負(fù)載均衡。

功能概述

  • 基于mybatis、spring jdbc、hibernate等各大orm框架實(shí)現(xiàn)通用dal層功能,并可以與已有項(xiàng)目完全兼容。同時(shí)也可以在已經(jīng)實(shí)現(xiàn)的orm框架之間任意切換,不需要對(duì)代碼作任何改動(dòng)。
  • 實(shí)現(xiàn)dal層cache,可以自由設(shè)置過期時(shí)間。
  • 實(shí)現(xiàn)基于多數(shù)據(jù)源(datasource)的讀寫分離、主備切換、故障轉(zhuǎn)移、恢復(fù)檢測(cè)和負(fù)載均衡
  • 使用該組件必須遵循以下規(guī)則:
    • 在使用DTO時(shí),字段名稱與數(shù)據(jù)庫(kù)表字段一致,也可以不使用任何DTO類。
    • 不支持連表查詢,主要是單表操作,如果需要連表請(qǐng)與原生orm框架配合使用。
    • sql默認(rèn)主鍵字段為id,nosql默認(rèn)主鍵字段為_id。

使用場(chǎng)景

隨著nosql等新方案的出現(xiàn),關(guān)系型數(shù)據(jù)庫(kù)更多的被用在業(yè)務(wù)數(shù)據(jù)存儲(chǔ)中,對(duì)于一些像報(bào)表等復(fù)雜數(shù)據(jù)處理的工作大部分公司已經(jīng)不再使用關(guān)系型數(shù)據(jù)庫(kù),而對(duì)于業(yè)務(wù)數(shù)據(jù)90%以上是不需要連表查詢等復(fù)雜操作的,該組件設(shè)計(jì)目標(biāo)是將這90%的工作量變成零,避免重復(fù)工作,提高開發(fā)速度。使用該組件后將不再需要mybatis的自動(dòng)生成,spring jdbc的sql語(yǔ)句,hibernate的各種注解,因?yàn)榻M件可以忙你處理。同時(shí)該組件只是基于orm框架對(duì)所有單表操作的提取和封裝,最終還是由相應(yīng)的orm框架完成持久化工作,所以不會(huì)代替orm的工作。因此論你的項(xiàng)目是新項(xiàng)目,還是老項(xiàng)目都可以使用,不會(huì)對(duì)原有業(yè)務(wù)生產(chǎn)任何影響,只是使你的操作更加便利,不再需要無(wú)數(shù)dao類,只要在需要的service中注入即可。組件需要遵循一定的COC規(guī)則,所以存在一定的使用限制,請(qǐng)大家注意。

maven

目前組件存放于oschina的maven庫(kù)中,請(qǐng)?jiān)陧?xiàng)目中添加以下地址:http://maven.oschina.net/content/repositories/thirdparty/

1 mybatis

    <dependency>
        <groupId>cn.uncode</groupId>
        <artifactId>uncode-dal</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>cn.uncode</groupId>
        <artifactId>uncode-dal-mybatis</artifactId>
        <version>1.0.3</version>
    </dependency>

2 spring jdbc

    <dependency>
        <groupId>cn.uncode</groupId>
        <artifactId>uncode-dal</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>cn.uncode</groupId>
        <artifactId>uncode-dal-spring-jdbc</artifactId>
        <version>1.0.3</version>
    </dependency>

3 mongo

    <dependency>
        <groupId>cn.uncode</groupId>
        <artifactId>uncode-dal</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>cn.uncode</groupId>
        <artifactId>uncode-dal-mongo</artifactId>
        <version>1.0.3</version>
    </dependency>

jar文件下載地址:http://www.uncode.cn/uncode-dal/uncode-dal-all-1.0.3.zip

spring集成

動(dòng)態(tài)數(shù)據(jù)源可以自動(dòng)處理故障轉(zhuǎn)移和恢復(fù)檢測(cè),讀寫分離時(shí)多個(gè)讀庫(kù)采用隨機(jī)獲取。緩存可以自定義實(shí)現(xiàn),可以統(tǒng)一開啟或關(guān)閉,方便在開發(fā)環(huán)境使用。

1 DataSource配置

可以使用任意數(shù)據(jù)庫(kù)連接池組件,建議使用Druid。配置Fastser-DAL動(dòng)態(tài)數(shù)據(jù)源,將自動(dòng)實(shí)現(xiàn)讀寫分離、主備自動(dòng)切換和故障轉(zhuǎn)移等,建議使用。使用Fastser-DAL動(dòng)態(tài)數(shù)據(jù)源后,在spring jdbc/mybatis配置中將所有的ref="dataSource"換成ref="dynamicDataSource"即可。

    <!--可選,動(dòng)態(tài)數(shù)據(jù)源配置 -->
    <bean id="dynamicDataSource" class="cn.uncode.dal.datasource.DynamicDataSource">
        <!-- 從數(shù)據(jù)庫(kù)配置,用于讀操作,目前負(fù)載均衡機(jī)制為隨機(jī)取一個(gè) -->
        <property name="slaveDataSources">
            <map key-type="java.lang.String">
                <entry key="readDataSourceOne" value-ref="dataSource3"/>
                <entry key="readDataSourceTwo" value-ref="dataSource4"/>
            </map>
        </property>
        <!-- 主數(shù)據(jù)庫(kù)配置 -->
        <property name="masterDataSource" ref="dataSource1" />
        <!-- 備數(shù)據(jù)庫(kù)配置 -->
        <property name="standbyDataSource" ref="dataSource2" />
    </bean>

    <!--可選,使用動(dòng)態(tài)數(shù)據(jù)源讀寫分離時(shí)必選 -->
    <bean id="transactionManager" class="cn.uncode.dal.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 可選,Spring事務(wù) -->
    <tx:annotation-driven transaction-manager="transactionManager" />

2 Spring jdbc 實(shí)現(xiàn)配置

    <!-- 可選,緩存默認(rèn)實(shí)現(xiàn),集群時(shí)建議使用分布式緩存自定義實(shí)現(xiàn) -->
    <bean id="dalCache" class="cn.uncode.dal.cache.impl.ConcurrentMapCache"></bean>
    <!-- 配置緩存 -->
    <bean id="cacheManager" class="cn.uncode.dal.cache.support.SimpleCacheManager">
        <property name="cache" ref="dalCache"></property>
    </bean>
    <!-- 配置數(shù)據(jù)源解析器 -->
    <bean id="resolveDatabase" class="cn.uncode.dal.descriptor.db.impl.SimpleResolveDatabase">
        <property name="dataSource" ref="dataSource" />
        <property name="cacheManager" ref="cacheManager" />
    </bean>

    <!-- spring jdbc實(shí)現(xiàn)配置 -->
    <bean id="commonJdbcSupport" class="cn.uncode.dal.spring.jdbc.CommonJdbcSupport">
        <property name="dataSource" ref="dataSource" />
    </bean>    
    <bean id="baseDAL" class="cn.uncode.dal.spring.jdbc.SpringJDBCDAL">
        <property name="cacheManager" ref="cacheManager" />
        <property name="commonJdbcSupport" ref="commonJdbcSupport" />
        <property name="resolveDatabase" ref="resolveDatabase" />
        <!-- 可選,配置全局不使用緩存,默認(rèn)為true -->
        <property name="useCache" value="false" />
        <!-- 可選,樂觀鎖,如果配置該項(xiàng)并且表中存在字段名為ver的字段,則該表可使用樂觀鎖機(jī)制 -->
        <property name="version" value="ver" />
    </bean>

3 Mybatis 實(shí)現(xiàn)配置

    <!-- 可選,緩存默認(rèn)實(shí)現(xiàn),集群時(shí)建議使用分布式緩存自定義實(shí)現(xiàn) -->
    <bean id="dalCache" class="cn.uncode.dal.cache.impl.ConcurrentMapCache"></bean>
    <!-- 配置緩存 -->
    <bean id="cacheManager" class="cn.uncode.dal.cache.support.SimpleCacheManager">
        <property name="cache" ref="dalCache"></property>
    </bean>
    <!-- 配置數(shù)據(jù)源解析器 -->
    <bean id="resolveDatabase" class="cn.uncode.dal.descriptor.db.impl.SimpleResolveDatabase">
        <property name="dataSource" ref="dataSource" />
        <property name="cacheManager" ref="cacheManager" />
    </bean>

    <!-- mybatis實(shí)現(xiàn)配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />  
    </bean>
    <bean id="commonMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
        <property name="mapperInterface" value="cn.uncode.dal.mybatis.CommonMapper" />
    </bean>
    <bean id="baseDAL" class="cn.uncode.dal.mybatis.MybatisDAL">
        <property name="cacheManager" ref="cacheManager" />
        <property name="commonMapper" ref="commonMapper" />
        <property name="resolveDatabase" ref="resolveDatabase" />
        <!-- 可選,配置全局不使用緩存,默認(rèn)為true -->
        <property name="useCache" value="false" />
        <!-- 可選,樂觀鎖,如果配置該項(xiàng)并且表中存在字段名為ver的字段,則該表可使用樂觀鎖機(jī)制 -->
        <property name="version" value="ver" />
    </bean>

4 MongoDB 實(shí)現(xiàn)配置

    <!-- 可選,緩存默認(rèn)實(shí)現(xiàn),集群時(shí)建議使用分布式緩存自定義實(shí)現(xiàn) -->
    <bean id="dalCache" class="cn.uncode.dal.cache.impl.ConcurrentMapCache"></bean>
    <!-- 可選,配置緩存 -->
    <bean id="cacheManager" class="cn.uncode.dal.cache.support.SimpleCacheManager">
        <property name="cache" ref="dalCache"></property>
    </bean>

    <!-- mongo實(shí)現(xiàn)配置 -->
    <bean id="mongoDataBase" class="cn.uncode.dal.mongo.MongoDataBase">
        <property name="host" value="172.16.30.98" />
        <property name="port" value="27017" />
        <property name="db" value="test" />
        <property name="username" value="xiaocong" />
        <property name="password" value="xiaocong" />
    </bean>

    <!-- 注意,mongo3.x請(qǐng)使用cn.uncode.dal.mongo.Mongo3DAL -->
    <bean id="mongoDAL" class="cn.uncode.dal.mongo.MongoDAL">
        <property name="database" ref="mongoDataBase"></property>
        <!-- 可選,配置全局不使用緩存,默認(rèn)為true -->
        <property name="useCache" value="false" />
    </bean>

與原生框架配合使用

該組件只處理所有單表操作,對(duì)于連表等復(fù)雜操作可以使用原生框架,以達(dá)到在滿足所有功能的前提下提高開發(fā)效率,降低成本。

1 Spring jdbc

JdbcTemplate jdbcTemplate = (JdbcTemplate)baseDAL.getTemplate();

2 Mybatis

<!-- 將原來(lái)的SqlSessionFactory配置修改如下 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis.xml"></property>
        <property name="mapperLocations" value="classpath:/cn/uncode/mapper/*Mapper.xml" />
</bean> 

<!-- 掃描注入Dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="cn.uncode.mapper" />
</bean>

3 MongoDB

MongoDB mongoDB = (MongoDB)baseDAL.getTemplate();

API

1 列表查詢

fields需要顯示的字段,queryCriteria查詢條件,seconds緩存時(shí)間

    QueryResult selectByCriteria(List<String> fields, QueryCriteria queryCriteria);
    QueryResult selectByCriteria(String[] fields, QueryCriteria queryCriteria);
    QueryResult selectByCriteria(List<String> fields, QueryCriteria queryCriteria, int seconds);
    QueryResult selectByCriteria(String[] fields, QueryCriteria queryCriteria, int seconds);
    QueryResult selectByCriteria(QueryCriteria queryCriteria);
    QueryResult selectByCriteria(QueryCriteria queryCriteria, int seconds);
    QueryResult selectPageByCriteria(List<String> fields, QueryCriteria queryCriteria);
    QueryResult selectPageByCriteria(String[] fields, QueryCriteria queryCriteria);
    QueryResult selectPageByCriteria(List<String> fields, QueryCriteria queryCriteria, int seconds);
    QueryResult selectPageByCriteria(String[] fields, QueryCriteria queryCriteria, int seconds);
    QueryResult selectPageByCriteria(QueryCriteria queryCriteria);
    QueryResult selectPageByCriteria(QueryCriteria queryCriteria, int seconds);

2 統(tǒng)計(jì)查詢

    queryCriteria查詢條件,seconds緩存時(shí)間
    int countByCriteria(QueryCriteria queryCriteria);
    int countByCriteria(QueryCriteria queryCriteria, int seconds);

3 主鍵查詢

Object帶主鍵對(duì)象實(shí)體,fields需要顯示的字段,queryCriteria查詢條件,seconds緩存時(shí)間,clazz對(duì)象實(shí)例類型,id對(duì)象主鍵

    QueryResult selectByPrimaryKey(Object obj);
    QueryResult selectByPrimaryKey(Object obj, int seconds);
    QueryResult selectByPrimaryKey(List<String> fields, Object obj);
    QueryResult selectByPrimaryKey(String[] fields, Object obj);
    QueryResult selectByPrimaryKey(List<String> fields, Object obj, int seconds);
    QueryResult selectByPrimaryKey(String[] fields, Object obj, int seconds);
    QueryResult selectByPrimaryKey(String[] fields, String database, Object obj, int seconds);
    QueryResult selectByPrimaryKey(Class<?> clazz, Object id);
    QueryResult selectByPrimaryKey(String table, Object id);
    QueryResult selectByPrimaryKey(Class<?> clazz, Object id, int seconds);
    QueryResult selectByPrimaryKey(String table, Object id, int seconds);
    QueryResult selectByPrimaryKey(List<String> fields, Class<?> clazz, Object id);
    QueryResult selectByPrimaryKey(List<String> fields, String table, Object id);
    QueryResult selectByPrimaryKey(List<String> fields, Class<?> clazz, Object id, int seconds);
    QueryResult selectByPrimaryKey(List<String> fields, String table, Object id, int seconds);

4 插入

Object帶主鍵對(duì)象實(shí)體

    Object insert(Object obj);
    Object insert(String table, Map<String, Object> obj);
    Object insert(String database, String table, Map<String, Object> obj);
    void asynInsert(Object obj);
    void asynInsert(String table, Map<String, Object> obj);
    void asynInsert(String database, String table, Map<String, Object> obj);

5 更新

Object帶主鍵對(duì)象實(shí)體,queryCriteria查詢條件

    int updateByCriteria(Object obj, QueryCriteria queryCriteria);
    int updateByPrimaryKey(Object obj);
    int updateByPrimaryKey(String table, Map<String, Object> obj);
    int updateByPrimaryKey(String database, String table, Map<String, Object> obj);

6 刪除

Object帶主鍵對(duì)象實(shí)體,queryCriteria查詢條件,clazz對(duì)象實(shí)例類型,id對(duì)象主鍵

    int deleteByPrimaryKey(Object obj);
    int deleteByPrimaryKey(String table, Map<String, Object> obj);
    int deleteByPrimaryKey(Class<?> clazz, Object id);
    int deleteByPrimaryKey(String table, Object id);
    int deleteByPrimaryKey(String database, String table, Object id);
    int deleteByCriteria(QueryCriteria queryCriteria);

7 其他操作

database數(shù)據(jù)名稱,tableName表名稱

    void reloadTable(String tableName);
    void clearCache(String tableName);
    void reloadTable(String database, String tableName);
    void clearCache(String database, String tableName);

8 QueryCriteria方法

    //排序,如:id desc, name
    setOrderByClause(String orderByClause)
    //去重
    setDistinct(boolean distinct)
    //只查詢一條
    ssetSelectOne(boolean selectOne)
    //第幾頁(yè),首頁(yè)為1
    setPageSize(int pageSize)
    //設(shè)置表名
    setTable(String table)
    //設(shè)置數(shù)據(jù)庫(kù)名稱,一般不需要設(shè)置
    setDatabase(String database)
    //使用樂觀鎖時(shí)設(shè)置版本,只用于更新操作
    setVersion(Object version)
    //根據(jù)DTO對(duì)象類型設(shè)置名表,此時(shí)DTO類名必須與表名一致
    setTable(Class<?> clazz)

9 Criteria方法

    Criteria append(Condition condition, String filed, Object value)
    Criteria append(String filed, Object value)
    Criteria append(String sql)
    andColumnIsNull(String column)
    andColumnIsNotNull(String column)
    andColumnEqualTo(String column, Object value) 
    andColumnNotEqualTo(String column, Object value)
    andColumnGreaterThan(String column, Object value) 
    andColumnGreaterThanOrEqualTo(String column, Object value)
    andColumnLessThan(String column, Object value)
    andColumnLessThanOrEqualTo(String column, Object value)
    andColumnIn(String column, List<Object> values)
    andColumnNotIn(String column, List<Object> values)
    andColumnLike(String column, Object value) 
    andColumnNotLike(String column, Object value)
    andColumnBetween(String column, Object value1, Object value2)
    andColumnNotBetween(String column, Object value1, Object value2)

10 QueryResult方法

    //單條數(shù)據(jù)以map獲取
    Map<String, Object> get()
      //單條數(shù)據(jù)以map獲取,并使用別名替換原來(lái)字段
    Map<String, Object> getWithAliasName(Map<String, String> aliasName)
    //單條數(shù)據(jù)以map獲取,并隱藏不需要顯示的字段
    Map<String, Object> get(List<String> hiddenFields)
    //單條數(shù)據(jù)以map獲取,隱藏不需要顯示的字段,使用別名替換原來(lái)字段
    Map<String, Object> getWithAliasName(List<String> hiddenFields, Map<String, String> aliasName)
    //多條數(shù)據(jù)以list獲取
    List<Map<String, Object>> getList()
    //多條數(shù)據(jù)以list獲取,并隱藏不需要顯示的字段
    List<Map<String, Object>> getList(List<String> hiddenFields)
    //多條數(shù)據(jù)以list獲取,并使用別名替換原來(lái)字段
    List<Map<String, Object>> getList(Map<String, String> aliasName)
    //多條數(shù)據(jù)以list獲取,隱藏不需要顯示的字段,使用別名替換原來(lái)字段
    List<Map<String, Object>> getList(List<String> hiddenFields, Map<String, String> aliasName)
    //單條數(shù)據(jù)轉(zhuǎn)為對(duì)象
    <T> T as(Class<T> beanClass)
    //多條數(shù)據(jù)轉(zhuǎn)為對(duì)象
    <T> List<T> asList(Class<T> beanClass)
    //獲取分頁(yè)信息
    Map<String, Object> getPage()

使用示例

1 User表結(jié)構(gòu)

    CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL COMMENT '用戶名',
  `pwd` varchar(50) DEFAULT NULL '密碼'
  `email` varchar(30) DEFAULT NULL '郵箱',
  `status` int(11) DEFAULT '0' COMMENT '1正常0禁用',
  `age` int(11) DEFAULT NULL '年齡',
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_user_userName` (`userName`)
  ) ENGINE=MyISAM AUTO_INCREMENT=167 DEFAULT CHARSET=utf8;

2 User.class對(duì)象

    import java.io.Serializable;

    public class User implements Serializable {

    private static final long serialVersionUID = 4799201163494761002L;

    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String PWD = "pwd";
    public static final String EMAIL = "email";
    public static final String STATUS = "status";
    public static final String AGE = "age";

    private int id;
    private String name;
    private String pwd;
    private String email;
    private int status;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
 }

3 BaseDal注入

  • 注解方式

    import cn.uncode.dal.core.BaseDAL;
    
    @Service
    public class UserService implements IUserService{
    
        @Autowired
        private BaseDAL baseDAL;
    
        .....
    }
    
  • XML方式

    <bean id="userService" class="cn.uncode.web.service.UserService">
        <property name="baseDAL" ref="baseDAL"></property>
    </bean>
    

4 無(wú)DTO示例

  • 列表查詢

    • 示例1

      //創(chuàng)建查詢對(duì)象
      QueryCriteria queryCriteria = new QueryCriteria();
      //沒有DTO對(duì)象時(shí)可以直接設(shè)置表名
      //queryCriteria.setTable("user");
      //創(chuàng)建條件封裝對(duì)象
      Criteria critera = queryCriteria.createCriteria();
      //設(shè)置條件
      critera.append(Condition.GREATER_THAN, "age", 18).append("status", 1);
      //critera.andColumnGreaterThan(“age”, 18);
      //critera.andColumnEqualTo("status", 1);
      //查詢所有字段并緩存
      QueryResult result =  baseDAL.selectByCriteria(queryCriteria);
      //查詢部分字段緩存60稱
      QueryResult result =  baseDAL.selectByCriteria(new String[]{"name","pwd"},queryCriteria, 60);
      List<Map<String, Object>> users = result.getList();
      //也可以轉(zhuǎn)成對(duì)應(yīng)的DTO列表
      List<User> users = result.asList(User.class);
      
    • 示例2

      //創(chuàng)建查詢對(duì)象
      QueryCriteria queryCriteria = new QueryCriteria();
      //設(shè)置查詢表,這樣設(shè)置時(shí)類名必須與表名一致
      queryCriteria.setTable(“user”);
      //設(shè)置分頁(yè)
      queryCriteria.setPageIndex(1);
      queryCriteria.setPageSize(20);
      //設(shè)置排序
      queryCriteria.setOrderByClause("age desc");
      //分頁(yè)查詢并緩存60稱
      QueryResult result =  baseDAL.selectPageByCriteria(queryCriteria, 60);
      List<Map<String, Object>> list = result.getList();
      Map<String, Object>  page = result.getPage();
      
  • 主鍵查詢

    • 示例1

      Map<String, Object> user = new HashMap<String, Object>();
      user.put("id", 1);
      //緩存20秒
      QueryResult result =  baseDAL.selectByPrimaryKey(user, 20);
      user = result.get();
      //也可以傳為DTO對(duì)象
      User dto = result.as(User.class);
      
    • 示例2

      //顯示部分字段,不使用緩存
      QueryResult result =  baseDAL.selectByPrimaryKey(new String[]{"name","pwd"},"user", 1,BaseDal.NO_CAHCE);
      Map<String, Object> user = result.get();
      
    • 示例3

      //不使用DTO對(duì)象,直接傳表名
      QueryResult result =  baseDAL.selectByPrimaryKey(”user“, 1,BaseDal.NO_CAHCE);
      Map<String, Object> user = result.get();
      
    • 示例4

      //聯(lián)合主鍵查詢
      QueryCriteria queryCriteria = new QueryCriteria();
      queryCriteria.setTable(“user”);
      Criteria critera = queryCriteria.createCriteria();
      critera.andColumnEqualTo("name", "uncode-dal");
      critera.andColumnEqualTo("pwd", "uncode-dal-mybatis");
      queryCriteria.setSelectOne(true);
      QueryResult result =  baseDAL.selectByCriteria(queryCriteria);
      Map<String, Object> user = result.get();
      
  • 插入

    • 示例1

      Map<String, Object> user = new HashMap<String, Object>();
      user.put("name", "uncode");
      user.put("pwd", faster");
      //第一個(gè)參數(shù)為表名
      int result = baseDAL.insert("user", user);
      
  • 更新

    • 示例1

      Map<String, Object> user = new HashMap<String, Object>();
      user.put("name", "uncode-dal-mybatis");
      user.setName("uncode-dal-mybatis");
      QueryCriteria queryCriteria = new QueryCriteria();
      queryCriteria.setTable("user");
      Criteria critera = queryCriteria.createCriteria();
      critera.andColumnEqualTo("name", "uncode");
      int result = baseDAL.updateByCriteria(user, queryCriteria);
      
    • 示例2

      Map<String, Object> user = new HashMap<String, Object>();
      user.put("email", "ywj_316@qq.com");
      user.put("id", 1);
      //第一個(gè)參數(shù)為表名,user中必須包括所有主鍵字段
      int result = baseDAL.updateByPrimaryKey("user", user);
      
    • 示例3

      Map<String, Object> user = new HashMap<String, Object>();
      //設(shè)置為null
      user.put("email", null);
      user.put("id", 1);
      //第一個(gè)參數(shù)為表名,user中必須包括所有主鍵字段
      int result = baseDAL.updateByPrimaryKey("user", user);
      
    • 示例4

      Map<String, Object> user = new HashMap<String, Object>();
      //設(shè)置函數(shù)
      user.put("email", "=concat('11','22')");
      user.put("age", "=age+1");
      user.put("id", 1);
      //第一個(gè)參數(shù)為表名,user中必須包括所有主鍵字段
      int result = baseDAL.updateByPrimaryKey("user", user);
      
  • 刪除

    • 示例1

      Map<String, Object> user = new HashMap<String, Object>();
      user.put("email", "ywj_316@qq.com");
      user.put("id", 1);
      //user中必須包括所有主鍵字段
      int result = baseDAL.deleteByPrimaryKey("user", user);
      
    • 示例2

      //第一個(gè)參數(shù)為表名
      int result = baseDAL.deleteByPrimaryKey(“user”, 165);
      
    • 示例3

      QueryCriteria queryCriteria = new QueryCriteria();
      queryCriteria.setTable("user");
      Criteria critera = queryCriteria.createCriteria();
      critera.andColumnEqualTo("name", "uncode");
      int result = baseDAL.deleteByCriteria(queryCriteria);
      
  • 其他操作

    • 示例1

      QueryCriteria queryCriteria = new QueryCriteria();
      queryCriteria.setTable("user");
      Criteria critera = queryCriteria.createCriteria();
      critera.andColumnGreaterThan("age", 18);
      critera.andColumnEqualTo("status", 1);
      //數(shù)量統(tǒng)計(jì)
      int result =  baseDAL.countByCriteria(queryCriteria);
      

5 使用DTO示例

  • 列表查詢

    • 示例1

      //創(chuàng)建查詢對(duì)象
      QueryCriteria queryCriteria = new QueryCriteria();
      //設(shè)置查詢表,這樣設(shè)置時(shí)類名必須與表名一致
      queryCriteria.setTable(User.class);
      //創(chuàng)建條件封裝對(duì)象
      Criteria critera = queryCriteria.createCriteria();
      //設(shè)置條件
      critera.append(Condition.GREATER_THAN, "age", 18).append("status", 1);
      //critera.andColumnGreaterThan(User.AGE, 18);
      //critera.andColumnEqualTo(User.STATUS, 1);
      //查詢所有字段并緩存
      QueryResult result =  baseDAL.selectByCriteria(queryCriteria);
      //查詢部分字段不緩存
      QueryResult result =  baseDAL.selectByCriteria(new String[]{"name","pwd"},queryCriteria, BaseDal.NO_CACHE);
      List<User> users = result.asList(User.class);
      
    • 示例2

      //創(chuàng)建查詢對(duì)象
      QueryCriteria queryCriteria = new QueryCriteria();
      //設(shè)置查詢表,這樣設(shè)置時(shí)類名必須與表名一致
      queryCriteria.setTable(User.class);
      //設(shè)置分頁(yè)
      queryCriteria.setPageIndex(1);
      queryCriteria.setPageSize(20);
      //設(shè)置排序
      queryCriteria.setOrderByClause(User.AGE+" desc");
      //分頁(yè)查詢并緩存60稱
      QueryResult result =  baseDAL.selectPageByCriteria(queryCriteria, 60);
      List<User> users = result.asList(User.class);
      Map<String, Object>  page = result.getPage();
      
  • 主鍵查詢

    • 示例1

      User user = new User();
      user.setId(1);
      //緩存20秒,有多個(gè)主鍵時(shí)必須全部設(shè)值
      QueryResult result =  baseDAL.selectByPrimaryKey(user, 20);
      user = result.as(User.class);
      
    • 示例2

      //顯示部分字段,不使用緩存
      QueryResult result =  baseDAL.selectByPrimaryKey(new String[]{"name","pwd"},User.class, 1,BaseDal.NO_CAHCE);
      User user = result.as(User.class);
      
    • 示例3

      //聯(lián)合主鍵也可以使用這種方式查詢
      QueryCriteria queryCriteria = new QueryCriteria();
      queryCriteria.setTable(User.class);
      Criteria critera = queryCriteria.createCriteria();
      critera.andColumnEqualTo(User.NAME, "uncode-dal");
      critera.andColumnEqualTo(User.PWD, "uncode-dal-mybatis");
      queryCriteria.setSelectOne(true);
      QueryResult result =  baseDAL.selectByCriteria(queryCriteria);
      User user = result.as(User.class);
      
  • 插入

    • 示例1

      User user = new User();
      user.setName("uncode");
      user.setPwd("faster");
      int result = baseDAL.insert(user);
      
  • 更新

    • 示例1

      User user = new User();
      user.setName("uncode-dal-mybatis");
      QueryCriteria queryCriteria = new QueryCriteria();
      queryCriteria.setTable(User.class);
      Criteria critera = queryCriteria.createCriteria();
      critera.andColumnEqualTo(User.NAME, "uncode");
      int result = baseDAL.updateByCriteria(user, queryCriteria);
      
    • 示例2

      User user = new User();
      user.setEmail("ywj_316@qq.com");
      user.setId(1);
      //user中必須設(shè)值所有主鍵字段
      int result = baseDAL.updateByPrimaryKey(user);
      
  • 刪除

    • 示例1

      User user = new User();
      user.setId(1);
      //user中必須設(shè)值所有主鍵字段
      int result = baseDAL.deleteByPrimaryKey(user);
      
    • 示例2

      int result = baseDAL.deleteByPrimaryKey(User.class, 165);
      
    • 示例3

      QueryCriteria queryCriteria = new QueryCriteria();
      queryCriteria.setTable(User.class);
      Criteria critera = queryCriteria.createCriteria();
      critera.andColumnEqualTo(User.NAME, "uncode");
      int result = baseDAL.deleteByCriteria(queryCriteria);
      
  • 其他操作

    • 示例1

      QueryCriteria queryCriteria = new QueryCriteria();
      queryCriteria.setTable(User.class);
      Criteria critera = queryCriteria.createCriteria();
      critera.andColumnGreaterThan(User.AGE, 18);
      critera.andColumnEqualTo(User.STATUS, 1);
      //數(shù)量統(tǒng)計(jì)
      int result =  baseDAL.countByCriteria(queryCriteria);
      

版權(quán)

作者:冶衛(wèi)軍(ywj_316@qq.com,微信:yeweijun)

技術(shù)支持QQ群:47306892

Copyright 2013 www.uncode.cn

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)