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 8499587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:42:29+00:00 2026-06-11T00:42:29+00:00

I am working under a web application which use spring mvc + mybatis +

  • 0

I am working under a web application which use spring mvc + mybatis + mysql.

And I found that I can not get the auto-generated key for the last inserted record (I have googled so much).

This is the related configuration(take the model ‘Post’ for example):

spring.xml:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    //omitted
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="config.xml" />
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

mybatis config.xml

<configuration>
    <typeAliases>
        <typeAlias alias="Post" type="com.king.model.Post" />
    </typeAliases>
    <mappers>
        <mapper resource="com/king/model/PostMapper.xml" />
    </mappers>
</configuration>

PostMapper.xml:

<mapper namespace="com.king.model.PostMapper">
    <insert id="insert" parameterType="Post">
        insert into posts (title,body,created_at,updated_at) values (#{title},#{body},#{createDate},#{updateDate})
    </insert>
</mapper>

Dao:

public abstract class AbstractSimpleDaoImpl<T> extends SqlSessionDaoSupport{
    @Override
    public int add(T entity) {
        return getSqlSession().insert(getMapperNamespace() + ".insert", entity);
    }

    protected abstract String getMapperNamespace();
}

public class PostDao extends AbstractSimpleDaoImpl<Post> {
    @Override
    protected String getMapperNamespace() {
        return "com.king.model.PostMapper";
    }
}

However,when I insert a new Post to database:

postDao.add(post);

I can not get the generated id for the inserted post. I always get 1. I know this is the row-affected number.

Also,I read the mybatis guide,and I tried this:

<insert id="insert" parameterType="Post" useGeneratedKeys="true" keyProperty="id">

But it seems that,this does not work.

How to fix it?

  • 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-06-11T00:42:31+00:00Added an answer on June 11, 2026 at 12:42 am

    In fact the following works:

    <insert id="insert" parameterType="Post" useGeneratedKeys="true" keyProperty="id">
    
    postDao.add(post);
    

    will return 1 as before,but post.getId() will get the key.

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

Sidebar

Related Questions

I have a web application that is working fine under the following environment GWT
I am developing a web application with spring mvc on eclipce under linux. What
I am working on a commercial web application that has a separate mobile browser
I have a web application which was working well until I converted it from
I've got an ASP.NET 4.0 web application (webforms, not mvc; asp.net routing isn't used)
I am working on a web application in which I need to show the
I start working at an existing project (an web application) that was created by
I'm working on a Website Project (as opposed to an ASP.NET Web Application) which
I'm working on an ASP .NET 2.0 site which uses a Web Application project
I have a small web.py Python application that I would like to serve under

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.