Mybatis框架-1
1.Mybatis框架:
Mybatis是一个半自动的对象关系映射(ORM),实现结果集的自动封装,sql写到配置文件中;
Mybatis使用的是DTD约束。
2.Mybatis模块调用:

3.SqlMapConfig.xml :Mybatis框架的核心配置。
default=“MySql”---> 默认使用MySQL数据库
映射配置中的resource=“”--> 单个对象的映射文件
<?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="mysql">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb1?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="admin" />
</dataSource>
</environment> <environment id="oracle">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
<property name="username" value="ht1602" />
<property name="password" value="htdb" />
</dataSource>
</environment>
</environments> <!-- 映射文件 -->
<mappers>
<mapper resource="pojo/UserMapper.xml" />
</mappers> </configuration>
4.XXXMapper.xml:一张表对应一个对象,则所有关于这张表的增删查改的SQL都写在一个配置文件中。
resultType="Javabean的全限定名" --> 将结果集自动封装到对象中
${ } 和 #{ } 的基本抉择:
作用:
1.含有预编译的效果,能够防止sql注入攻击
2.为参数添加了一对""号
注意事项:
如果sql语句中以列名为参数时,切记使用${Map中的key},必须配置Map一起联用.
总结
以列名为参数时使用${},其他的使用#{}
能用#{}取值,决不用${}
<?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"> <!-- namespace:唯一标识映射文件 -->
<mapper namespace="pojo.UserMapper"> <!-- resultType: 将结果集自动封装到对象中 -->
<select id="find" resultType="pojo.User">
select * from account
</select> <!-- #{对象的属性} -->
<insert id="adduser">
insert into account (id,name,money) values (null,#{name},#{money})
</insert> <update id="updatauser">
update account set money=#{money} where id=#{id}
</update> <!-- ${map中的Key} -->
<select id="selectByMoney" resultType="pojo.User">
select * from account where money > ${minMoney} <![CDATA[and money< ${maxMoney} ]]>
</select> </mapper>
<![CDATA[ ... ]]> :大段转义字符;写在中括号内部的字符都将变成字符串输出。这样就避免了xml文件中的关键符号;一般在用xml作为数据传输格式时这个可以方便将整个xml文件输出字符串进行传输
5.获取数据库连接(SqlSessionFactory):
1.通过流读取Mybatis核心配置文件;
2.创建SalSessionFactory对象;
3.获取数据库连接(SqlSession);
6.执行数据库的CRUD:
--->openSession()
@Test
public void test1() throws IOException{ //通过流读取Mybatis核心配置文件
InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml"); //获取SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); //从数据源中获取连接
SqlSession openSession = factory.openSession(); //执行sql ---namespace.id
List<User> list = openSession.selectList("pojo.UserMapper.find"); for (User user : list) {
System.out.println(user);
} }
7.Mybatis中的多值传递问题
如需求:
要求查询年龄在18-22之间的人
问题:在Mybatis中只支持单值的传递.多值传递时没有现成的API
解决方法:
可以将多个值转化为单个对象或Map
建议:
如果是插入操作/更新操作,使用对象.其他的使用MAP
8.动态更新操作
需求:
如果某些数据只修改特定的值,其他参数不变,这时需要使用动态更新
基本语法:
<update id="dynamicUpdate">
update user
<set>
<if test="name !=null">name=#{name},</if>
<if test="age !=null">age=#{age},</if>
<if test="sex !=null">sex=#{sex}</if>
</set>
where id=#{id}
</update>
set标签的作用:去除where条件前多余的1个逗号
9.动态查询
需求:
根据对象中的属性值,查询信息
方案:使用动态查询
基本语法:
<select id="dynamicFind" resultType="pojo.User">
select * from user
<where>
<if test="id !=null">id=#{id}</if>
<if test="name !=null">and name = #{name}</if>
<if test="age !=null">and age = #{age}</if>
<if test="sex !=null">and sex = #{sex}</if>
</where>
</select>
Where标签的作用:去除where后边多余1个的and
9.动态插入操作
<insert id="addUser">
insert into user
<!--trim能够实现拼接 和去除指定的元素 -->
<trim prefix="(" suffix=")" suffixOverrides=",">
id,
<if test="name !=null">name,</if>
<if test="age !=null">age,</if>
<if test="sex !=null">sex</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
null,
<if test="name !=null">#{name},</if>
<if test="age !=null"> #{age},</if>
<if test="sex !=null"> #{sex}</if>
</trim>
</insert>
trim能够实现拼接 和去除指定的元素
10.批量删除(遍历)
需求:
删除ID从20---31的数据
解决方法:
使用in关键字实现动态删除
** 注意:collection=“...” --- 数组:array、List集合:list、Map集合:map中的key
<delete id="deleteUser">
delete from user where id in
<!--遍历数组 -->
<foreach collection="array" open="(" close=")"
item="id" separator=",">
#{id}
</foreach>
</delete>
11.别名标签
需求:
当进行结果集映射时,如果包名比较长,这时进行封装比较麻烦.
解决方法:用别名标签
<!--定义别名标签
type="需要起别名的类型"
alias="User" 类名
-->
<typeAliases>
<typeAlias type="pojo.User" alias="User"/>
</typeAliases>
12.Mybatis中单值传递问题

说明:在Mybaits中如果传递的参数为单值例如 12,"String".通过#{}取值时,参数可以是任意。
13.sql的复用

如果需要多表关联时,sql语句较多,可以使用sql标签进行简化.但是可读性降低了.
Mybatis框架-1的更多相关文章
- Mybatis框架的多对一关联关系(六)
一.一对多的关联映射 一对多关联查询多表数据 1接口 public interface IDeptDAO { //根据部门编号查询该部门单个查询 public Emp getEmpById(Integ ...
- Spring+SpringMvc+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- Mybatis框架中实现双向一对多关系映射
学习过Hibernate框架的伙伴们很容易就能简单的配置各种映射关系(Hibernate框架的映射关系在我的blogs中也有详细的讲解),但是在Mybatis框架中我们又如何去实现 一对多的关系映射呢 ...
- Hibernate框架与Mybatis框架的对比
学习了Hibernate和Mybatis,但是一直不太清楚他们两者的区别的联系,今天在网上翻了翻,就做了一下总结,希望对大家有帮助! 原文:http://blog.csdn.net/firejuly/ ...
- 初识Mybatis框架,实现增删改查等操作(动态拼接和动态修改)
此第一次接触Mybatis框架确实是有点不适应,特别是刚从Hibernate框架转转型过来,那么为什么要使用Mybatis框架,Mybatis框架和Hibernate框架又有什么异同呢? 这个问题在我 ...
- Spring+MyBatis框架中sql语句的书写,数据集的传递以及多表关联查询
在很多Java EE项目中,Spring+MyBatis框架经常被用到,项目搭建在这里不再赘述,现在要将的是如何在项目中书写,增删改查的语句,如何操作数据库,以及后台如何获取数据,如何进行关联查询,以 ...
- SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- Spring3.0 与 MyBatis框架 整合小实例
本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...
- 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...
- 详解Java的MyBatis框架中SQL语句映射部分的编写
这篇文章主要介绍了Java的MyBatis框架中SQL语句映射部分的编写,文中分为resultMap和增删查改实现两个部分来讲解,需要的朋友可以参考下 1.resultMap SQL 映射XML 文件 ...
随机推荐
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- CSS3知识之折角效果
CSS3折角效果:可兼容不同背景
- Linux检测硬盘读取速度
1. 清空缓存 > /proc/sys/vm/drop_caches 2. 测试读取速度 a. 将/dev/zero中数据按1M的数据单位写入testfile,共写512个单位,并不通过缓存 c ...
- [Luogu 3966] TJOI 2013 单词
经典ACAM. 注意单词之间添加字符,以及对重复单词的处理. #include <cstdio> #include <cstring> #include <queue&g ...
- 版本号中Snapshot的含义
Maven中建立的依赖管理方式基本已成为Java语言依赖管理的事实标准,Maven的替代者Gradle也基本沿用了Maven的依赖管理机制.在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个 ...
- Ant打jar包时,参数名被修改的问题
https://blog.csdn.net/landehuxi/article/details/42678117 使用Ant打jar包后,发现jar包中的方法名会在前面自动添加了“param”前缀,导 ...
- 01-QQ 3-最终重构版
Demo示例程序源代码
源代码下载链接:01-QQ 3.zip292.5 KB // QQAppDelegate.h Map // // QQAppDelegate.h // 01-QQ // // Created ...
- 关于SQL注入的五大报错注入函数
~全部都以查user()为例子~ 1.floor()id = 1 and (select 1 from (select count(*),concat(version(),floor(rand(0) ...
- H题 hdu 2520 我是菜鸟,我怕谁
题目大意:http://acm.hdu.edu.cn/showproblem.php?pid=2520 我是菜鸟,我怕谁 Time Limit: 2000/1000 MS (Java/Others) ...
- 代码回滚:Reset、Checkout、Revert 的选择
git reset.git checkout 和 git revert 是你的 Git 工具箱中最有用的一些命令.它们都用来撤销代码仓库中的某些更改,而前两个命令不仅可以作用于提交,还可以作用于特定文 ...