一.想要进行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>