逆向工程 生成mapper,dao,entity(使用教程--超詳細)

項目地址: https://gitee.com/manyundemimang/db-generate

快速使用

1.修改generator.properties配置文件中,連接數據庫的配置信息

jdbc.mysql.driverClass=com.mysql.cj.jdbc.Driver
jdbc.mysql.connectionURL=jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.mysql.userId=root
jdbc.mysql.password=root

jdbc.orcal.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.orcal.connectionURL=jdbc:oracle:thin:@192.168.0.111:1521:orcl
jdbc.orcal.userId=childim
jdbc.orcal.password=childim

2.resources目錄下有兩個配置文件,

mysql數據庫使用generatorConfig.xml

orcale數據庫使用generatorConfig1.xml

修改配置文件中,生成文件目錄,設置成項目所在目錄

      

3.運行Generator.java中的main方法(注意使用mysql要傳入的是/generatorConfig.xml)

這樣我們就完成了逆向工程,幫我們生成了,dao,entity,mapper

 

在項目中使用

1.我們可以將這些生成的文件複製到我們的項目中,但是這要注意修改到中引入實體類的路徑,還有mapper中映射類的路徑

2.可以直接在別項目中,通過maven引入這個項目打成的jar包。如果是maven的多模塊項目,可直接引入這個逆向工程的模塊。

然後在掃描mapper文件,和dao文件時要把這個工程的也給加上去:

在application.yml文件中加

mybatis:
  mapper-locations:
    - classpath:dao/*.xml  ##這個是項目mapper文件位置
    - classpath*:mybatis/mapper/*.xml  ##這個就是掃描逆向工程中的mapper文件

創建MyBatisConfig類

/**
 * MyBatis配置類
 */
@Configuration
@EnableTransactionManagement
//第一個爲項目dao的位置,第二個是逆向工程生成dao位置
@MapperScan({"com.hn.shopadmin.dao", "com.hn.dbgenerate.mapper"})
public class MyBatisConfig {
}

具體使用

看給我們生成的entity的文件中,除了生成了DeptDict.java這個實體類文件,還有一個DeptDictExample.java類,這個類呢就是操作DeptDict表的條件生成類,可以看到mapper文件中很多方法的參數也都是這個類。

這裏先記住這些Example結尾的都是條件類就行,具體使用後面再說。我們先說說這些方法都是,幹啥的,看他的方法名稱也很明瞭。

countByExample 根據條件統計數量;

deleteByExample 根據條件刪除數據;

deleteByPrimaryKey 根據主鍵刪除數據;

insert 和 insertSelective都是插入數據,他兩者區別insert方法會實體中所有字段進行插入,而insertSelective只會將值不爲空的字段進行插入,一般使用insertSelective

selectByExample  根據條件查詢列表

selectByExampleWithBLOBs 這個方法只有在數據庫存在二進制類型的字段纔會生成,selectByExample 方法查詢的內容的不包含二進制字段,這個方法包含

selectByPrimaryKey 根據主鍵查詢信息

updateByExampleSelective 根據條件修改指定數據,一般情況就使用這個

updateByExample 根據條件修改數據,這個方法和updateByExampleSelective方法區別在於,這個方法會將所有字段進行修改,如果傳的是空,那麼就將字段修改爲空,而updateByExampleSelective只會修改非空的字段

updateByExampleWithBLOBs 相對於updateByExample 方法,它會修改二進制文件

updateByPrimaryKeySelective,updateByPrimaryKeyWithBLOBs,updateByPrimaryKey  這三和方法和上面三個類似,只是將條件修改爲主鍵

 

條件類的使用:以DeptDictExample.java舉例

實例:

@Service
public class DeptDictServiceimpl implements DeptDictService {
    @Autowired
    DeptDictMapper deptDictMapper;
    
    public void select() {
        String str = "111";
        String str2 = "222";
        DeptDictExample deptDictExample = new DeptDictExample();
        deptDictExample.setOrderByClause("dept_code desc");//order by dept_code desc

        deptDictExample.createCriteria()
                .andDeptCodeEqualTo(str) //dept_code = '111'
                .andDeptNameEqualTo(str);//(dept_code = '111' and dept_name ='111')

        deptDictExample.or() //(dept_code = '111' and dept_name ='111') or
                .andDeptCodeEqualTo(str2) //(dept_code = '111' and dept_name ='111') or dept_code = '222'
                .andDeptNameEqualTo(str2);//(dept_code = '111' and dept_name ='111') or (dept_code = '222' and dept_name ='222')

        //select * from detp_dict where (dept_code = '111' and dept_name ='111') or (dept_code = '222' and dept_name ='222') order by dept_code desc
        List<DeptDict> deptDicts = deptDictMapper.selectByExample(deptDictExample);
    }
}

方法講解:

            String str = "111";
            String str2 = "222";
            List<String> strs = new ArrayList<String>();
            strs.add(str);
            strs.add(str2);
            //創建條件類
            DeptDictExample deptDictExample = new DeptDictExample();
            //這類主要包含以下
            //設置是否去重
            deptDictExample.setDistinct(true);
            //設置排序
            deptDictExample.setOrderByClause("deptCode desc");
            //創建一個條件實體類,這個類是DeptDictExample的內部類,這個類裏爲每個字段創建了以下幾個方法
            DeptDictExample.Criteria criteria = deptDictExample.createCriteria();

            criteria.andClinicAttrEqualTo(str); //and Clinic_Attr = "111"
            criteria.andClinicAttrNotEqualTo(str);//and Clinic_Attr != "111"

            criteria.andClinicAttrGreaterThan(str);//and Clinic_Attr > "111"
            criteria.andClinicAttrGreaterThanOrEqualTo(str);//and Clinic_Attr >= "111"

            criteria.andClinicAttrLessThan(str);//and Clinic_Attr < "111"
            criteria.andClinicAttrLessThanOrEqualTo(str);//and Clinic_Attr <= "111"

            criteria.andClinicAttrLike("%" + str + "%"); //and Clinic_Attr like "%111%"
            criteria.andClinicAttrNotLike("%" + str + "%");//and Clinic_Attr not like "%111%"

            criteria.andClinicAttrIsNull();//and Clinic_Attr is null
            criteria.andClinicAttrIsNotNull();//and Clinic_Attr is not null

            criteria.andClinicAttrIn(strs);//and Clinic_Attr in('111','222')
            criteria.andClinicAttrNotIn(strs);//and Clinic_Attr not in ('111','222')

            criteria.andClinicAttrBetween(str,str2);//and Clinic_Attr BETWEEN '111' AND '222'
            criteria.andClinicAttrNotBetween(str,str2);//and Clinic_Attr not BETWEEN '111' AND '222'

            //拼接一個or 的條件
            DeptDictExample.Criteria or = deptDictExample.or();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章