//mybatis
MyyBatis框架简介
-------------------------------------------------------------------------------------------------------------
什么是什么是MyBaits
– MyBatis 本是apache的一个开源项目iBatis, 2010年这个项
由apacheapache softwaresoftware foundationfoundation 迁移到了googlegoogle codecode,并
且改名为MyBatis 。2013年11月迁移到Github。
– iBATIS一词来源于“internet”和“abatis”的组合,是一个基于
JJava的持久层框架。iBATIS提供的持久层框架包括SQL
Maps和Data Access Objects (DAO)
–MyBatis是一个数据持久层(ORM)框架。把实体类和SQL语句之
间建立了映射关系,是一种半自动化的ORM实现。
–– 下载地址:https://github.com/mybatis
MyBatisMyBatis的优点:
– 1.基于SQL语法,简单易学。
– 2.能了解底层组装过程。
– 3.SQL语句封装在配置文件中语句封装在配置文件中,便于统一管理与维护,降低
了程序的耦合度。
– 4.程序调试方便。
– 所有sql语句,全部定义在xml(建议)中。也可以通过注解
的方式在接口上实现。这些映射文件称之为mapper。
与传统JDBC的比较的比较
– 减少了61%的代码量
– 最简单的持久化框架
– 架构级性能增强
– SQL代码从程序代码中彻底分离,可重用
– 增强了项目中的分工
– 增强了移植性
SqlMapConfig.xml全局配置文件
配置要包括所有的文件内容
<configuration>
在这个里面写全部内容
<!-- 加载属性文件 db.properties是jdbc里面的各种属性储存的文件2、后先被读取-->
<properties resource="db.properties">
<!--properties中还可以配置一些属性名和属性值 -->
<!-- <property name="jdbc.driver" value=""/> 1、首先被读取-->
</properties>
全局参数
<settings>
<setting name="logImpl" value="LOG4J" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="false" />
</settings>
<!-- mappers告诉MyBatis去哪里找持久化类的映射文件 -->
<mappers>
<mapper resource="org/apwla/mapper/PersonMapper.xml"></mapper>
<mapper resource="org/apwla/mapper/CardMapper.xml"></mapper>
<mapper resource="org/apwla/mapper/EmployeeMapper.xml" />
</mappers>
<typeAliases>
<!-- <typeAlias type="全程" alias="别名"/> -->
</typeAliases>
</configuration>
---------------mapper.xml-----------------
org.apwla.mapper.EmployeeMapper为接口地址
<mapper namespace="org.apwla.mapper.EmployeeMapper">
selectEmployeeWithId为借口中的方法名一致
mapper.java接口中的方法输入参数类型和mapper.xml
中statement的parameterType指定的类型一致。
mapper.java接口中的方法返回值类型和mapper.xml中
SELECT * FROM tb_employee where id = #{id}
</select>
<!-- if -->
<select id="selectEmployeeByIdLike" resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee WHERE state = 'ACTIVE'
<!-- 可选条件,如果传进来的参数有id属性,则加上id查询条件 -->
<if test="id != null ">
and id = #{id}
</if>
</select>
<!-- if -->
<select id="selectEmployeeByLoginLike" resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee WHERE state = 'ACTIVE'
<!-- 两个可选条件,例如登录功能的登录名和密码查询 -->
<if test="loginname != null and password != null">
and loginname = #{loginname} and password = #{password}
</if>
</select>
<!-- choose(when、otherwise) -->
<select id="selectEmployeeChoose" parameterType="hashmap"
resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee WHERE state = 'ACTIVE'
<!-- 如果传入了id,就根据id查询,没有传入id就根据loginname和password查询,否则查询sex等于男的数据 -->
<choose>
<when test="id != null">
and id = #{id}
</when>
<when test="loginname != null and password != null">
and loginname = #{loginname} and password = #{password}
</when>
<otherwise>
and sex = '男'
</otherwise>
</choose>
</select>
<select id="findEmployeeLike" resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee
<where>
<if test="state != null ">
state = #{state}
</if>
<if test="id != null ">
and id = #{id}
</if>
<if test="loginname != null and password != null">
and loginname = #{loginname} and password = #{password}
</if>
</where>
</select>
<!-- where -->
<select id="selectEmployeeLike" resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee
<where>
<if test="state != null ">
state = #{state}
</if>
<if test="id != null ">
and id = #{id}
</if>
<if test="loginname != null and password != null">
and loginname = #{loginname} and password = #{password}
</if>
</where>
</select>
<!-- set -->
<update id="updateEmployeeIfNecessary" parameterType="org.apwla.domain.Employee">
update tb_employee
<set>
<if test="loginname != null">
loginname=#{loginname},
</if>
<if test="password != null">
password=#{password},
</if>
<if test="name != null">name=#{name},</if>
<if test="sex != null">sex=#{sex},</if>
<if test="age != null">age=#{age},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="sal != null">sal=#{sal},</if>
<if test="state != null">state=#{state}</if>
</set>
where id=#{id}
</update>
<!-- foreach -->
<select id="selectEmployeeIn" resultType="org.apwla.domain.Employee">
SELECT *
FROM tb_employee
WHERE ID in
<foreach item="item" index="index" collection="list" open="("
separator="," close=")">
#{item}
</foreach>
</select>
<!-- bind -->
<select id="selectEmployeeLikeName" resultType="org.apwla.domain.Employee">
<bind name="pattern" value="'%' + _parameter.getName() + '%'" />
SELECT * FROM tb_employee
WHERE loginname LIKE #{pattern}
</select>
</mapper>
MyyBatis框架简介
-------------------------------------------------------------------------------------------------------------
什么是什么是MyBaits
– MyBatis 本是apache的一个开源项目iBatis, 2010年这个项
由apacheapache softwaresoftware foundationfoundation 迁移到了googlegoogle codecode,并
且改名为MyBatis 。2013年11月迁移到Github。
– iBATIS一词来源于“internet”和“abatis”的组合,是一个基于
JJava的持久层框架。iBATIS提供的持久层框架包括SQL
Maps和Data Access Objects (DAO)
–MyBatis是一个数据持久层(ORM)框架。把实体类和SQL语句之
间建立了映射关系,是一种半自动化的ORM实现。
–– 下载地址:https://github.com/mybatis
MyBatisMyBatis的优点:
– 1.基于SQL语法,简单易学。
– 2.能了解底层组装过程。
– 3.SQL语句封装在配置文件中语句封装在配置文件中,便于统一管理与维护,降低
了程序的耦合度。
– 4.程序调试方便。
– 所有sql语句,全部定义在xml(建议)中。也可以通过注解
的方式在接口上实现。这些映射文件称之为mapper。
与传统JDBC的比较的比较
– 减少了61%的代码量
– 最简单的持久化框架
– 架构级性能增强
– SQL代码从程序代码中彻底分离,可重用
– 增强了项目中的分工
– 增强了移植性
SqlMapConfig.xml全局配置文件
配置要包括所有的文件内容
<configuration>
在这个里面写全部内容
<!-- 加载属性文件 db.properties是jdbc里面的各种属性储存的文件2、后先被读取-->
<properties resource="db.properties">
<!--properties中还可以配置一些属性名和属性值 -->
<!-- <property name="jdbc.driver" value=""/> 1、首先被读取-->
</properties>
全局参数
<settings>
<setting name="logImpl" value="LOG4J" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="false" />
</settings>
<!-- mappers告诉MyBatis去哪里找持久化类的映射文件 -->
<mappers>
<mapper resource="org/apwla/mapper/PersonMapper.xml"></mapper>
<mapper resource="org/apwla/mapper/CardMapper.xml"></mapper>
<mapper resource="org/apwla/mapper/EmployeeMapper.xml" />
</mappers>
<typeAliases>
<!-- <typeAlias type="全程" alias="别名"/> -->
</typeAliases>
</configuration>
---------------mapper.xml-----------------
org.apwla.mapper.EmployeeMapper为接口地址
<mapper namespace="org.apwla.mapper.EmployeeMapper">
selectEmployeeWithId为借口中的方法名一致
mapper.java接口中的方法输入参数类型和mapper.xml
中statement的parameterType指定的类型一致。
mapper.java接口中的方法返回值类型和mapper.xml中
statementstatement的resultType指定的类型一致。
<select id="selectEmployeeWithId" parameterType="int" resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee where id = #{id}
</select>
<!-- if -->
<select id="selectEmployeeByIdLike" resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee WHERE state = 'ACTIVE'
<!-- 可选条件,如果传进来的参数有id属性,则加上id查询条件 -->
<if test="id != null ">
and id = #{id}
</if>
</select>
<!-- if -->
<select id="selectEmployeeByLoginLike" resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee WHERE state = 'ACTIVE'
<!-- 两个可选条件,例如登录功能的登录名和密码查询 -->
<if test="loginname != null and password != null">
and loginname = #{loginname} and password = #{password}
</if>
</select>
<!-- choose(when、otherwise) -->
<select id="selectEmployeeChoose" parameterType="hashmap"
resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee WHERE state = 'ACTIVE'
<!-- 如果传入了id,就根据id查询,没有传入id就根据loginname和password查询,否则查询sex等于男的数据 -->
<choose>
<when test="id != null">
and id = #{id}
</when>
<when test="loginname != null and password != null">
and loginname = #{loginname} and password = #{password}
</when>
<otherwise>
and sex = '男'
</otherwise>
</choose>
</select>
<select id="findEmployeeLike" resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee
<where>
<if test="state != null ">
state = #{state}
</if>
<if test="id != null ">
and id = #{id}
</if>
<if test="loginname != null and password != null">
and loginname = #{loginname} and password = #{password}
</if>
</where>
</select>
<!-- where -->
<select id="selectEmployeeLike" resultType="org.apwla.domain.Employee">
SELECT * FROM tb_employee
<where>
<if test="state != null ">
state = #{state}
</if>
<if test="id != null ">
and id = #{id}
</if>
<if test="loginname != null and password != null">
and loginname = #{loginname} and password = #{password}
</if>
</where>
</select>
<!-- set -->
<update id="updateEmployeeIfNecessary" parameterType="org.apwla.domain.Employee">
update tb_employee
<set>
<if test="loginname != null">
loginname=#{loginname},
</if>
<if test="password != null">
password=#{password},
</if>
<if test="name != null">name=#{name},</if>
<if test="sex != null">sex=#{sex},</if>
<if test="age != null">age=#{age},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="sal != null">sal=#{sal},</if>
<if test="state != null">state=#{state}</if>
</set>
where id=#{id}
</update>
<!-- foreach -->
<select id="selectEmployeeIn" resultType="org.apwla.domain.Employee">
SELECT *
FROM tb_employee
WHERE ID in
<foreach item="item" index="index" collection="list" open="("
separator="," close=")">
#{item}
</foreach>
</select>
<!-- bind -->
<select id="selectEmployeeLikeName" resultType="org.apwla.domain.Employee">
<bind name="pattern" value="'%' + _parameter.getName() + '%'" />
SELECT * FROM tb_employee
WHERE loginname LIKE #{pattern}
</select>
</mapper>