如何写选择标签形式mybatis选择 [英] how to write select tag form mybatis select

查看:227
本文介绍了如何写选择标签形式mybatis选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个班级

public class Product {
private int id; 
private String name;
private double price;
private String type;
}

dao界面

public interface {
 public Product selectOne(int id);
}

数据库中的表格

T_Product (
id tinyint,
name varchar(50),
price long,
type varchar(30) );

我想知道如何在mybatis中为selectOne方法编写sqlMapper!

I want to know how to write the sqlMapper in mybatis for the selectOne method!

推荐答案

这是注释的另一个选项:

This is another option by annotation:

public interface ProductMapper{
 @Select( "select id, name, price, tag from Product where id = #{id}" )
 public Product selectOne( @Param("id") int id);
}

这是另一种在xml中写作的方式:

This is another way of writing in the xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="YOUR_INTERFACE_NAME_WITH_PACKAGE_NAME">
     <select id="selectOne" resultType="Product">
        select id, name, price, tag from Product where id = #{id}
    </select>
</mapper>

不需要结果映射,因为列可以直接映射到对象属性。

No result map is required because the columns can be map directly to the object property.

这篇关于如何写选择标签形式mybatis选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆