类似这种:
public class TestMain05 { public static void main(String[] args) { String sql = "select * from test where name = '?' "; String name = "小强"; sql = sql.replace("?", name); System.out.println(sql); // select * from test where name = '小强' } }
${}
他就相当于是普通的拼接 你传入什么就原封不动的给你放到Sql后边
类似这种:
public class TestMain06 { public static void main(String[] args) { String sql = "select * from test where name = ?"; String name = "'小强'"; sql = sql.replace("?", name); System.out.println(sql); } }
四、ResultMap ResultType的区别
resultType使用方式我们已经用过很多次了,这里先下一个resultMap的使用方式
<resultMap id="testResultMap" type="testentity"> <id property="id" column="id" javaType="long" jdbcType="BIGINT"/> <result property="name" column="name" javaType="string" jdbcType="VARCHAR"/> <result property="age" column="name" javaType="int" jdbcType="INTEGER"/> <result property="salary" column="name" javaType="decimal" jdbcType="DECIMAL"/> </resultMap> <select id="testResultMap" resultMap="testResultMap"> select * from test </select>
resultType、resultMap功能类似,都是返回对象信息,但是resultMap要更强大一些,可以实现自定义。
我们一般项目中数据库都是下划线比如course_date 但是实体类中都是用courseDate 所以大部分时候都需要进行名称匹配都会用到resultMap 或者 sql写别名
sql别名方式:
<select id="list" resultType="testentity"> select id as id name as name age as age course_date as courseDate from test </select>
id&result
他俩都是将一列值映射到一个简单的数据类型(String、int、double、Date等)的属性或字段。
他俩唯一的不同是:id元素对应的属性会被标记为对象的标识符,在比较对象实例的时候使用。这样可以提升整体的性能,尤其是进行缓存和嵌套结果映射的时候。
他俩都有一些属性:
属性 | 描述 |
---|---|
property |
映射到列结果的字段或属性 |
column |
数据库中的列名,或者是列的别名。一般情况下,这和传递给 resultSet.getString(columnName) 方法的参数一样。就是数据库列名 |
javaType |
一个 Java 类的全限定名,或一个类型别名 如果你映射到一个 JavaBean,MyBatis 通常可以推断类型。然而,如果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证行为与期望的相一致。 |
jdbcType |
这就是数据库对应的类型,非必须的 |
typeHandler |
结果处理器,就是把结果再处理一次返回 后边几篇写一下自定义typeHandler |
在中文官网上找了个例子:
<!-- 非常复杂的结果映射 --> <resultMap id="detailedBlogResultMap" type="Blog"> <constructor> <idArg column="blog_id" javaType="int"/> <arg column="name" javaType="varchar"></arg> </constructor> <result property="title" column="blog_title"/> <association property="author" javaType="Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/> <result property="favouriteSection" column="author_favourite_section"/> </association> <collection property="posts" ofType="Post"> <id property="id" column="post_id"/> <result property="subject" column="post_subject"/> <association property="author" javaType="Author"/> <collection property="comments" ofType="Comment"> <id property="id" column="comment_id"/> </collection> <collection property="tags" ofType="Tag" > <id property="id" column="tag_id"/> </collection> <discriminator javaType="int" column="draft"> <case value="1" resultType="DraftPost"/> </discriminator> </collection> </resultMap>
constructor: 用于实例化类时,注入结果到构造方法中
idArg:这个就是主键ID
arg:这个就是普通字段
association: 一个复杂类型的关联 对象里字段是对象
collection:一个复杂的类型关联 对象里字段是集合
discriminator: 使用结果值来确认使用哪个resultMap
下集预告:
写一个复杂点的返回结果resultMap案例
动态SQL 常用标签
到此这篇关于一小时迅速入门Mybatis之Prepared Statement与符号的使用的文章就介绍到这了,更多相关Mybatis Prepared Statement内容请搜索