一.想要進行Mybatis的開發,首先需要定義一個核心配置文件,路徑resources/mybatis/mybatis.cfg.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="mysqlDev"> <!-- 定義數據庫連接的相關配置 --> <environment id="mysqlDev"> <!-- 配置MySQL數據庫連接 --> <transactionManager type="jdbc"/> <!-- 事務控制類型 --> <dataSource type="POOLED"> <!-- 使用連接池的模式管理連接 --> <property name="driver" value="org.gjt.mm.mysql.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/yootk" /> <property name="username" value="root" /> <property name="password" value="mysqladmin" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/yootk/mybatis/vo/mapper/News.xml"/> </mappers> </configuration>
對以上配置說明:
1. “<transactionManager type="jdbc"/> ” : 說明當前使用的是”JDBC“進行事務;
2. ”<dataSource type="POOLED">“ 表示當前數據庫的連接全部由連接池進行管理 ;
|-”type="JNDI"“:利用容器上數據庫連接池進行管理;
|-”type="UNPOOLED"“;不使用連接池管理
二.Mybatis的實現需要進行一個SQL文件的配置,一般與vo保存在同一個目錄,為了方便維護這里它的名字與VO類的名稱相同,
<?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">
<!-- 定義所有的SQL語句的映射, 對於本實體而言相關的所有的SQL代碼都在此定義 -->
<mapper namespace="com.yootk.mapper.NewsNS"> <!-- SSM整合的時候,這個命名空間異常重要 -->
<!-- 編寫增加的操作語句,其中id隨意定義,只要有意義即可 -->
<insert id="doCreate" parameterType="com.yootk.mybatis.vo.News">
INSERT INTO news(title,content) VALUES (#{title},#{content}) ;
</insert>
</mapper>
對以上配置,概念解釋如下:
1. “<mapper namespace="com.yootk.mapper.NewsNS">” :表示指定命名空間的映射,改命名空間是標識不同SQL配置文件的唯一標記;
2.“ <insert>” :在此處定義要添加的SQL語句;
3.“id=doCreate” : 描述此舉的ID,與命名空間一樣
4.parameterType=:"com.yootk.mybatis.vo.NewS" :設置該語句執行參數的類型;
5.#{title} :表示將傳入的VO對象的指定屬性取出,填充到執行的SQL中 ;
三,在修改resource/mybatis/mybatis.cfg.xml配置文件,引入以上定義的mapping操作路徑:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="mysqlDev"> <!-- 定義數據庫連接的相關配置 --> <environment id="mysqlDev"> <!-- 配置MySQL數據庫連接 --> <transactionManager type="jdbc"/> <!-- 事務控制類型 --> <dataSource type="POOLED"> <!-- 使用連接池的模式管理連接 --> <property name="driver" value="org.gjt.mm.mysql.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/yootk" /> <property name="username" value="root" /> <property name="password" value="mysqladmin" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/yootk/mybatis/vo/mapper/News.xml"/> </mappers> </configuration>
別名配置
在之前執行的程序代碼,在進行參數接收時全部是明確的程序類的信息,但是一旦配置項比較多,重復定義完整的名稱是比較繁瑣的,所以為了解決這個問題,可以在mybatis.cfg.xml配置文件里面進行配置文件的別名;
,例: <typeAlias types="com.yootk.mybatis.vo.News" alias="News">,但是一個個修改還是太麻煩了,所以這里設置批量掃描定義別名
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <!-- 批量掃描指定包下的類,類名稱就是別名 --> <package name="com.yootk.mybatis.vo"/> </typeAliases> <environments default="mysqlDev"> <!-- 定義數據庫連接的相關配置 --> <environment id="mysqlDev"> <!-- 配置MySQL數據庫連接 --> <transactionManager type="jdbc"/> <!-- 事務控制類型 --> <dataSource type="POOLED"> <!-- 使用連接池的模式管理連接 --> <property name="driver" value="org.gjt.mm.mysql.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/yootk" /> <property name="username" value="root" /> <property name="password" value="mysqladmin" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/yootk/mybatis/vo/mapper/News.xml"/> </mappers> </configuration>
同時News.xml文件里也需要進行別名定義處理:
<mapper namespace="com.yootk.mapper.NewsNS"> <!-- SSM整合的時候,這個命名空間異常重要 --> <!-- 編寫增加的操作語句,其中id隨意定義,只要有意義即可 --> <insert id="doCreate" parameterType="News"> INSERT INTO news(title,content) VALUES (#{title},#{content}) ; </insert> </mapper>
Mybatis語句
<?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"> <!-- 定義所有的SQL語句的映射, 對於本實體而言相關的所有的SQL代碼都在此定義 --> <mapper namespace="com.yootk.mapper.NewsNS"> <!-- SSM整合的時候,這個命名空間異常重要 --> <!-- 編寫增加的操作語句,其中id隨意定義,只要有意義即可 --> <insert id="doCreate" parameterType="News" keyProperty="nid" keyColumn="nid" useGeneratedKeys="true"> INSERT INTO news(title,content) VALUES <trim prefix="(" suffix=")" suffixOverrides=","> <if test="title == null"> 'NOTitle', </if> <if test="title != null"> #{title}, </if> <if test="content == null"> 'NOContent', </if> <if test="content != null"> #{content}, </if> </trim> </insert> <update id="doEdit" parameterType="News"> UPDATE news <set> <if test="title != null and title != """> title=#{title}, </if> <if test="content != null and content != """> content=#{content}, </if> </set> <where> <if test="nid != null and nid != 0"> nid=#{nid} </if> </where> </update> <delete id="doRemove" parameterType="java.lang.Long"> DELETE FROM news WHERE nid=#{suibianxiemingzi} </delete> <select id="findById" parameterType="java.lang.Long" resultType="News"> SELECT nid,title,content FROM news WHERE nid=#{wodenid} ; </select> <select id="findAll" resultType="News" parameterType="Map"> SELECT nid,title,content FROM news <if test="title != null and title != """> WHERE title=#{title} </if> </select> <!-- 在進行數據查詢的時候,發現有些查詢的內容總在不斷重復,所以可以定義為一個重復引用的標記 --> <sql id="selectBase"> SELECT nid,title,content FROM news </sql> <select id="findByIds" resultType="News" parameterType="java.lang.Long"> <include refid="selectBase"/> <where> nid IN <foreach collection="array" open="(" close=")" separator="," item="ele"> #{ele} </foreach> </where> </select> <delete id="doRemoveByIds" parameterType="java.lang.Long"> DELETE FROM news <where> nid IN <foreach collection="array" open="(" close=")" separator="," item="ele"> #{ele} </foreach> </where> </delete> <select id="findAllCondition" resultType="News" parameterType="java.util.Map"> SELECT nid,title FROM news <where> <choose> <when test="nid != null and title !=null and content !=null"> nid=#{nid} AND title=#{title} AND content=#{content} </when> <when test="nid != null and title !=null and content==null"> nid=#{nid} AND title=#{title} </when> <when test="nid != null and title ==null and content!=null"> nid=#{nid} AND content=#{content} </when> </choose> </where> </select> <select id="findSplit" resultType="News" parameterType="java.util.Map"> SELECT nid,title,content FROM news <if test="column != null and keyword != null and column != "" and keyword != """> WHERE ${column} LIKE #{keyword} </if> LIMIT #{start},#{lineSize} ; </select> <select id="getAllCount" resultType="java.lang.Long" parameterType="java.util.Map"> SELECT COUNT(*) FROM news <if test="column != null and keyword != null and column != "" and keyword != """> WHERE ${column} LIKE #{keyword} </if> </select> </mapper>