一、POJO
POJO(Plain Ordinary Java Object),简单普通的java对象,也就是普通JavaBean。主要用来指代那些没有遵循特定的java对象模型,约定或者框架的对象。没有业务逻辑,有时可以作为VO(value -object)或dto(Data Transform Object)来使用。
二、JavaBean
JavaBean 是一种JAVA语言写成的可重用组件。为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器。JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性,set和get方法获取。
三、ResultMap
使用resultMap做SQL语句返回结果类型处理时,通常需要在mapper.xml中定义resultMap进行pojo和相应表字段的对应。
id column用来设置主键
result column对应sql中的列名。
property对应pojo中的属性名,也就是说property是column的别名。
<?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="com.sz.cms.mapper.ArticleMapper">
<select id="selectArticlesByCid" resultMap="ArticleResultMap">
select * from web_article where cid=#{arg0} limit #{arg1}, #{arg2}
</select>
<resultMap id="ArticleResultMap" type="com.sz.cms.po.Article">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="title" property="title" jdbcType="VARCHAR" />
<result column="text" property="text" jdbcType="VARCHAR" />
<result column="imgUrl" property="imgUrl" jdbcType="VARCHAR" />
</resultMap>
</mapper>
四、ResultType
使用resultType做SQL语句返回结果类型处理时,对于SQL语句查询出的字段在相应的pojo中必须有和它相同的字段对应,而resultType中的内容就是pojo在本项目中的位置。
<?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="demo.boot.mapper.UserMapper">
<select id="selectAll" resultType="User">
select * from t_user
</select>
<insert id="insertUser" parameterType="User">
insert into t_user values(default, #{name}, #{password})
</insert>
<delete id="deleteUserById" parameterType="int">
delete from t_user where id = #{id}
</delete>
<update id="updateUser" parameterType="User">
update t_user set name=#{name}, password=#{password} where id=#{id}
</update>
</mapper>