对Mybatis粗浅认知

来源:互联网 发布:商场女装品牌推荐 知乎 编辑:程序博客网 时间:2024/04/24 00:12

首先我们应该知道ibatis就是mybatis的前身是apache的ibatis,后来放到google code 改名为mybatis。

mybatis也是一款orm的实现;ORM(object-relection-mapping)关系型映射。

但是mybatis并不是jpa下的产品,jpa是一套规范,用以规范时长上的orm框架,但是mybatis并不是实现jpa的产品

mybatis相对于hibernate来说,它相当于一个“半自动化”框架,它需要自己建立表,而hibernate是一个“全自动化”的orm框架,因为hibernate可以自动进行表的创建,但是记住一点,它只是创建表并不能创建数据库,因此数据库还得自己来创建。

mybatis用到的主要的核心包(这里楼主以mybatis3.4.4为例)



写mybatis.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>
<!-- 配置加载进来的数据源信息的位置 -->
<properties resource="c3p0.properties"></properties>
<!-- 配置环境 、默认环境-->
<environments default="development">
<!--  环境1,意思是可以有多个环境,如果要在哪个环境下执行,
就将大环境下的default设置成谁的id-->
<environment id="development">
<!-- 设置数据库事务管理、一般都是jdbc -->
<transactionManager type="JDBC"></transactionManager>
<!-- 设置数据源 -->
<dataSource type="POOLED">
<property name="driver" value="${c3p0.driverClass}" />
<property name="url" value="${c3p0.url}" />
<property name="username" value="${c3p0.username}" />
<property name="password" value="${c3p0.password}" />
</dataSource>
</environment>
</environments>
<!-- 配置mapper文件的位置 -->
<mappers><mapper resource="org/com/mapper/mapper.xml"/></mappers>
</configuration>

再一步配置mapper文件,即是将需要操作的sql语句写在里面

<?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">
<mapper namespace="org.fkit.hrm.mapper.UserMapper">
<!--我们要编写一个操作数据库的配置 -->
<!--第一个程序我要编写插入数据到数据库 insert:表示的是向数据库出入数据 id:相当于给这个方法整个名字 调用的时候方便 insert的值 
就是让我们来编写 这个sql语句 -->
<!--添加用户得操作sql -->
<insert id="addUser" parameterType="org.fkit.hrm.domain.User">
<!-- 这个里面就编写sql语句 ,这里的#仅仅只是占位用的,如果传入的值是巴中基本的数据类型,那么{}里面的名字是可以随意写的,如果是自己定义的类的类型,那                          么就需要对应的值-->
INSERT INTO tb_user(username,status,loginname,password,createDate)
VALUES(#{username},#{status},#{loginname},#{password},#{createDate})
</insert>

    <!--查询用户的操作sql语句 -->
<select id="findUser" parameterType="org.fkit.hrm.domain.User"
resultMap="userResultMap">
select * from tb_user
<include refid="findbyUser"></include>
</select>
<!--  查询用户的合法性-->
<select id="findUserByNameAndpwd" parameterType="org.fkit.hrm.domain.User"
resultMap="userResultMap">
select * from tb_user  where loginname=#{loginname} and password=#{password}
</select>
<!--更新用户的操作sql语句 -->
<update id="updateUser" parameterType="org.fkit.hrm.domain.User">
update tb_user set
username=#{username},status=#{status},loginname=#{loginname},password=#{password}
where id=#{id}
</update>
<!-- 删除用户的操作 -->
<delete id="deleteUser" parameterType="integer">
delete from tb_user where
id=#{values}
</delete>
<!-- 查询分页的操作 -->
<select id="findUserByPage" parameterType="java.util.Map"    resultMap="userResultMap">
select * from tb_user 
<include refid="findbyUser1"></include>
limit #{pageModel.firstLimitParam},#{pageModel.pageSize}
</select>
<resultMap type="org.fkit.hrm.domain.User" id="userResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="status" column="status" />
<result property="loginname" column="loginname" />
<result property="password" column="password" />
<result property="createDate" column="createDate" />
</resultMap>
<sql id="findbyUser">
<trim prefix="where" prefixOverrides="and|or">
<if test="username!=null  and username.length>0">
and username like '%${username}%'
</if>
<if test="status !=null and status.length>0">
and status=#{status}
</if>
</trim>


</sql>
<sql id="findbyUser1">
<trim prefix="where" prefixOverrides="and|or">
<if test="user!=null and user.username!=null  and  user.username.length>0">
and username like '%${user.username}%'
</if>
<if test="user!=null  and user.status !=null and user.status.length>0">
and status=#{user.status}
</if>
</trim>
</sql>

</mapper>

上面的mapper文件包含了sql片段,这个便于不知道用户会选择什么的情况下使用,包含了模糊查询%${}%,注意  这种方式是容易被sql注入的

还有一点mybatis可以做到只写接口和mapper文件就可以进行数据库的更新查询工作,但是有个前提就是执行sql的id要与接口的方法名相同,传参类型与paramtype相同,返回值类型与方法的返回值类型相同

mybatis返回值的处理,我们知道我们返回一个实体对象很容易,但是如果是两个实体对象呢?   楼楼有两种方法解决这个问题

1.写两个实体的包装类,然后在mapper的sql返回值类型写这个包装类的全路径

2.写resultmap的映射配置(这时就需要复习一对多,一对一的配置方式  楼楼写了如下的配置)

<下面是一对一的配置>

<resultMap type="org.fkit.hrm.domain.Notice" id="NoticeResultMap">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
<result property="createDate" column="createDate">

<result property="uId" column="uId" />
<association property="user" javaType="org.fkit.hrm.domain.User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="status" column="status" />
<result property="loginname" column="loginname" />
<result property="password" column="password" />
<result property="createDate" column="createDate" />
</association>
</resultMap>

<下面是一对多的配置>

<resultMap type="org.fkit.hrm.domain.Document" id="DocumentResultMap">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="remark" column="remark" />
<result property="createDate" column="createDate" />
<result property="uId" column="uId" />
<result property="fileName" column="fileName" />
<association property="user" javaType="org.fkit.hrm.domain.User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="status" column="status" />
<result property="loginname" column="loginname" />
<result property="password" column="password" />
<result property="createDate" column="createDate" />
</association>
</resultMap>

先整理到这里,后期在学习中继续更新、、、、、

原创粉丝点击