一、正向工程?
设计UML类图--设计数据库
二、逆向工程
设计数据库---直接java类--UML类图
逆向工程一般使用mybatis-generator来进行--将数据库中的表生产
1.pojo实体类 2.映射文件 3.接口文件 4.Exanmple类【提供了单表的CRUD】
这个第三方托管在github上
官网:MyBatis Generator Core – Introduction to MyBatis Generator![]()
讯享网http://mybatis.org/generator/
三、基于java代码
1.创建配置文件
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generatorconfig_1_0.dtd"> <generatorConfiguration> <!--id: 唯一标识 targetRuntime: 运行目标--> <context id="simple" targetRuntime="MyBatis3"> <!--jdbcConnection:数据库连接信息--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql:///java2201q" userId="root" password=""/> <!-- 配置pojo的实体类的生成规则 targetPackage: 实体类生成的包名 targetProject: 生成到哪个项目中 .\当前项目中 --> <javaModelGenerator targetPackage="com.sofwin.pojo" targetProject=".\src"/> <!-- 配置映射文件的生成规则 targetPackage: 映射文件生成的包名 targetProject: 生成到哪个项目中 .\当前项目中 --> <sqlMapGenerator targetPackage="com.sofwin.mapper" targetProject=".\src"/> <!-- 接口文件的生成规则 targetPackage: 接口文件生成的包名 targetProject: 生成到哪个项目中 .\当前项目中 type;XMLMAPPER xml文件 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.sofwin.mapper" targetProject=".\src"/> <!-- 指定需要逆向的表 可以写多个table标签同时进行逆向工程 --> <table tableName="b_user" /> </context> </generatorConfiguration>
讯享网
2.创建java类进行生成
代码如下(示例):
讯享网public static void main(String[] args) throws Exception{ // 空集合 存放运行时的警告 List<String> warnings = new ArrayList<String>(); // true ==命令-overwrite boolean overwrite = true; // 创建file 将逆向工程的配置文件转为file File configFile = new File("D:\\eclipseworkspace20221\\mybatis5\\sr c\\configer.xml"); // 用于解析配置文件 ConfigurationParser cp = new ConfigurationParser(warnings); // 将file转为configuation配置类型 Configuration config =cp.parseConfiguration(configFile); // 设置是否覆盖 DefaultShellCallback callback =new DefaultShellCallback(overwrite); // 逆向工程的核心类 MyBatisGenerator myBatisGenerator= new MyBatisGenerator(config, callback, warnings); // 核心方法 调用逆向 myBatisGenerator.generate(null); } }
3.配置文件的重要属性

conditional:
1个pojo
一个映射文件
一个接口文件
一个Example类(用于自定义条件的)
flat
跟conditional一样
hierarchical
特殊
表中字段不包含大字段类型
只包含主键属性的类
普通列对应的属性生成一个类 extend主键类
表中字段包含大字段类型(text类型)
只包含主键属性的类
普通列对应的属性生成一个类继承 主键类
blob类型的字段对应的一个类
共有
一个接口文件
一个映射文件
一个Examole类
四、逆向工程的使用
新增
int insert(BUser row);
将表中所以的列全部参入,如果有默认值的列也插入null,默认值无效
int insertSelective(BUser row);
选择性的插入 --只插入非空的字段
删除
int deleteByPrimaryKey(Integer id)
根据主键删除数据
int deleteByExample(BUserExample example)
根据自定义条件删除
更新
int updateByPrimaryKey(BUser row);
根据主键更新 更新所以字段(不更新大字段)
int updateByPrimaryKeySelective(BUser row)
根据主键更新 只更新不为null的字段
intupdateByExample(@Param("row") BUser row, @Param("example") BUserExample example) 根据自定义条件更新,更新所以列
intupdateByExampleSelective(@Param("row") BUser row, @Param("example") BUserExample example) 根据自定义条件更新,更新不为null的列
updateByPrimaryKeyWithBLOBs(BUseWithBLOBs row)
更新所以列 包含大字段
查询
BUser selectByPrimaryKey(Integer id);
通过主键查询(不包含大字段)
List<BUser> selectByExample(BuserExample example );
通过自定义条件查询 example为null--没有条件查询所有
long countByExample(BUserExample example);
根据条件查询总条数
List<BUserWithBLOBs> selectByExampleWithBLOBs(BUserExample example);
查询结果包含blob类型的字段
XXXExample类的重要属性
protected String orderByClause;// 排序字段列表
protected boolean distinct;// 是否去重
protected List<Criteria> oredCriteria;// 多
个查询条件
// 在查询条件外部增加or,默认是and
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
// Criteria作为内部类是当前Example类中的查询条件
public Criteria createCriteria() {
Criteria criteria =
createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
public class TestInsert {
public static void main(String[] args) {
SqlSession session =SqlSessionUtil.getSession();
BUserMapper mapper =session.getMapper(BUserMapper.class);
BUser user = new BUser();
user.setId(8);
user.setPwd("");
BUserExample example = newBUserExample();
// 会自动增加(),会将接下来定义的具体的查询
条件以and的形式放在()中
Criteria criteria =example.createCriteria();
// id =value
// criteria.andIdEqualTo(value)
// // id>value
criteria.andIdGreaterThan(1);
// // id>=value
//
criteria.andIdGreaterThanOrEqualTo(value)
// // where (id=value and id>value and
id>=value)
// // id<value
// criteria.andIdLessThan(value)
// // id<=value
//
criteria.andIdLessThanOrEqualTo(value)
// // id<>value
// criteria.andIdNotEqualTo(value)
// // and id in()
// criteria.andIdIn(values)
// // and id not in
// criteria.andIdNotIn(values)
// // and is null
// criteria.andIdIsNull()
// // and id is not null
criteria.andIdIsNotNull();
// // and id between 1 and 2
// criteria.andIdBetween(value1, value2)
// // and id not between 1 and 2
// criteria.andIdNotBetween(value1,
value2)
// 占位符
// 字符拼接
criteria.andLoginNameLike("%aaa%");
// criteria.andLoginNameNotLike(value)
//(id=2)
Criteria
criteria2=example.createCriteria();
criteria2.andIdEqualTo(2);
example.or(criteria2);
// mybatis会自动增加order by 关键字
example.setOrderByClause("id
desc,login_name asc");
// 去重
example.setDistinct(true);
List<BUser> users =
mapper.selectByExample(example);
System.out.println(users.size());
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/24592.html