如何使用MyBatis将Java保留字的数据库列名称映射到Java模型对象字段?

时间:2018-09-11 07:04:49

标签: java mybatis

所以我有一个表find . -type f -exec echo '${}' \; ,其中有两列:CoolTablesomething

我的模型类名为short反映了两个字段:CoolClasssomething_short是Java保留的关键字,因此该字段必须带有下划线作为前缀。

现在,我的mybatis映射器XML像这样使用short

CoolClass

现在,当我 <select id="getStuff" resultType="CoolClass"> SELECT * FROM CoolTable </select> <insert id = "insertStuff" parameterType = "CoolClass"> INSERT INTO CoolTable (something, short) VALUES (#{something}, #{short}) </insert> getStuff时,只有insertStuff列会被检索和插入。 something始终为空。

我搜索了MyBatis的所有文档,但找不到与该案例有关的任何帮助。

如何将short列映射到课程的short字段?

1 个答案:

答案 0 :(得分:0)

使用ResultMap修复它,如下所示:

private CallbackManager callbackManager;
private ShareDialog shareDialog;
private String title;
private String status;

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    searchAlbumArt(PlayerService.getCurrentTrack().getArtist(), PlayerService.getCurrentTrack().getAlbum());
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    shareDialog = new ShareDialog(this);
    setTheme(ThemeUtils.getTheme(getApplicationContext()));
    setContentView(R.layout.activity_share_song);
    initializeComponents(); // Initialize the Components
    initializeListeners(); // Initialize the Listeners
    callbackManager = CallbackManager.Factory.create();
    shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result) {
            Toast.makeText(getApplicationContext(), "Share success!", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Fb onSuccess");
        }

        @Override
        public void onCancel() {
            Toast.makeText(getApplicationContext(), "Did not share", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Fb onCancel");
        }


        @Override
        public void onError(FacebookException error) {
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Fb onError");
        }
    }, 90);
 }


private void initializeComponents() {
    btnShareSongfb = (Button) findViewById(R.id.fb_share_button);
    ShareButton shareButton = (ShareButton) findViewById(R.id.fb_share_button);
    ShareLinkContent content = generateFbStatus();
    shareButton.setShareContent(content);
 }


private ShareLinkContent generateFbStatus() {
           status = "xxx";
           title = "yyy";

        ShareLinkContent content = new ShareLinkContent.Builder()
                .setContentUrl(Uri.parse( (getResources().getString(R.string.google_play_url))))
                .setQuote(title)
                .setShareHashtag(new ShareHashtag.Builder()
                        .setHashtag(status)
                        .build())
                .build();
        return content;
    }
}


@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}