java - 无法理解我的 Spring DTO 的工作原理

标签 java mysql spring mybatis

我目前正在开发一个由第三方承包商构建的遗留项目。它是一个使用 Spring 和 MyBatis 并与 MySQL 服务器通信的 Java 服务器。

我正在尝试在我的 DTO getter/setter 中重构一些丑陋的方法名称,其中包含 CamelCase 和下划线。我将使用方法 getCommon_name() 作为示例。当我将其重命名为 getCommonName()) 时,该方法的数据库查找将停止工作。我尝试进行文本搜索来查找调用该方法的代码中出现的情况,但它似乎只存在于 DTO 定义中。可能存在一些我无法正确理解的数据库自动映射,因为该方法查找名为 common_name 的表,但我无法确定。

有人能够阐明可能发生的事情吗?这是我第一次使用 Spring/MyBatis。

编辑更多详细信息:

到目前为止,答案一直在询问我的映射是如何设置的。它看起来像这样:

mybatis-config.xml

<configuration>
  <typeAliases>
    <typeAlias type="com.example.dto.SpeciesDTO" alias="species" />
    <!--aliases for other DTOs-->
  </typeAliases>

  <mappers>
    <mapper resource="com/example/dao/species/speciesSQL.xml" />
    <!--aliases for other DAOs-->
  </mappers>
</configuration>

mybatis-context.xml

<beans>
  <bean id="SpeciesDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface"
        value="com.example.dao.species.SpeciesDAO" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
  </bean>
  <!--beans for other DAOs-->
</beans>

此外,DAO 文件都具有关联的 .xml 文件,如下所示:

<mapper namespace="com.example.species.speciesDAO">
  <select id="getSpecies" parameterType="String" resultType="species">
    SELECT * FROM SPECIES
  </select>
  <!--other methods executing SQL-->
</mapper>

我还没有看到任何像 DAO 方法那样显式映射 DTO getter/setter 的内容。例如,如果我搜索方法 getCommon_name(),则唯一出现的情况是 DTO 本身中的定义以及在我的一项服务中对该方法的调用(我确保在以下情况下编辑这两个出现的情况):重命名该方法)。该方法所做的只是返回一个名为 common_name 的属性。

最佳答案

是否有 mybatis-config.xml 或其他 xml 映射文件正在设置名称约定?例如,来自here :

<mapper namespace="org.podcastpedia.dao.PodcastDao">
<!--    result maps     -->
<resultMap id="podcastsMap" type="Podcast" >
    <id column="podcast_id" property="podcastId"  />
    <result column="url" property="url" />
    <result column="rating" property="rating" />
    <result column="numberRatings" property="number_ratings" />
    <result column="number_visitors" property="numberOfVisitors" />
    <result column="DESCRIPTION" property="description" />
    <result column="PODCAST_IMAGE_URL" property="urlOfImageToDisplay" />
    <result column="TITLE" property="title" />
    <result column="last_episode_url" property="lastEpisodeMediaUrl" />
    <result column="title_in_url" property="titleInUrl" />
    <result column="publication_date" property="publicationDate"/>
</resultMap>

关于java - 无法理解我的 Spring DTO 的工作原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41111906/

相关文章:

java - 在 JavaFX 8 中管理多线程的最佳方式是什么?

java - 使用 Spring RestTemplate 对对象进行 POST 参数

java - 如何在 Java/Android 中重用内部类?

java - 从客户端应用程序(React Js+Node Js)向 Java Web 服务发出请求

mysql - 删除 Mysql 值

php - 如何在php中搜索查询?

MySQL内连接问题

java - Controller 方法上的自定义 validator

java - AWS Lambda 与 Java Spring

java - 分割字符串而不丢失分割字符