Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4105460
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:14:59+00:00 2026-05-20T21:14:59+00:00

I’m just starting to learn MyBatis and I’m wondering, when I’m creating insert or

  • 0

I’m just starting to learn MyBatis and I’m wondering, when I’m creating insert or update queries, is there a way that I can make property names a bit more friendly to refactoring? I’ll explain in more detail:

I have constants in all of my domain classes that can be used to reference property names. In my opinion, it cuts down on typos and makes refactoring a bit easier.

public static final String FIRST_NAME = "firstName";
private String firstName = "";

When I create a MyBatis select statement using annotations, I can do something like this:

@Select("SELECT ID, FIRST_NAME, LAST_NAME FROM CUSTOMERS WHERE ID = #{id}")
@Results({
    @Result(property = CustomerDetail.ID, column = "ID"),
    @Result(property = CustomerDetail.FIRST_NAME, column = "FIRST_NAME"),
    @Result(property = CustomerDetail.LAST_NAME, column = "LAST_NAME")
})
CustomerDetail selectById(final int id);

If I refactor my domain object (CustomerDetail) and change property names, it ends up being fairly simple.

However, when I create a MyBatis insert statement using annotations, I have to do something like this:

@Insert("INSERT INTO CUSTOMERS (ID, FIRST_NAME, LAST_NAME) VALUES (#{id}, #{firstName}, #{lastName})")
void insertCustomerDetail(final CustomerDetail customerDetail);

In this case, if I refactor my domain object (CustomerDetail) and change property names, it’s much more error prone. Is there a way I can use my constants without resorting to a bunch of string concatenation? Is it something I should even concern myself with?

As a total newbie, I was expecting the @Insert and @Update annotations to mimic the structure of the @Select annotation. For example (please note, the below are NOT valid MyBatis annotations):

@Insert("INSERT INTO CUSTOMERS (ID, FIRST_NAME, LAST_NAME)")
@Params({
    @Param(property = CustomerDetail.ID, column = "ID"),
    @Param(property = CustomerDetail.FIRST_NAME, column = "FIRST_NAME"),
    @Param(property = CustomerDetail.LAST_NAME, column = "LAST_NAME")
})
void insertCustomerDetail(final CustomerDetail customerDetail);

Have I missed any options that would have the same effect as my above sample? Alternatively, is it possible for me to unit test MyBatis mappings to ensure no one is using property names that don’t exist in my domain objects? Testing may be a better solution since it would also apply to XML based mappings. Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:14:59+00:00Added an answer on May 20, 2026 at 9:14 pm

    Is it something I should even concern
    myself with?

    I don’t think so. I understand your concern, and I see how such a feature could be beneficial to you, especially early in development when POJO’s tend to change so often.

    I don’t think your objects fields will be refactored enough to justify the investment needed to provide this functionality.

    I suggest you read about the @Param tag in the mybatis user guide. It’s not exactly what your looking for here, but it is a way to decouple object field names to mybatis sql map variables. In my mind, I would take your approach over passing in individual feilds with a @Param.

    In regards to unit testing your sql maps, i’m fairly certain that if you use an ognl expression that doesn’t have a corresponding get method in the object you’ll get an exceptions. i.e if you use #{someField}, and the object your passing in doesn’t have a getSomeField() method, then you get exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.