MybatisのExampleの使用を記す

9316 ワード

mapperインタフェースの関数と方法
AccountByExample(UserExample example)thorws SQLException条件カウントint deleeteByPrimaryKey(Integer id)thorws SQLException主キー削除int deleeteByExample(UserExample example)thorws SQLException条件クエリーString/integer insert(User record)thorws SQLException挿入データ(戻り値ID)User selectByPrimaryKey(Integer)Key(Integer)thorws SQLExceptionプライマリ・キーを押してListselectByExample(UserExample example)thorws SQLException条件別問合せListselectByExampleWithBLOGS(UserExample example)thorws SQLException条件別問合せ(BLOBフィールドを含む).データ・テーブルのフィールド・タイプにバイナリがある場合にのみ生成されます.int updateByPrimaryKey(User record)thorws SQLExceptionキー更新int updateByPrimaryKeySelective(User record)thorws SQLExceptionキー更新値nullでないフィールドint updateByExample(User record,UserExample example)thorws SQLExceptionキー更新int updateByExample(User record,UserExample example)thorws SQLException条件付き更新値nullでないフィールド
mybatisのインバースエンジニアリングではインスタンスとインスタンスに対応するexampleが生成され、exampleは条件を追加するために使用され、whereの後ろの部分xxxExample example=new xxxExample()に相当する.Criteria criteria = new Example().createCriteria();
example.setOrderByClause(「フィールド名ASC」);昇順配列条件を追加し、DESCは降順exampleである.setDistinct(false)重複を除去し、boolean型、trueは重複しないレコードを選択します.criteria.andXxxxNull追加フィールドxxxがnullの条件criteria.andXxxxNotNull追加フィールドIsxxxがnullでない条件criteria.andXxxEqualTo(value)xxxフィールドがvalue条件criteriaに等しいことを追加する.andXxxNotEqualTo(value)xxxフィールドがvalue条件criteriaに等しくないことを追加します.andXxxGreaterThan(value)追加xxxフィールドはvalue条件criteriaより大きい.andXxx GreaterThanOrEqualTo(value)は、value条件criteria以上のxxxフィールドを追加する.andXxxLessThan(value)xxxフィールドがvalue条件criteriaより小さいことを追加する.andXxxLessThanOrEqualTo(value)xxxフィールドがvalue条件criteria以下であることを追加する.andXxxIn(List)はxxxフィールド値をList条件criteriaに追加する.andXxxNotIn(List)にxxxフィールド値を追加するのはList条件criteriaではない.andXxxLike("%"+value+"%)xxxフィールドの値がvalueのあいまいなクエリー条件criteriaを追加します.andXxxNotLike("%"+value+"%)xxxフィールドの値がvalueでないあいまいなクエリー条件criteriaを追加します.andXxxBetween(value 1,value 2)xxxフィールド値をvalue 1とvalue 2の間に条件criteriaを追加する.andXxxNotBetween(value 1,value 2)xxxフィールド値がvalue 1とvalue 2の間にない条件を追加
1.クエリー
selectByPrimaryKey() User user = XxxMapper.selectByPrimaryKey(100);//select*from user where id=100 1 selectByExample()とselectByExampleWithBLOGS()UserExample example=new UserExample()に相当します.Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo(“wyw”); criteria.andUsernameIsNull(); example.setOrderByClause(“username asc,email desc”); List>list = XxxMapper.selectByExample(example);//「select*from user where username="wyw"and username is null order by username asc,email desc 1 2注:iBator逆方向エンジニアリングで生成するファイルXxxExampleに相当する.JAvaにはstaticの内部クラスCriteriaが含まれています.Criteriaの方法はSQL文whereを定義した後のクエリー条件です.
2.データの挿入
1.insert()
User user = new User(); user.setId(“dsfgsdfgdsfgds”); user.setUsername(“admin”); user.setPassword(“admin”) user.setEmail(“[email protected]”); XxxMapper.insert(user);//insert into user(ID,username,password,email)values(‘dsfgsdfgdsfgds’,‘admin’,‘admin’,‘admin’,‘[email protected]’);
3.データの更新
1.updateByPrimaryKey()
User user =new User(); user.setId(“dsfgsdfgdsfgds”); user.setUsername(“wyw”); user.setPassword(“wyw”); user.setEmail(“[email protected]”); XxxMapper.updateByPrimaryKey(user);//update user set username=‘wyw’,password=‘wyw’,email=‘[email protected]’ where id=‘dsfgsdfgdsfgds’ 2.updateByPrimaryKeySelective()
 	@Override
    @Transactional(transactionManager = "db2DataSourceTransactionManager",timeout=36000,rollbackFor=TerjoyException.class)
    public int modifierPhone(Integer tjid,Integer authtjid,String mobile,Integer type) {
     
        //      
        Validate.isTel(mobile);
        DbBsDriver driver = findByTjidOrMobile(mobile);
        if(null == driver){
     
            //     
            int ret = updateMobile(tjid,mobile);

            //         update db set mobile=#{mobile} where tjid = #{tjid}
            DbUserMobileExample example = new DbUserMobileExample();
            DbUserMobileExample.Criteria criteria = example.createCriteria();
            criteria.andTjidEqualTo(tjid);
            DbUserMobile dbUserMobile = new DbUserMobile();
            dbUserMobile.setMobile(mobile);
            userMobileMapper.updateByExampleSelective(dbUserMobile, example);

            if(null != type && type == 1){
     
                ......
                return 1;
        }
        return 0;
    }

User user = new User(); user.setId(“dsfgsdfgdsfgds”); user.setPassword(“wyw”); XxxMapper.updateByPrimaryKey(user);//update user set password='wyw'where id='dsfgsdsfgds'updateByExample()およびupdateByExampleSelective()UserExample example=new UserExample()に相当します.Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo(“admin”); User user = new User(); user.setPassword(“wyw”); XxxMapper.updateByPrimaryKeySelective(user,example);//update user set password='wyw'where username='admin'updateByExample()はnullのフィールドも含めてすべてのフィールドを更新します.updateByExampleSelective()を使用して更新したいフィールドを更新することをお勧めします.
4.データの削除
1.deleteByPrimaryKey()
XxxMapper.deleteByPrimaryKey(1);//相当:delete from user where id=1
5.問合せデータ数
1.countByExample()
UserExample example = new UserExample(); Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo(“wyw”); int count = XxxMapper.countByExample(example);//相当:select count(*)from user where username=‘wyw’