-
Mybatis的OGNL表達式 取值寫法查看全部
-
1、Mybatis中SqlSession的作用; 1、向SQL語句出入參數; 2、執行SQL語句; 3、獲取SQL語句執行的結果; 4、事務的控制; 2、如何得到SqlSession? 1、通過配置文件獲取數據庫連接的相關信息; 2、通過配置信息構建SqlSessionFactory; 3、通過SqlSessionFactory打開數據庫會話;查看全部
-
Mybatis中保存主表數據之后獲取id值添加到javabean對應屬性的方法. 因為id是自動增長的,在用Mybatis插入的sql語句中不需要管id這個字段,插入數據之后,javabean實體類中的id屬性是沒有值的,因此需要在<insert>標簽添加useGeneratedKeys="true"屬性與keyProperty="javabean的id屬性名"屬性關聯. 那么在插入完數據之后,本來javabean中的id屬性是沒有值的,但是設置了這個屬性之后就會自動根據數據庫表自動增長的id值賦值給javabean中的id屬性. 或者直接在數據庫中插入數據,再從java中通過select查詢所有信息也可以.查看全部
-
#{}與${}是一樣的,寫法與來源都一樣,都是寫在sql語句上. 區別: #{}:有預編譯,可防sql注入. ${}:無預編譯,直接拼接參數,字符串無單引號. select * from message where command=#{command} select * from message where command='${command}' 兩條sql語句都是查詢,但是最后Mybatis運行時解析的時候不同. 第一條sql語句解析成: select * from message where command=? ; (預編譯,最后Mybatis再為其賦值) 第二條sql語句解析成: select * from message where command='查詢'; //假設參數值為查詢,即直接賦值 注意: ${}因為不是預編譯,當參數是字符串,Mybatis解析時不會加上''號,它會直接賦值,所以需要在sql語句上自己加上單引號,即'${command}',否則就會出錯. #{}與ognl表達式取值寫法是相同的.查看全部
-
resultMap與resultType屬性都是表示結果集與java對象之間的關系.但要注意兩者區別. 如果用了resultType屬性就不用在Sql配置文檔中配置<resultMap>標簽. resultType映射的原理是:如果映射到自定義類,那么javabean中的屬性名與查詢出來的列名相同的時候就表示他們有映射關系(大小寫不敏感,也不用考慮同名列名的問題),結果集就會放進這些javabean的屬性中. resultType也可以映射到Map中,那么查詢的列名就是key值,數據就是value值. resultMap屬性="要映射的<resultMap>標簽的id值" resultType屬性="要映射的java類" <select id="find" resultMap="resultMessage"> select * from message </select> <select id="find" resultType="com.imooc.Message"> select * from message </select> //此時就不需要配置<resultMap>標簽.查看全部
-
Mybatis常用標簽歸納查看全部
-
Sql配置文檔的其他常用標簽 <choose>標簽,有<when test="">和<otherwise>子標簽. 相當于if(){}if(){} else{}語句 <select > select * from message <choose> <when test=""></when> <when test=""></when> <otherwise></otherwise> </choose> </select>查看全部
-
Sql配置文檔的其他常用標簽 <set>標簽,與<where>標簽同級.<set>標簽其實就是代替<where>標簽的where關鍵字.也經常與<if>標簽配合使用,會自動添加逗號,功能與<wherer>標簽相似. <update id="updateset" parameterType="com.imooc.Message"> update message <set> <if test="command!=null"> command=#{command} </if> <if test="description!=null"> description=#{description} </if> </set> </update>查看全部
-
Sql配置文檔的其他常用標簽 <sql>標簽,此標簽與<select>、<update>這些標簽同一個級別. <sql>標簽與java中的常量定義一個意思.即可以把經常用到的一些sql語句把它放在<sql>標簽下,在<sql>標簽下添加id屬性.在要用到<sql>標簽的位置添加<include refid="sql標簽的id值">標簽即可. <select id="find" resultMap="resultMap"> select <include refid="sql1"> from message </select> <sql id="sql1">command,description,content</sql> 一般<sql>標簽存的是列名,因為在要插入數據或者查詢數據時直接引用sql常量會比較方便.查看全部
-
Sql配置文檔的其他常用標簽 <where>標簽,一般與<if>標簽一起使用. 用了where關鍵字就不用再Sql語句后面加where 1=1來拼接,<where>標簽會幫我們補上where關鍵字,假如<where>標簽下的<if>條件都不滿足,<where>標簽與不會為我們添加where關鍵字. 當<where>標簽下的<if>條件滿足多個時,拼接語句添加" and command=#{command}",因為用了<where>標簽,它會把where關鍵字之后的第一個and關鍵字出掉.即會變成一條符合規范的sql語句. <select id="find" resultMap="resultMap"> select * from Message <where> <if test="command!=null"> and comand =#{command} </if> <if test="description!=null"> and description =#{descrtiption} </if> </where> </select>查看全部
-
在子表類的Sql配置文檔的<resultMap>標簽下需要添加一個<association>標簽來指明當前子表這個類里包含著一個主表類類型的成員變量. 其中<assoication>標簽有property、column(可無)、javaType屬性、resultMap屬性. property屬性指明在子表類中的主表類類型的成員變量名. column屬性指明數據庫中的字段. javaType屬性指明主表類的類名. resutlMap屬性指明主表類的<resultMap>標簽的id屬性的屬性值. 在<association>標簽下有<id>與<result>子標簽,他們都有property與column屬性.來表明主表類的所有成員變量.或者直接用resultMap屬性直接替代,就可以不用寫子標簽. (在子表的Sql配置文件) <resultMap type="com.imooc.Content" id="Content"> <id column="ID" jdbcType="Integer" property="id"/> <result column="Content" jdbcType="VARCHAR" property="content"/> <result column="CommandId" jdbcType="Integer" property="commandId"/> <association property="command" javaType="com.imooc.Command"> <id column="CID" property="id"/> <result column="name" property="name"/> <result column="description" property="description" /> </association> </resultMap> 等于: <resultMap type="" id=""> <id column="" jdbcType="" property=""/> <result column="" jdbcType="" property=""/> <association property="" resultMap="" /> </resultMap>查看全部
-
且當select u.name from User as u,column屬性不能寫成u.name,不能帶表名.因為在jdbc的ResultSet接口根據列名獲取數據的方法中,根據的列名是沒有表名前綴的.因此表名前綴會失效. 所以在Mybatis一對多關聯的Sql配置文檔下的<select>標簽的sql語句不能出現兩個字段名相同的查詢.否則在相關聯的不同Sql配置文件的<resultMap>標簽下的其他標簽的column屬性就會重復,Mybatis并不知道這個重復的字段是屬于哪個表的.所以要不起別名,要不設計數據庫不同表之間不能出現同名的列名. 要不設置別名: <select id="getMessageCom" resultMap="resultMapCom"> select a.id as cid,a.name,a.description,b.id,b.content from command as a left join content as b on a.id=b.commandid </select> 要不就設計表時列名不重復: <select id="getMessageCom" resultMap="resultMapCom"> select a.cid,a.name,a.description,b.id,b.content from command as a left join content as b on a.id=b.commandid </select>查看全部
-
在主表類的Sql配置文檔的<resultMap>標簽下需要添加一個<collection>標簽來指明我當前主表這個類里包含著子表類的集合.其中<collection>標簽下存在property屬性與resultMap屬性.沒有column屬性,因為在數據庫表中不會額外創建一個此集合的字段.property屬性表示主表類中哪個屬性名,resultMap屬性要指明包含的子表類的Sql配置文件的<resultMap>標簽的id值. <resultMap type="com.imooc.Command" id="Command"> <id column="CID" jdbcType="Integer" property="id"/> <result column="Name" jdbcType="VARCHAR" property="name"/> <result column="Description" jdbcType="VARCHAR" property="description"/> <collection property="contentlist" resultMap="Content.content"/> //因為是跨文件所以要namespace.id </resultMap> 特別要注意的是column屬性對應數據庫表的字段名意思是與select子句選擇的字段名匹配起來.如果select u.name as username from User as u; 雖然在數據庫表中的字段名是name,但是select子句為name字段設置了別名,即username,因此在<resultMap>標簽下的<result>標簽的column屬性要等于username而并非name,是與select子句中的名稱匹配,要注意當設置了別名那么column屬性也為別名查看全部
-
Mybatis實現一對多的關系配置 (之前的例子是指令與回復內容一對一的關系) 指令與回復內容之間有一對多的關系.把指令所在的表稱為主表,回復內容所在的表稱為子表.在代碼中也需要對應兩個實體類.且都有各自的Sql配置文件. public class Command{ private int id; private String name; private String description; private List<Content> contentlist; 對應的set/get()方法 } public class Content{ private int id; private String content; private int commandId; private Command command; 對應的set/get()方法 } 在主表的實體類中除了要對應數據庫表字段的屬性,還要有一個子表的集合屬性,與Hibernate相似,不僅能用Set集合,還可以用List集合.有了這個集合才能反映出一對多的關系. 在子表的實體類中也要有一個主表類類型的屬性來存放此Content對象對應的主表類的信息. 有了關聯就要可以在各自Sql配置文檔配置sql語句,在Mybatis中可以直接用sql語句進行多表的連接. (主表類的Sql配置文件) <select id="getMessageCom" resultMap="resultMapCom"> select a.id as cid,a.name,a.description,b.id,b.content from command as a left join content as b on a.id=b.commandid </select> (子表類的Sql配置文件) <select id="getMessageCon" resultMap="resultMapCon"> select b.id,b.content,a.id as cid,a.name,a.description from content as b left join command as a on b.commandid=a.id </select>查看全部
-
在js的if()語句內 如果有 var command="" //空字符串 command=null; command=undefinded; content=0 //數字0 那么在if(command)語句里判斷都是返回false的.查看全部
舉報
0/150
提交
取消