|
目录
一:MyBatis的简单理解?
二:Mybatis的优点?编辑
三:Mybatis与java程序对接的对象获取
四:Mybatis入门开发流程原理
五:mybatis基础开发
5.1 Mapper代理开发
六:mybatis的xml文件讲解
七:mybatis的动态查询
7.1 多条件的动态查询
7.2?单条件的动态查询
7.3?添加和修改
7.4?批量删除
一:MyBatis的简单理解?
mybatis框架简单理解就是封装了JDBC操作的一个框架,开发者已面向对象的思想去管理数据库。
mybatis框架是一个数据持久性的开源框架(ORMapping)
O是指:Object(面向对象)
R是指:Relationship(关系型数据库)
最常用的例子就是Java到Mysql的映射
二:Mybatis的优点
三:Mybatis与java程序对接的对象获取

四:Mybatis入门开发流程原理
首先加入mybatis依赖和mysql依赖,这样子就相当于导入了jar包,使得我们能够操作mybatis和mysql的相关功能,配置一个mybatis-config.xml文件这个文件在mybatis框架执行的时候用来连接本机数据库,加载到sql操作文件,这个sql操作文件就是你对数据库的各种操作信息,mybatis框架通过每一个操作的id值来获取不同的操作。
说白了一句话就是mybatis框架可以自动去创建你java和数据库连接的必须的对象sqlSession。
五:mybatis基础开发
在入门开发中,咱们的测试类中把操作sql文件中的ID都给写死了,并且在测试类中获取sql文件中的所要执行的对象的时候,这些sqlID是不同的,所以我们人为的去管理这些信息是不太方便的。
所以我们通过框架中所封装的类进行这些sql操作的信息管理。
5.1 Mapper代理开发
通过sqlsession获取Mapper对象来操作sql文件ID。


六:mybatis的xml文件讲解
<environments default="development">
<environment id="development">
......
</environment>
????<environment id="test">
???????......
? </environment>
</environments>
不同的environment代表不同的库的信息。通过default="development"来切换不同的库的环境。
七:mybatis的动态查询
7.1 多条件的动态查询
if

select *
from tb_brand
where
<if test="stas">
stas = #{stas}
</if>
and
<if test="company_name != null">
company_name like #{company_name}
</if>
and
<if test="brand_name != null">
brand_name like #{brand_name};
?where
当where 后面直接跟着 and的时候服务器会报错。
我们可以加一个1==1的恒等式。
或者
使用<where></where>
<where>
<if test="stas">
stas = #{stas}
</if>
and
<if test="company_name != null">
company_name like #{company_name}
</if>
and
<if test="brand_name != null">
brand_name like #{brand_name};
</if>
</where>
7.2?单条件的动态查询
当用户在选择框中选择了一个条件作为查询条件的时候,这就用到了<choose></choose>表达式
<select id="selectByConditionSingle" resultType="com.rbynode.pojo.Brand">
select * from tb_brand
<where>
<choose>
<when test="stas != null">
stas = #{stas}
</when>
<when test="company_name != null">
company_name like #{company_name}
</when>
<when test="brand_name != null">
brand_name like #{brand_name}
</when>
</choose>
</where>
</select>
7.3?添加和修改
添加和修改中最重要的就是要提交事务,如果不提交事务则数据库中不会有新增加和修改的数据

在获取sqlSession的时候,如果将openSession()中的boolean值设置为true,那么就可以自己提交事务
主键返回

7.4?批量删除
批量删除用到了foreach()coolection接收到的是已经改了名字的数组(ids),默认是array。
<delete id="deleteByIds">
delete
from tb_brand
where id in (
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>);
</delete>

|