Mybatis知识
MyBatis 本是apache的一個開源項目iBatis,2010年這個項目由apache software foundation 遷移到了google code,并且改名為MyBatis,實質(zhì)上Mybatis對ibatis進行一些改進。
MyBatis是一個優(yōu)秀的持久層框架,它對jdbc的操作數(shù)據(jù)庫的過程進行封裝,使開發(fā)者只需要關(guān)注SQL本身,而不需要花費精力去處理例如注冊驅(qū)動、創(chuàng)建connection、創(chuàng)建statement、手動設(shè)置參數(shù)、結(jié)果集檢索等jdbc繁雜的過程代碼。
Mybatis通過xml或注解的方式將要執(zhí)行的各種statement(statement、preparedStatemnt、CallableStatement)配置起來,并通過java對象和statement中的sql進行映射生成最終執(zhí)行的sq1語句,最后由nybatis框架執(zhí)行sq1并將結(jié)果映射成Java對象并返回。
MyBatis Java持久層框架,用于操作關(guān)系型數(shù)據(jù)庫,是對JDBC的一個封裝。
原始的dao層,接口與接口實現(xiàn)都需要自己寫
mapper代理,只需要寫接口,不必去管實現(xiàn)
對于mapper.xml的命名,建議以表名+Mapper.xml進行命名。
JDBC 寫出的SQL語句是硬編碼,也就是寫死的東西,每當后期的需求進行更改的時候需要重新進行編譯,這樣就會導(dǎo)致系統(tǒng)不易維護,**?**表示占位符,可以設(shè)置相關(guān)的參數(shù),將SQL語句放在一塊,?占位符與參數(shù)放在一塊,查詢到的結(jié)果集映射到pojo上面。
SQL語句中,#{ } 表示占位符,mybatis后期會將值自動添加單引號進行填入。
占位符接受的參數(shù)是parameterType中傳過來的,如果是簡單類型的參數(shù)的話名字是無所謂的,但是當接受的參數(shù)是pojo時就必須名字進行一一匹配,防止進行OGNL解析時出現(xiàn)錯誤。
${ }表示不加修飾的拼接參數(shù),這樣的缺點是不可以防止SQL注入,例如一條like語句
like '%${keyword}%'
注意SQL中千萬不要寫 ; 進行結(jié)尾。
resultMap中,SQL的列名與對象的屬性不相同時可進行一一屬性映射達到匹配來完成返回的結(jié)果集正確。
原生的mybatis需要一個配置文件,mybatisConfig.xml,相當于加載其運行環(huán)境。
sqlSessionFactory(SQL會話工廠),作用是創(chuàng)建sqlSession(面向用戶的接口)來進行操作數(shù)據(jù)庫,每次用完后都需要進行關(guān)閉。
mapper.xml的例子
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="xyz.tylt.mybatisLearn.mapper.UserMapper"> <!-- 自定義返回結(jié)果集 --><resultMap id="userMap" type="xyz.tylt.mybatisLearn.beans.UserBean"> <!-- 此處的id為指定下列查詢的,返回的結(jié)果集,resultMap的值,并且必須唯一且對應(yīng),此處的type指的是返回出去的結(jié)果集類型,這里使用的對象類型 --><id property="id" column="id" javaType="java.lang.Integer"></id><!-- column指的是對應(yīng)的數(shù)據(jù)庫表中的列,property指的對應(yīng)的實體對象的屬性名,javaType指的是數(shù)據(jù)表中字段的屬性,主要為了數(shù)據(jù)表字段與對象屬性進行不重名綁定,重名也可以 --><result property="username" column="username" javaType="java.lang.String"></result><result property="password" column="password" javaType="java.lang.String"></result><result property="account" column="account" javaType="java.lang.Double"></result></resultMap> <!-- 在各標簽(SQL)中的id屬性必須和接口中的方法名相同 , id屬性值必須是唯一的,不能夠重復(fù)使用。parameterType屬性指查詢時使用的參數(shù)類型,resultType屬性指查詢返回的結(jié)果集id--> <!-- useGeneratedKeys:( 僅 對 insert 有 用 ) 這 會 告 訴 MyBatis 使 用 JDBC 的getGeneratedKeys方法來取出由數(shù)據(jù)(比如:像 MySQL 和 SQLServer 這樣的數(shù)據(jù)庫管理系統(tǒng)的自動遞增字段)內(nèi)部生成的主鍵。默認值: false。 --> <!--keyProperty: (僅對 insert有用)標記一個屬性, MyBatis 會通過 getGeneratedKeys或者通過 insert 語句的 selectKey 子元素設(shè)置它的值。默認:不設(shè)置。 --> <!--#{}中的內(nèi)容,為占位符,當參數(shù)為某個JavaBean時,表示放置該Bean對象的屬性值 --><insert id="insertUser" useGeneratedKeys="true" keyProperty="id">insert into t_user (username,password,account) values (#{username},#{password},#{account})</insert><update id="updateUser" >update t_user set username=#{username},password=#{password},account=#{account} where id=#{id}</update><delete id="deleteUser" parameterType="int">delete from t_user where id=#{id} </delete><select id="selectUserById" parameterType="int" resultMap="userMap">select * from t_user where id=#{id}</select><select id="selectAllUser" resultMap="userMap">select * from t_user</select></mapper> <?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"> <!-- namespace命名空間 --> <mapper namespace="com.tylt.mapper.UserDao"><!-- 輸出user信息 --><resultMap id="UsersMap" type="com.tylt.model.User"><id column="id" property="id" jdbcType="INTEGER" /><result column="name" property="name" jdbcType="VARCHAR" /><result column="password" property="password" jdbcType="VARCHAR" /></resultMap><select id="findAll" resultMap="UsersMap">select * from userinfo</select><!-- 登錄 --><select id="findUser" parameterType="String" resultType="com.tylt.model.User">select * from userinfo where name = #{name}and password = #{password}</select><update id="updateUsersById" parameterType="User">update userinfo<set><if test="name != null">name=#{name},</if><if test="password != null">password=#{password}</if></set>where id=#{id}</update><insert id="insertUsers" parameterType="User">insert intouserinfo(name,password)values(#{name},#{password})</insert><delete id="deleteUsersById" parameterType="int">delete from userinfowhere id=#{id}</delete><select id="login" resultMap="UsersMap">select * from userinfo where name=#{name} and password=#{password}</select> </mapper>總結(jié)
- 上一篇: surface pro java_【微软
- 下一篇: 家庭公网IP动态解析至阿里云DNS