2019-0長文詳解Java逆向工程生成以及作用,附加下載地址8-25

經(jīng)過周末的休息,Damon也是重新更新了。在我們做項目中,關(guān)于持久層的生成相信是許多小伙伴頭疼的事情。并且mybatis是目前很流行的持久層框架,因此有沒有一種辦法能夠減少我們的開發(fā)時間量。
對于上面的疑問自然是有的,那就是逆向工程。對于逆向工程這個概念以及方法其實已經(jīng)出現(xiàn)很長一段時間,其存在的目的就是為了我們的開發(fā)時間。所謂mybatis逆向工程,就是mybatis會根據(jù)我們設(shè)計好的數(shù)據(jù)表,自動生成pojo、mapper以及mapper.xml。不用通過自己編寫,依靠插件為我們生成,從而大大減少我們的工作量。接下來就讓Damon來跟大家說說如何操作。

首先從插件的安裝上(資料可以在文章末尾的Github下載):

  • 將features、plugins拷貝到myeclipse10下的dropins文件夾中,如下圖所示,重啟myeclipse。


    image

    -以下以MySQL為例逆向生成映射文件,其中數(shù)據(jù)庫為test表為userinfo,id字段自增。
    創(chuàng)建一新的web project mybatis_generator,并添加sql驅(qū)動jar包。


    image
  • 在src中創(chuàng)建一個MyBatis生成orm文件的配置文件,單擊next,保持默認(rèn)設(shè)置finish即可。


    image
  • 對生成的generatorConfig.xml文件進(jìn)行更改。
        <?xml version="1.0" encoding="UTF-8" ?>  
        <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >  
        <generatorConfiguration >  
            <context id="mybatis_generator" >  
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"   
               connectionURL="jdbc:mysql://localhost:3306/test"   
                userId="root" password="root" />  
        <javaModelGenerator targetPackage="orm" targetProject="mybatis_generator" />  
  <sqlMapGenerator targetPackage="orm" targetProject="mybatis_generator" />  
           <javaClientGenerator targetPackage="orm" targetProject="mybatis_generator"   
                type="XMLMAPPER" />  
      <table schema="test" tableName="userinfo" >  
             <generatedKey column="id" sqlStatement="mysql" identity="true" />  
            </table>  
          </context>  
        </generatorConfiguration>  
  • 注:context id、targetProject均為項目名稱,generatorKey標(biāo)簽配置id字段自增。
  • 右鍵配置文件,選擇Generator MyBatis/iBATIS Artifacts即可生成orm映射文件
    ![avatar]


    image
  • 如果出現(xiàn)錯誤:Unexpected error while running MyBatis Generator. Exception getting JDBC Driver
  • 就把mysql-connector-java-5.0.5-bin.jar添加到Referenced Libraries中。

接下來就說說生成的mapper接口方法的解析,大家好好理解下。

  • mapper接口中的函數(shù)及方法

一:方法功能說明

  • int countByExample(UserExample example) thorws SQLException 按條件計數(shù)
  • int deleteByPrimaryKey(Integer id) thorws SQLException 按主鍵刪除
  • int deleteByExample(UserExample example) thorws SQLException 按條件查詢
  • String/Integer insert(User record) thorws SQLException 插入數(shù)據(jù)(返回值為ID)
  • User selectByPrimaryKey(Integer id) thorws SQLException 按主鍵查詢
  • ListselectByExample(UserExample example) thorws SQLException 按條件查詢
  • ListselectByExampleWithBLOGs(UserExample example) thorws SQLException 按條件查詢(包括BLOB字段)。只有當(dāng)數(shù)據(jù)表中的字段類型有為二進(jìn)制的才會產(chǎn)生。
  • int updateByPrimaryKey(User record) thorws SQLException 按主鍵更新
  • int updateByPrimaryKeySelective(User record) thorws SQLException 按主鍵更新值不為null的字段
  • int updateByExample(User record, UserExample example) thorws SQLException 按條件更新
  • int updateByExampleSelective(User record, UserExample example) thorws SQLException 按條件更新值不為null的字段

二:example實例解析

  • mybatis的逆向工程中會生成實例及實例對應(yīng)的example,example用于添加條件,相當(dāng)where后面的部分
  • xxxExample example = new xxxExample();
    Criteria criteria = new Example().createCriteria();
  • 方法 說明
  • example.setOrderByClause(“字段名 ASC”); 添加升序排列條件,DESC為降序
  • example.setDistinct(false) 去除重復(fù),boolean型,true為選擇不重復(fù)的記錄。
  • criteria.andXxxIsNull 添加字段xxx為null的條件
  • criteria.andXxxIsNotNull 添加字段xxx不為null的條件
  • criteria.andXxxEqualTo(value) 添加xxx字段等于value條件
  • criteria.andXxxNotEqualTo(value) 添加xxx字段不等于value條件
  • criteria.andXxxGreaterThan(value) 添加xxx字段大于value條件
  • criteria.andXxxGreaterThanOrEqualTo(value) 添加xxx字段大于等于value條件
  • criteria.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(value1,value2) 添加xxx字段值在value1和value2之間條件
  • criteria.andXxxNotBetween(value1,value2) 添加xxx字段值不在value1和value2之間條件

三:應(yīng)用舉例

  • 1.查詢

  • ① selectByPrimaryKey()

  • User user = XxxMapper.selectByPrimaryKey(100); //相當(dāng)于select * from user where id = 100

  • ② 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);
    //相當(dāng)于:select * from user where username = 'wyw' and username is null order by username asc,email desc

  • 注:在iBator逆向工程生成的文件XxxExample.Java中包含一個static的內(nèi)部類Criteria,Criteria中的方法是定義SQL 語句where后的查詢條件。

  • 2.插入數(shù)據(jù)
  • ①insert()

  • User user = new User();

  • user.setId("dsfgsdfgdsfgds");

  • user.setUsername("admin");

  • user.setPassword("admin")

  • user.setEmail("wyw@163.com");

  • XxxMapper.insert(user);

  • //相當(dāng)于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','wyw@126.com');

  • 3.更新數(shù)據(jù)
  • ①updateByPrimaryKey()

  • User user =new User();

  • user.setId("dsfgsdfgdsfgds");

  • user.setUsername("wyw");

  • user.setPassword("wyw");

  • user.setEmail("wyw@163.com");

  • XxxMapper.updateByPrimaryKey(user);

  • //相當(dāng)于:update user set username='wyw', password='wyw', email='wyw@163.com' where id='dsfgsdfgdsfgds'

  • ②updateByPrimaryKeySelective()

  • User user = new User();

  • user.setId("dsfgsdfgdsfgds");

  • user.setPassword("wyw");

  • XxxMapper.updateByPrimaryKey(user);

  • //相當(dāng)于:update user set password='wyw' where id='dsfgsdfgdsfgds'

  • ③ updateByExample() 和 updateByExampleSelective()

  • UserExample example = new UserExample();

  • Criteria criteria = example.createCriteria();

  • criteria.andUsernameEqualTo("admin");

  • User user = new User();

  • user.setPassword("wyw");

  • XxxMapper.updateByPrimaryKeySelective(user,example);

  • //相當(dāng)于:update user set password='wyw' where username='admin'

  • updateByExample()更新所有的字段,包括字段為null的也更新,建議使用 updateByExampleSelective()更新想更新的字段

4.刪除數(shù)據(jù)

  • ①deleteByPrimaryKey()

  • XxxMapper.deleteByPrimaryKey(1); //相當(dāng)于:delete from user where id=1

  • ②deleteByExample()

  • UserExample example = new UserExample();

  • Criteria criteria = example.createCriteria();

  • criteria.andUsernameEqualTo("admin");

  • XxxMapper.deleteByExample(example);

  • //相當(dāng)于:delete from user where username='admin'
    數(shù)量

  • ①countByExample()

  • UserExample example = new UserExample();

  • Criteria criteria = example.createCriteria();

  • criteria.andUsernameEqualTo("wyw");

  • int count = XxxMapper.countByExample(example);

  • //相當(dāng)于:select count(*) from user where username='wyw'

總結(jié):

作為一個新生程序猿,Damon希望能夠與大家一同進(jìn)步。文章或者描述有所不足的地方,希望大家多多提出來,一同進(jìn)步。

Damon會繼續(xù)發(fā)掘一些有用的咨詢,知識以及新工具,與大家一同分享,謝謝!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容