Mybatis框架代码生成器插件使用说明

插件->代码生成器

1.架构

1)依赖项

	<dependency>
		<groupId>org.mybatis.generator</groupId>
		<artifactId>mybatis-generator-core</artifactId>
		<version>1.3.5</version>
	</dependency>

2)dtd文件
mybatis-generator-config_1_0.dtd
百度网盘:

链接:https://pan.baidu.com/s/1IWsXwiuwhsb_GLGQ6Yvmkw 提取码:==yvnj ==
复制这段内容后打开百度网盘手机App,操作更方便哦

3)包
实体包
持久层

2.配置

<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8"
                        userId="root"
                        password="root">
        </jdbcConnection>
		
	<!-- targetProject:生成VO类的位置 -->
        <javaModelGenerator targetPackage="com.uplooking.pojo"
                            targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="false" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mappers"
                         targetProject=".\src\main\resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.uplooking.dao"
                             targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
		
	<!-- 指定数据库表 ORM-->
        <table schema="" tableName="book" mapperName="BookMapper" domainObjectName="BookVO">
        	<generatedKey column="bid" sqlStatement="MySql" identity="true" />
        	<columnOverride column="bpublic" javaType="java.sql.Timestamp" />
        </table> 
		
		说明:tableName="表名" mapperName="接口名" domainObjectName="实体名"
			<generatedKey column="bid" sqlStatement="MySql" identity="true" /> 自增列
			<columnOverride column="bpublic" javaType="java.sql.Timestamp" /> 自定义类型映射

3.应用

1)(条件)辅助类 -> 将SQL脚本封装为对应方法
	//条件
	andXXXIsNull()	andXXXIsNotNull()
	andXXXEqualTo()	andXXXNotEqualTo()
	andXXXGreaterThan() andXXXGreaterThanOrEqualTo()
	andXXXLessThan() andXXXLessThanOrEqualTo()
	andXXXIn()	andXXXNotIn()
	andXXXBetween()	andXXXNotBetween()
	andXXXLike() andXXXNotLike()
	
	//去重
	distinct	默认:false
	
	//排序
	orderByClause
	
	//分页	自定义设置
	start
	limit


2)insert 添加
	int insert(BookVO record);				->  NULL插入
	int insertSelective(BookVO record);		->  NULL不插入
	
3)delete 删除
	int deleteByPrimaryKey(Integer bid);	-> 根据主键id删除
	int deleteByExample(BookVOExample example);	->设置条件删除 (批量删除)
	
4)update 修改
	//根据主键id修改
	int updateByPrimaryKey(BookVO record);			-> NULL更新
	int updateByPrimaryKeySelective(BookVO record);	-> NULL不更新
	
	//设置条件修改(批量修改)
	int updateByExample(@Param("record") BookVO record, @Param("example") BookVOExample example);
	int updateByExampleSelective(@Param("record") BookVO record, @Param("example") BookVOExample example);
	
5)select 查询
	long countByExample(BookVOExample example);	-> 统计记录数
	BookVO selectByPrimaryKey(Integer bid);		-> 根据主键id查找
	List<BookVO> selectByExample(BookVOExample example);	->根据条件查询(综合查询)	

4.缺陷 (手动完成)

1)分页

public class BookVOExample {
	//分页
	protected Integer start;
	protected Integer limit;
	//省略setter getter
}
<select id="selectByExample" parameterType="com.uplooking.pojo.BookVOExample" resultMap="BaseResultMap">
	.......
	<if test="start != null and limit != null">
	  LIMIT #{start},#{limit}
	</if>
 </select>

2)联表

方案一 : Mapper映射文件是配置表关系 
	优势 : 效率高
	劣势 : 安全性低 和 稳定性低 
方案二 : 业务层手动进行关系处理
	优势 : 稳定性高 和 灵活性高 
	劣势 : 效率低

5.执行生成代码

@Test
	public void test() {
		try {
			List<String> warnings = new ArrayList<String>();
			boolean overwrite = true;
			// 指定 逆向工程配置文件
			InputStream inputStream = Resources.getResourceAsStream("generatorConfig.xml");
			//File configFile = new File("classpath:generatorConfig.xml");
			ConfigurationParser cp = new ConfigurationParser(warnings);
			Configuration config = cp.parseConfiguration(inputStream);
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

源码提取:

链接:https://pan.baidu.com/s/19Hv00mze5VZzcq9j9mnBFA 提取码:tirn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你不懂、、、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值