J2EE轻量级框架--3.27学习心得

来源:互联网 发布:网络棋牌游戏出售 编辑:程序博客网 时间:2024/04/30 03:48

今天查询了一下相关mybatis的知识,收获如下:

在映射文件中的配置
通过#{property}来得到对应Property的值
1.向数据表中插入一条记录
<insert id="insert" parameterType="Person">
INSERT INTO t_person (name, age, birthday) VALUES(#{name}, #{age}, #{birthday})
</insert>
更新,删除和插入记录类似
2.查询
2.1 单个记录查询
    <select id="select" parameterType="int" resultType="Person">
SELECT * FROM t_person WHERE id = #{id}
    </select>
2.2 查询多个记录
    和单个记录查询类似,在写java代码时需要调用SqlSession的selectList()方法
2.3 sql参数不是类的属性
    如要查询在某个年龄段的人的信息,范围的起始值不是Person的属性值,这时可以用Map     来解决
    <select id="selectForNotPropertyParam" parameterType="map"resultType="Person">
SELECT * FROM t_person WHERE age BETWEEN #{min} AND #{max}
    </select>
    这里的min和max都是map中的key
2.4 数据表中的字段和类属性名称不一致
    如Person的gender属性,对应在数据表中的字段名为sex,这时需要通过<resultMap>来对     其进行映射
    <resultMap type="Person" id="PersonMap">
<result column="sex" property="gender"/>
    </resultMap>
    <select id="selectForDB2Property" resultMap="PersonMap">
SELECT * FROM t_person;
    </select>
3.标签的包含:对于基本的sql语句我们可以将其包含起来,以避免重复的代码
<sql id="baseSql">SELECT * FROM t_person</sql>
<select id="selectUseBaseSql" resultType="Person">
<include refid="baseSql"/>
</select>

0 0
原创粉丝点击