如果我有课
public class Product {
private int id;
private String name;
private double price;
private String type;
}
dao 接口(interface)
public interface {
public Product selectOne(int id);
}
数据库中的表
T_Product (
id tinyint,
name varchar(50),
price long,
type varchar(30) );
我想知道如何在mybatis中为selectOne方法编写sqlMapper!
最佳答案
这是注释的另一个选项:
public interface ProductMapper{
@Select( "select id, name, price, tag from Product where id = #{id}" )
public Product selectOne( @Param("id") int id);
}
这是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>
不需要结果映射,因为列可以直接映射到对象属性。
关于java - 如何在mybatis select中编写select标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30640551/