MyBatis_02 框架
- 动态SQL语句
- Xml方式
- 注解方式
- MyBatis的缓存
- MyBatis的关联查询
- MyBatis逆向工程
- 根据条件组装不同结构的SQL语句,可以提高SQL代码的重用性.
- 满足某些特定需求,如,条件判断查询
package cn.zj.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import cn.zj.mybatis.pojo.User;
public interface UserMapper { /**
* 根据条件查询结果
* @param user
* @return
*/
List<User> selectByCondition(User user); /**
* 根据提交查询总数
* @param user
* @return
*/
Long selectTotalByCondition(User user); /**
* 修改用户
* @param user
* @return
*/
int updateUserByNotNull(User user); /**
* 批量删除用户
* @param ids
* @return
*/
int deleteByIds(@Param("ids")Integer[] ids); /**
* 批量插入
* @param users
* @return
*/
int insertByBatch(@Param("users")List<User> users); }
<?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 配置 mybatis 表的映射
namespache :命名空间(和对应映射的接口的 全限定名一样),通俗讲,当前映射文件的唯一标识
全限定名 : 包名+接口名/类名
-->
<mapper namespace="cn.zj.mybatis.mapper.UserMapper"> <!-- sql片段 -->
<sql id="condition_sql">
<!-- where 标签 -->
<where>
<if test="name !=null">
name like concat('%',#{name},'%')
</if>
<if test="password !=null">
and password = #{password}
</if>
<if test="age !=null">
and age = #{age}
</if>
</where> </sql> <!-- 多条件查询 -->
<select id="selectByCondition" parameterType="cn.zj.mybatis.pojo.User" resultType="cn.zj.mybatis.pojo.User">
select * from user
<!-- 引入sql片段 -->
<include refid="condition_sql"/>
</select> <!-- 多条件查询总数 -->
<select id="selectTotalByCondition" parameterType="cn.zj.mybatis.pojo.User" resultType="long">
select count(*) from user
<!-- where 标签 -->
<include refid="condition_sql"/>
</select> <!-- 修改用户信息(数据不为空的修改) -->
<update id="updateUserByNotNull" parameterType="cn.zj.mybatis.pojo.User">
update user
<set>
<if test="name !=null">
name = #{name},
</if>
<if test="password !=null">
password = #{password},
</if>
<if test="age !=null">
age = #{age},
</if>
</set>
where id = #{id}
</update> <!-- 批量删除 -->
<delete id="deleteByIds" parameterType="integer">
<!-- sql : delete from user where id in (1,2,3,4) -->
delete from user where id in <!-- 动态sql语句值 foreach --> <foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete> <!-- 批量插入 -->
<delete id="insertByBatch" parameterType="list">
<!-- insert into user (name,password,age) values (xx,xx,xx),(xx1,xx1,xx1) --> insert into user (name,password,age) values <foreach collection="users" item="user" separator="," >
(#{user.name},#{user.password},#{user.age})
</foreach>
</delete>
</mapper>
package cn.zj.mybatis.test; |
- @SelectProvider 动态查询SQL语句对应注解
- @InsertProvider 动态插入SQL语句对应注解
- @UpdateProvider 动态修改SQL语句对应注解
- @DeleteProvider 动态删除SQL语句对应注解
package cn.zj.mybatis.mapper; |
package cn.zj.mybatis.pojo; |
package cn.zj.mybatis.test; |
@Test |
<settings> |
|
<ehcache>
<!-- 缓存的磁盘位置 -->
<diskStore path="D:/mybatis_cache"/>
<!-- 默认的缓存策略: 如果开发者在某一个需要缓存的文件配置了自定义缓存,就不使用默认的,如果没有配置,就使用默认缓存策略-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
</ehcache>
|
|
<mapper namespace="cn.zj.mybatis.dao.UserMapper">
<!-- 当前表的映射开启支持二级缓存,并设置相关的缓存提供商,以及缓存的相关配置 -->
<cache type="org.mybatis.caches.ehcache.EhcacheCache" >
<!--最大的空闲时间 -->
<property name="timeToIdleSeconds" value="10000"/>
<!-- 最大的在线时间 -->
<property name="timeToLiveSeconds" value="20000"/>
<!-- 内存的大小 b字节 m1 =1024k 1k=1024b -->
<property name="maxEntriesLocalHeap" value="2000000"/>
<!-- 文件的大小 b字节-->
<property name="maxEntriesLocalDisk" value="20000000"/>
<!-- 算法 LRU:最少使用优先, "LFU" or "FIFO:先进先出 -->
<property name="memoryStoreEvictionPolicy" value="LRU"/>
</cache>
<select id="selectAll" resultType="User">
select * from user
</select>
</mapper>
|
|
public class User implements Serializable{
private static final long serialVersionUID = -8366151150155170110L;
}
|
|
命中率= 从缓存中获取数据的次数/ 查询的总次数
如 : 两次查询 从缓中获取一次
0.5 = 1/2;
0.666666 = 2/3;
命中率越高缓存效果越好
|
|
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.0
DEBUG [main] - ==> Preparing: select * from user where id = ?
DEBUG [main] - ==> Parameters: 3(Integer)
DEBUG [main] - <== Total: 1
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.5
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.6666666666666666
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.75
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.8
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.8333333333333334
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.8571428571428571
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.875
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.8888888888888888
DEBUG [main] - Cache Hit Ratio [cn.zj.mybatis.mapper.UserMapper]: 0.9
|
|
在实际开发中,一个业务可能涉及到多个数据表的查询,那么多表查询就涉及连接查询(等值连接), 等值连接 表与表之间有一个外键关键
但是程序中最终获取的表封装的对象, 对象与对象之间是没有外键关系的,对象和对象之间只有依赖关系
对象之间关系主要是四种
一对一 关系
一个人对应身份证id,一个QQ号对应一个QQ空间
一对多 关系
一个部门对应多个员工
多对一 关系
多个员工对应一个部门
多对多 关系
多个学生对应多个老师,多个学生对应多个课程
什么关系应该从哪个对象作为中心点来看
一对多, 以one方作为中心点
MyBatis框架支持多表查询封装对象之间关系
<collection> 一对多查询
<association>多对一和一对一查询
|
|
员工表
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`dept_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
部门表
CREATE TABLE `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
|
|
public class Employee {
private Integer id;
private String name;
//以员工为中心 :
多个员工对应一个部门,多对一关系,many2one
//员工与部门的对象关系
private Department dept;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", dept=" + dept + "]";
}
}
|
|
public class Department {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + "]";
}
}
|
|
Man2oneMapper.xml 映射文件
|
|
<?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="cn.zj.mybatis.mapper.Many2OneMapper">
<select id="selectByEmpId" parameterType="Integer" resultMap="emp_map">
select * from employee where id = #{id}
</select>
<resultMap type="cn.zj.mybatis.pojo.Employee" id="emp_map">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!--
需要映射部门对象属性
private Department dept;
解决方案,使用关联查询
<association property="dept" column="dept_id" select=""/>
property :需要映射的属性
column :要映射属性对应的外键列
select :对应的查询方案, 对应查询的命名空间+.+功能id
-->
<association property="dept" column="dept_id"
select="cn.zj.mybatis.mapper.Many2OneMapper.findDeptById"/>
</resultMap>
<!-- 根据部门Id查询对应的部门对象 -->
<select id="findDeptById" parameterType="Integer" resultType="cn.zj.mybatis.pojo.Department">
select * from department where id = #{dept_id}
</select>
</mapper>
|
|
Pojo对象
|
|
public class Department {
private Integer id;
private String name;
//以部门为中心:一个部门对应多个与员工 一对多关系 (one2many)
private List<Employee> emps;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmps() {
return emps;
}
public void setEmps(List<Employee> emps) {
this.emps = emps;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + ", emps=" + emps + "]";
}
}
|
|
public class Employee {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
}
|
|
<?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="cn.zj.mybatis.mapper.One2ManyMapper">
<select id="selectDeptById" parameterType="Integer" resultMap="dept_map">
select * from department where id = #{id}
</select>
<!-- 手动映射 -->
<resultMap type="cn.zj.mybatis.pojo.Department" id="dept_map">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!-- private List<Employee> emps;
集合属性映射
<collection property="emps" column="id" select=""/>
property:需要映射得集合 emps
column : 部门的主键 id
select : 关联查询,去根据部门id查询出对应的员工
值关联查询功能的 命名空间+.+功能id
-->
<collection property="emps" column="id" select="cn.zj.mybatis.mapper.One2ManyMapper.selectUsersByDeptId"/>
</resultMap>
<!-- 关联查询的功能
根据部门的id查询所有的员工
-->
<select id="selectUsersByDeptId" parameterType="integer" resultType="cn.zj.mybatis.pojo.Employee">
select * from employee where dept_id = #{id}
</select>
</mapper>
|
![]() |
|
<?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="cn.zj.mybatis.mapper.One2ManyMapper">
<select id="selectDeptById" parameterType="Integer" resultMap="dept_map">
select e.id e_id ,e.name e_name,d.id d_id,d.name d_name
from department d JOIN employee e ON d.id = e.dept_id WHERE d.id = #{id};
</select>
<!-- 手动映射 -->
<resultMap type="Department" id="dept_map">
<id column="d_id" property="id"/>
<result column="d_name" property="name"/>
<!-- private List<Employee> emps;
集合属性映射
<collection property="emps" column="id" select=""/>
property:需要映射得集合 emps
column : 部门的主键 id
ofType : 需要映射集合的泛型的类型
-->
<collection property="emps" ofType="Employee">
<!-- 集合泛型类型对应的pojo的主键列映射 ,Employee的主键列映射 -->
<id column="e_id" property="id"/>
<!-- 集合泛型类型对应的pojo的非主键列映射 ,Employee的非主键列映射 -->
<result column="e_name" property="name"/>
</collection>
</resultMap>
</mapper>
|
|
DEBUG [main] - ==> Preparing: select e.id e_id ,e.name e_name,d.id d_id,d.name d_name from department d JOIN employee e ON d.id = e.dept_id WHERE d.id = ?;
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 2
Department [id=1, name=总经办, emps=[Employee [id=1, name=乔峰], Employee [id=3, name=段誉]]]
|
|
MyBatis的逆向工程能自动帮开发者生成数据库表对应的 pojo实体文件,自动生成映射文件
自定生成表的各种(CRUD)的sql语句, 但是只能做单表操作,联合查询还得开发者自己动
使用逆向工程得先在Eclipse安装逆向工程的插件
|
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="context1">
<!-- 注释构建 -->
<commentGenerator>
<!-- 去掉所有的注释 -->
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 数据库四要素 -->
<jdbcConnection connectionURL="jdbc:mysql://localhost:3306/mybatis"
driverClass="com.mysql.jdbc.Driver"
password="admin"
userId="root" />
<!-- 实体类 : pojo
targetPackage : 实体类生成后存放的包
targetProject : 存放的目录一般都放在 src下面
-->
<javaModelGenerator targetPackage="cn.zj.mybatis.pojo" targetProject="mybatis-generator/src" />
<!-- 映射文件 -->
<sqlMapGenerator targetPackage="cn.zj.mybatis.mapper" targetProject="mybatis-generator/src" />
<!-- 操作接口
type 生成映射的形式
ANNOTATEDMAPPER : 纯注解的,没有xml映射
XMLMAPPER : 生成的有xml映射文件
-->
<javaClientGenerator targetPackage="cn.zj.mybatis.mapper" targetProject="mybatis-generator/src" type="XMLMAPPER" />
<!-- 要生成对应表的配置
tableName : 数据库表名
//如果下面全部是true,mybatis直接可以使用纯面向对象开发
enableCountByExample : 是否生成查询总数的 Example
enableDeleteByExample : 是否生成删除的 Example
enableSelectByExample : 是否生成查询集合的 Example
enableUpdateByExample : 是否生成修改的 Example
-->
<table tableName="user" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
<table tableName="employee" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
<table tableName="department" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
</context>
</generatorConfiguration>
|
MyBatis_02 框架的更多相关文章
- 避免重复造轮子的UI自动化测试框架开发
一懒起来就好久没更新文章了,其实懒也还是因为忙,今年上半年的加班赶上了去年一年的加班,加班不息啊,好了吐槽完就写写一直打算继续的自动化开发 目前各种UI测试框架层出不穷,但是万变不离其宗,驱动PC浏览 ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
- 旺财速啃H5框架之Bootstrap(五)
在上一篇<<旺财速啃H5框架之Bootstrap(四)>>做了基本的框架,<<旺财速啃H5框架之Bootstrap(二)>>篇里也大体认识了bootst ...
- Angular企业级开发(5)-项目框架搭建
1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...
- Scrapy框架爬虫初探——中关村在线手机参数数据爬取
关于Scrapy如何安装部署的文章已经相当多了,但是网上实战的例子还不是很多,近来正好在学习该爬虫框架,就简单写了个Spider Demo来实践.作为硬件数码控,我选择了经常光顾的中关村在线的手机页面 ...
- 制作类似ThinkPHP框架中的PATHINFO模式功能
一.PATHINFO功能简述 搞PHP的都知道ThinkPHP是一个免费开源的轻量级PHP框架,虽说轻量但它的功能却很强大.这也是我接触学习的第一个框架.TP框架中的URL默认模式即是PathInfo ...
- 旺财速啃H5框架之Bootstrap(四)
上一篇<<旺财速啃H5框架之Bootstrap(三)>>已经把导航做了,接下来搭建内容框架.... 对于不规整的网页,要做成自适应就有点玩大了.... 例如下面这种版式的页面. ...
- 一起学 Java(三) 集合框架、数据结构、泛型
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
- Hibernatel框架关联映射
Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...
随机推荐
- NSCTF 2017-pwn2
目录 程序基本信息 程序漏洞 整体思路 exp脚本 发现的问题 内容参考 程序基本信息 32位动态链接程序,开启了数据段不可执行以及栈溢出保护 程序漏洞 在函数中sub_80487fa中有一个格式化字 ...
- Chrome接口调试工具
网页接口测试工具开发背景 在web开发中,服务器端和客户端的开发和测试必不可少,但是测试的工作往往需要服务器端完成之后,客户端才能进行测试,这无疑延后了测试流程,导致服务器端开发完成后,无法进行充分的 ...
- clickhouse redash---olap
curl -XPUT '10.1.193.250:9200/ngx-logstash-2019-06/_settings' -d ' { "index.mapping.total_field ...
- Hibernate HQL和QBC
OID查询 一.什么是OID查询 根据对象的OID主键进行检索 二.OID查询方式 1. get方法 当get()方法被调用的时候就会立即发出SQL语句 并且返回的对象也是实际的对象 使用get()和 ...
- 从零配置webpack(react+less+typescript+mobx)
本文目标 从零搭建出一套支持react+less+typescript+mobx的webpack配置 最简化webpack配置 首页要初始化yarn和安装webpack的依赖 yarn init -y ...
- SpringBoot+Mybatis+Maven+MySql小案例
数据准备: 建表t_user ,插入数据..... 创建工程 构建pom.xml <?xml version="1.0" encoding="UTF-8" ...
- 使用Docker快速搭建Tensorflow开发环境
当我刚开始学习使用scikit-learn时,总是会出现各种各样的包依赖问题,兜兜转转了一遍才全部安装好,现在的机器学习算法开发者大都使用tensorflow.pytorch来实现自己的想法,但依然会 ...
- Qt编写控件属性设计器6-动态属性
一.前言 之前就提过,Qt的属性机制强大到爆,这次的动态属性功能就是要让他爆,很难想象只要一行代码即可widget->setProperty("value", value); ...
- Django补充之模板语言
路由系统 Django页面详情以及分页 举个例子: 有一组后台数据,需要展示到页面上,但由于数据量较大,那就需要做分页了吧,那么怎么才能将页面详情和分页都融合进去呢,Django里的路由系统加上正则表 ...
- 设置Android模拟器的窗口大小
Android SDK 中两个位置可以设置Android模拟器的窗口大小 1.设置Android模拟器的分辨率 Android Virtual Device Manager中创建AVD时,窗口中部Re ...
