mybatis基础学习1---(配置文件和sql语句)
1:配置文件(主要配置文件)
2:配置文件(引入)
3:sql语句解析:
<mapper namespace="/">
<!-- 1 -->根据id查对象
<select id="findById" parameterType="int" resultType="jike.book.pojo.JiKeUser">
select *
from jikeUser where id=#{id}
</select>
<!-- 1 -->
<!-- 2 -->插入对象
<insert id="insertUser" parameterType="JiKeUser" statementType="PREPARED"
keyProperty="id" useGeneratedKeys="true">
insert into JikeUser
(userName,password) values
(#{userName},#{password})
</insert>
<!-- 2 -->
<!-- 3 -->更改对象
<update id="updateUser" parameterType="JiKeUser">
UPDATE JiKeUser SET
userName=#{userName},
password=#{password}
WHERE id = #{id}
</update>
<!-- 3 -->
<!-- 4 -->登录选择
<select id="loginSelect" resultType="JiKeUser" parameterType="hashmap">
select * from JikeUser where userName=#{userName} and
password=#{password}
</select>
<!-- 4 -->
<!-- 5 -->登录选择
<select id="login2" resultType="JiKeUser" parameterType="JiKeUser">
select *
from JikeUser where userName=#{userName} and password=#{password}
</select>
<!-- 5 -->
<!-- 6 -->对象列表查询
<select id="selectJiKeUserList" resultType="JiKeUser">
select * from
JikeUser
</select>
<!-- 6 -->
<!-- 7 --><!--和下面有联系-->
<resultMap id="JiKeUserMap" type="JiKeUser">
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="password" column="password" />
</resultMap>
<!-- 7 -->
<!-- 8 -->对象Map查询<!--和上面有联系-->
<select id="selectUsers" resultMap="JiKeUserMap">
select id, userName, password
from JiKeUser
</select>
<!-- 8 -->
</mapper>
----------------------------------------------------------------------------------------------------------------
4:mybatis的sql语句区分大小写
例:
select * from JikeUser where userName=#{userName} and
password=#{password}
大括号里的字段必须和Bean定义的字段大小写一致(因为靠java的setter传值)。外面的字段要和建表字段一致(如下图)
---------------------------------------------------------------------------------------------------------------------
5:mybatis配置文件引入。(下面类和包引用写反了)
直接引用xml文件
<mappers>
<mapper resource="com/yu/res/UserMapper.xml" />
</mappers>
通过绝对路径引用,注意在绝对路径前加上:“file:///”
<mappers>
<mapper url="file:///D:/workspace/mywork/MyBatis/src/com/yu/res/UserMapper.xml"/>
</mappers>
引用mapper接口对象的方式:
<mappers>
<mapper resource="com.yu.mapper.UserMapper" />
</mappers>
引用mapper接口包的方式:
<mappers>
<package name="com.yu.mapper"/>
</mappers>
6:包定义注意事项比较
引入主配置文件(MyBatisConfig.xml)要加包名
----------------------------------------------------------------------------------------------------------------------
主配置文件引其他配置文件也要加包名
=============================================================