我正在使用Mybatis(用于持久化java到数据库)和Mybatis Generator(用于从数据库模式自动生成映射器xml文件和java接口)的项目.
Mybatis生成器在生成基本crud操作所需的文件方面做得很好.
上下文
对于某些表/类,我们需要比MyBatis Generator工具生成的"crud stuff"更多的"东西"(代码查询等).
有没有办法拥有"两全其美",即使用自动生成以及"自定义代码".如何分离和构建"手动编辑的文件"和"自动生成的文件".
提案
我正在考虑以下内容,即表格"Foo"
自动生成
(其中"Crud"代表"创建读取更新删除")
手编辑
概念:如果架构发生了变化,您可以随时安全地自动生成"Crud"xml和.java文件,而不会消除任何自定义更改.
问题
mybatis会正确处理这种情况,即这个映射器是否会正确执行自动生成的"crud代码"?
FooMapper fooMapper = sqlSession.getMapper(FooMapper.class);
你推荐什么方法?
编辑1:*我们的数据库设计使用"核心表"("元素"),其他表"扩展"该表并添加额外的属性(共享密钥).我查看了文档和来源得出结论,我不能将Mybatis Generator与这样的'扩展'结合使用而不需要任何手动编辑:
即这不起作用.-ElementMapper扩展"ElementCrudMapper"-FooMapper.xml扩展"ElementCrudMapper"和"FooCrudMapper"
谢谢大家!
我在eclipse中使用MyBatis Generator.这是generatorConfig.xml文件.当我右键单击并选择"生成MyBatis工件"时,它会显示错误消息,如"列名模式不能为NULL或为空".
<?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>
<classPathEntry
location="C:\Users\myplace\.m2\repository\mysql\mysql-connector-java\6.0.2\mysql-connector-java-6.0.2.jar" />
<context id="context1">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/world?serverTimezone=EST"
userId="root" password="root" />
<javaModelGenerator targetPackage="com.example.ws.model" targetProject="com.example.ws" />
<sqlMapGenerator targetPackage="com.example.ws.sql" targetProject="com.example.ws" />
<javaClientGenerator targetPackage="com.example.ws.mapper" targetProject="com.example.ws" type="ANNOTATEDMAPPER" />
<table schema="world" tableName="city">
<columnOverride column="ID" property="id" javaType = "Integer" />
<columnOverride column="Name" property="name" javaType = "String" />
<columnOverride column="CuntryCode" property="cuntryCode" javaType = "String" />
<columnOverride column="District" property="district" javaType = "String" />
<columnOverride column="Population" property="population" javaType …Run Code Online (Sandbox Code Playgroud) 根据DDD,有些类是实体,有些类具有@javax.persistence.Entity注释。他们应该是同一班吗?还是JPA实体应仅充当映射器(https://martinfowler.com/eaaCatalog/dataMapper.html)从数据库加载DDD实体(并将其存储)并保留在域模型之外的机制?
如果将数据库元数据分离并存储在外部(例如,以XML格式),会有所不同吗?如果此类是实体,边界在哪里?我认为从XSD(例如,使用JAXB)甚至使用MyBatis Generator从数据库生成的类都不是DDD中所理解的实体。
我们正在使用MyBatis 3.0.6来处理我们的数据库.
MyBatis生成器将自动生成4个updateByXYZ()方法.这些方法中的每一个都返回一个int.
我一直在挖掘,但找不到任何文档告诉我这些方法返回了哪些可能的值.有人知道吗?
我的问题很简单。我在mysql数据库的产品表中有一个列名product_name,但在我的产品类(java)中,在productName中使用了camelcase。MyBatis没有将product_name映射到productName。有什么解决办法吗?我以前在Hibernate中没问题,但是现在我需要使用mybatis进行开发
Mysql 数据库共有 4 个模式,我只想生成一个名为“预订”的指定模式。但它总是为所有模式生成所有表。所以我需要你的帮助。下面是我的 generatorConfig.xml。
<?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="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/booking?useSSL=false"
userId="root"
password="123456">
</jdbcConnection>
<javaModelGenerator targetPackage="com.clycle.booking.entity" targetProject="C:\Users\a243903\projects\booking\webapi\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="entity-mapper" targetProject="C:\Users\a243903\projects\booking\webapi\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="C:\Users\a243903\projects\booking\webapi\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="booking" tableName="%">
</table>
</context>
</generatorConfiguration>
Run Code Online (Sandbox Code Playgroud) 我只是想用Mybatis映射一个布尔值,但是我遇到了问题。首先,我将向您展示涉及的部分:
XML File:
<resultMap id="destinationTypeMap" type="DestinationTypeDTO">
<result property="destinationTypeId" column="education_destination_type_id" javaType="java.lang.Long" jdbcType="NUMERIC"/>
<result property="description" column="description" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result property="available" column="is_available" javaType="boolean" jdbcType="VARCHAR" typeHandler="BooleanHandler"/>
</resultMap>
Run Code Online (Sandbox Code Playgroud)
Java类:
public class DestinationTypeDTO {
private long destinationTypeId;
private String description;
private boolean available;
public long getDestinationTypeId() {
return destinationTypeId;
}
public void setDestinationTypeId(long destinationTypeId) {
this.destinationTypeId = destinationTypeId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) …Run Code Online (Sandbox Code Playgroud) 这不仅仅是一个简单的问题,而且我的英语没有我想要的那么好......我会尽力而为。
\n\n我使用 java 8,在 Postgres 9.6 上使用 Mybatis 3.4.6,我需要执行自定义动态查询。
\n\n在我的 mapper.java 类中,我创建了一个与myBatis SQL Builder 类一起使用的方法
\n\n@SelectProvider(type = PreIngestManager.class, method = "selectPreIngestsSQLBuilder")\n@Results({ @Result(property = "id", column = "id"), @Result(property = "inputPath", column = "input_path"),\n @Result(property = "idCategoriaDocumentale", column = "id_categoria_documentale"), @Result(property = "idCliente", column = "id_cliente"),\n @Result(property = "outputSipPath", column = "output_sip_path"), @Result(property = "esito", column = "esito"),\n @Result(property = "stato", column = "stato"), @Result(property = "pathRdp", column = "path_rdp"),\n @Result(property = "dataInizio", column = "data_inizio"), @Result(property …Run Code Online (Sandbox Code Playgroud) 我正在学习如何使用MyBatis。老实说,我非常喜欢这个框架。它易于使用,并且对它感到满意,因为我可以使用它的sql命令:)我使用MyBatis 3.4.2和PostgreSQL数据库。
例如,我喜欢在插入@SelectKey注释之前执行查询的难易程度。和数据映射就像一个魅力,如果我的接口方法之前添加一些注释,像这样:@Results({ @Result(property = "javaField", column = "database_field", javaType = TypeHandler.class)。
以下是我不喜欢的(希望您能将我带往正确的方向):
(问题1)我有一些查询,这些查询使我可以使用null和正常值,而无需任何其他“ if” java语句来检查变量是否包含null值。他们看起来像这样:
SELECT * FROM table
WHERE key_name = ? AND ((? IS NULL AND user_id IS NULL) OR User_id = ?)
Run Code Online (Sandbox Code Playgroud)
使用JDBC,我需要执行以下操作:
stmt = connection.prepareStatement(query);
stmt.setString(1, "key");
stmt.setString(2, userId);
stmt.setString(3, userId);
Run Code Online (Sandbox Code Playgroud)
如您所见,我需要传递两次userId,因为这是JDBC的工作方式。老实说,我期望下面的代码可以在MyBatis上使用,但是不幸的是它不起作用。仍然需要定义第三个参数。
我想知道是否可以将此功能添加到MyBatis。如果MyBatis可以自动绑定userId两次,那应该没问题,如下所示:
@Select("SELECT * FROM table key_name = #{key} and ((#{userId} is null and user_id is null) OR user_id = #{userId})
SomeClass findByKeyAndUserId(String …Run Code Online (Sandbox Code Playgroud)