逆向工程 生成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();

 

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