mybtis自动生成Example详解

在进行表关联的时候,tb_goods  主键id    tb_goods_desc 主键goods_id  现在要向tb_goods_desc里面插一条记录的时候,这个表的goods_id要从哪里来呢,   就是从添加的tb_goods 这个表的id  那么怎么能获得这个id呢 用selectKey就可以实现

由于我们需要在商品表添加数据后可以得到自增的ID,所以我们需要在TbGoodsMapper.xml中的insert配置中添加如下配置

    <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">

SELECT LAST_INSERT_ID() AS id

</selectKey>

加上这句代码之后,意义就在于你执行完insert语句之后,就能得到这个id


<?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="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/pinyougoudb" userId="root"
password="root">
</jdbcConnection>
<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
userId="yycg"
password="yycg">
</jdbcConnection> -->


<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>


<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.pinyougou.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.pinyougou.mapper" 
targetProject=".\resource">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.pinyougou.mapper" 
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="tb_address"></table>
<table schema="" tableName="tb_areas"></table>
<table schema="" tableName="tb_brand"></table>
<table schema="" tableName="tb_cities"></table>
<table schema="" tableName="tb_content"></table>
<table schema="" tableName="tb_content_category"></table>
<table schema="" tableName="tb_freight_template"></table>
<table schema="" tableName="tb_goods"></table>
<table schema="" tableName="tb_goods_desc"></table>
<table schema="" tableName="tb_item"></table>
<table schema="" tableName="tb_item_cat"></table>
<table schema="" tableName="tb_item_spec_option"></table>
<table schema="" tableName="tb_order"></table>
<table schema="" tableName="tb_order_item"></table>
<table schema="" tableName="tb_pay_log"></table>
<table schema="" tableName="tb_provinces"></table>
<table schema="" tableName="tb_seller"></table>
<table schema="" tableName="tb_specification"></table>
<table schema="" tableName="tb_specification_option"></table>
<table schema="" tableName="tb_type_template"></table>
<table schema="" tableName="tb_user"></table>

<!-- 有些表的字段需要指定java类型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->


<!-- 要对那些数据表进行生成操作,必须要有一个.
        <table schema="mybatis" tableName="category" domainObjectName="Category" 
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">     
        </table>


当我们需要生成example类的时候,需要table里面去掉


enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"  

TbItemCatExample example=new TbItemCatExample();
example.or()
.andNameEqualTo("胡斌")
.andIdIsNull();

example.or()
.andNameNotEqualTo("胡斌")
.andIdIsNotNull();

List nameField = new ArrayList();
nameField.add(8);
nameField.add(18);
nameField.add(28);
nameField.add(38);

example.or()
.andNameIn(nameField);

example.or()
.andNameBetween("3", "7");
上面的例子中動態生成的where子句是
where(name=胡斌 and id is null)
or(name<> 胡斌 and id is not null)
or(name in(8,18,28,38))
or(name between 3 and 7)
将会返回满足这些条件的记录结果 
Criteria,包含一个Cretiron的集合,每一个Criteria对象内包含的Cretiron之间是由AND连接的,是逻辑与的关系。
oredCriteria,Example内有一个成员叫oredCriteria,是Criteria的集合,就想其名字所预示的一样,这个集合中的Criteria是由OR连接的,
是逻辑或关系。oredCriteria就是ORed Criteria。
or()方法,会产生一个新的Criteria对象,添加到oredCriteria中,并返回这个Criteria对象,从而可以链式表达,为其添加Criterion。
查询条件1:a=? and (b=? or c=?) 不支持


查询条件2:(a=? And b=?) or (a=? And c=?) 支持




DemoExample example=new DemoExample();  
 
  DemoExample.Criteria criteria1=example.createCriteria();  
  criteria1.andAEqualTo(?).andBEqualTo(?);  
          
  DemoExample.Criteria criteria2=example.createCriteria();  
  criteria2.andAEqualTo(?).andCEqualTo(?);  
 
  example.or(criteria2);  
 
  SqlSession sqlSession = MyBatisUtil.openSession();
  DemoMapper m = sqlSession.getMapper(DemoMapper.class);
  m.countByExample(example);  
  //生成的sql语句
  select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )
 
 
 
  DemoExample example=new DemoExample();  
  
  example.or().andAEqualTo(?).andBEqualTo(?);
  example.or().andAEqualTo(?).andCEqualTo(?); 
  
  SqlSession sqlSession = MyBatisUtil.openSession();
  DemoMapper m = sqlSession.getMapper(DemoMapper.class);
  m.countByExample(example);  
  //生成的sql语句
  select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )
 
 
 



-->



</context>
</generatorConfiguration>

猜你喜欢

转载自blog.csdn.net/sod5211314/article/details/80959275