一、Mybatis的特性
- MyBatis支持定制化SQL、存储过程以及高级映射
- Mybatis避免了几乎所有的JDBC代码和手动设置参数以及结果集解析操作
- Mybatis可以使用简单的
- Mybatis是一个半自动的ORM(对象关系映射)框架
二、Mybatis的优势
*轻量级,性能出色
*SQL和Java编码分开,功能边界清晰。Java代码专注业务逻辑,SQL语句专注数据
*能够与Spring很好的集成
三、Mybatis的基本用法
1.添加Mybatis所需依赖
1.Mybatis核心包
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency>2.MySQL驱动
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.3</version> </dependency>3.junit单元测试
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>4.log4j日志
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency><setting name="aggressiveLazyLoading" value="false"/><setting name="lazyLoadingEnabled" value="true"/>2.properties标签:关联外部属性文件 <properties resource="jdbc.properties"/>
3.mappers标签:注册Mapper配置文件
- mapper标签:具体注册一个Mapper配置文件
<mapper resource="mappers/EmployeeMapper.
- package标签:指定Mapper配置文件所在包,全部注册
注意:如果工程是Maven工程,那么Mapper配置文件还是要放在resources目录下,且目录结构要和Mapper接口的目录结构保持一致
<package name="com.atguigu.mybatis.mapper"/>
- environments标签:配置Mybatis使用的环境,通常会转移到Spring中配置
<environments default="development"> <!-- environment表示配置Mybatis的一个具体的环境 --> <environment id="development"> <!-- Mybatis的内置的事务管理器 --> <transactionManager type="JDBC"/> <!-- 配置数据源 --> <dataSource type="POOLED"> <!-- 建立数据库连接的具体信息 --> <property name="driver" value="${wechat.dev.driver}"/> <property name="url" value="${wechat.dev.url}"/> <property name="username" value="${wechat.dev.username}"/> <property name="password" value="${wechat.dev.password}"/> </dataSource> </environment> </environments>一、基本增删改查操作
1.select标签
resultType属性:指定封装查询结果的实体类类型
1.简单数据类型
<select id="selectCount" resultType="int"> SELECT count(*) from t_emp </select>2.Map类型
<select id="selectEmpNameAndMaxSalary" resultType="map"> select emp_name,max(emp_salary) from t_emp </select>3.实体类类型
- 返回单个实体类
<select id="selectEmpById" resultType="com.atguigu.mybatis.entity.Emp"> select emp_id ,emp_name,emp_salary from t_emp where emp_id=#{empId} </select>- 返回实体类的集合:mybatis再将查询结果封装到实体类对象之后,会自动把多个实体类对象放在List集合返回
<select id="selectAll" resultType="com.atguigu.mybatis.entity.Emp"> select * from t_emp; </select>resultMap属性
1.映射实体类自身属性<!--专门声明一个resultMap设定column到property之间的对应关系--> <resultMap id="empMapResultMap" type="com.atguigu.mybatis.entity.Emp"> <id column="emp_id" property="empId"/> <result column="emp_name" property="empName"/> <result column="emp_salary" property="empSalary"/> </resultMap> <!--Emp selectWithResultMap(long empId)--> <select id="selectWithResultMap" resultMap="empMapResultMap"> select emp_id ,emp_name,emp_salary from t_emp where emp_id = #{empId} </select>2.映射关联属性
- 对一:association标签和javaType属性
<resultMap id="selectOrderWithCustomerResultMap" type="com.atguigu.mybatis.entity.Order"> <id column="order_id" property="orderId"/> <result column="order_name" property="orderName"/> <result column="customer_id" property="customerId"/> <!-- 使用association标签配置"对一"关联关系 --> <association property="customer" javaType="com.atguigu.mybatis.entity.Customer"> <result column="customer_id" property="customerId"/> <result column="customer_name" property="customerName"/> </association> </resultMap> <!--Order selectOrderWithCustomer(long orderId);--> <!--关键词:association javaType--> <select id="selectOrderWithCustomer" resultMap="selectOrderWithCustomerResultMap"> select order_id,order_name,c.customer_id,customer_name from t_order o left join t_customer c on o.customer_id = c.customer_id where order_id = #{orderId} </select>- 对多:collection标签和ofType属性
<resultMap id="selectCustomerWithOrderListResultMap" type="com.atguigu.mybatis.entity.Customer"> <id column="customer_id" property="customerId"/> <result column="customer_name" property="customerName"/> <collection property="orderList" ofType="com.atguigu.mybatis.entity.Order"> <result column="order_id" property="orderId"/> <result column="order_name" property="orderName"/> <result column="customer_id" property="customerId"/> </collection> </resultMap> <!--Customer selectCustomerWithOrderList(long customerId);--> <!--关键词:collection ofType--> <select id="selectCustomerWithOrderList" resultMap="selectCustomerWithOrderListResultMap"> select c.customer_id,customer_name,order_id,order_name from t_customer c left join t_order o ON c.customer_id = o.customer_id where c.customer_id = #{customerId} </select>
2.insert标签
- useGeneratedKey属性:设置为true时表示使用数据库自增主键
- keyProperty属性:指定实体类中接收自增主键的属性
<insert id="getGeneratedKeys" useGeneratedKeys="true" keyProperty="empId"> insert into t_emp(emp_name, emp_salary) values (#{empName},#{empSalary}) </insert>3.update标签
<update id="updateEmp"> update t_emp set emp_name=#{empName},emp_salary=#{empSalary} where emp_id=#{empId} </update>4.delete标签
<delete id="deleteEmp"> delete from t_emp where emp_id=#{empId} </delete>二、给SQL传参
#{}:将来变成?占位符${}:将来拼接字符串
三、动态SQL
动态SQL存在的意义是为了解决拼接SQL语句字符串时的痛点问题。
- where标签:会自动去掉标签体内前面、后面多余的and/or
- if标签
<select id="selectEmpByCondition" resultType="com.atguigu.mybatis.entity.Emp"> select * from t_emp <where> <if test="empName != null"> or emp_name = #{empName} </if> <if test="empSalary > 800.00"> or emp_salary > #{empSalary} </if> </where> </select>- set标签
- trim标签
- prefix属性:指定要动态添加的前缀
- suffix属性:指定要动态添加的后缀
- prefixOverrides属性:指定要动态去掉的前缀,使用"|"分隔有可能的多个值
- suffixOverrrides属性:指定要动态驱动的后缀,使用"|"分隔有可能的多个值
<select id="selectEmpByConditionByTrim" resultType="com.atguigu.mybatis.entity.Emp"> SELECT emp_id,emp_name,emp_salary from t_emp <trim prefix="where" suffixOverrides="and|or"> <if test="empName != null"> emp_name = #{empName} or </if> <if test="empSalary < 1000"> emp_salary <#{empSalary} </if> </trim> </select>- sql标签
- foreach标签
- collection属性:指定要遍历的集合
- 使用@Param注解,按照注解指定的集合名来引用
- 没使用@Param注解,则使用默认名collection或list
- item属性:指定遍历出来每个元素的名字
- open属性:指定整个循环把字符串拼好后,字符串整体的前面要添加的字符串
- close属性
- separator属性:循环体之间的间隔符号
- index属性:遍历过程中的索引
- collection属性:指定要遍历的集合
- 注意:在拼接多条SQL语句的时候,需要让连接数据库的地址后面附加一个参数:allowMultiQueries=true
<--批量插入多条数据--><insert id="insertEmpByBatch"> insert into t_emp(emp_name,emp_salary) <foreach collection="empList" index="index" item="emp" open="values" separator=","> (#{emp.empName},#{emp.empSalary}) </foreach> </insert><--批量更新多条数据--><update id="updateEmpByBatch"> <foreach collection="empList" separator=";" item="emp" > update t_emp set emp_name = #{emp.empName},emp_salary = #{emp.empSalary} where emp_id = #{emp.empId} </foreach> </update></mapper>- choose/when/otherwise标签
- include标签:引用已抽取的SQL片段,于sql标签搭配使用
四、开启二级缓存功能
- 使用cache标签:type属性:指定具体的缓存产品,例如EHCache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>原文转载:http://www.shaoqun.com/a/801550.html
writer:https://www.ikjzd.com/w/1280
sonar:https://www.ikjzd.com/w/215
丰趣海淘:https://www.ikjzd.com/w/1716
easy buy:https://www.ikjzd.com/w/2162
一、Mybatis的特性MyBatis支持定制化SQL、存储过程以及高级映射Mybatis避免了几乎所有的JDBC代码和手动设置参数以及结果集解析操作Mybatis可以使用简单的Mybatis是一个半自动的ORM(对象关系映射)框架二、Mybatis的优势*轻量级,性能出色*SQL和Java编码分开,功能边界清晰。Java代码专注业务逻辑,SQL语句专注数据*能够与Spring很好的集成三、Myb
PopSockets:https://www.ikjzd.com/w/907
亚马逊VCS:https://www.ikjzd.com/w/908
Informed.co:https://www.ikjzd.com/w/909
数魔跨境:https://www.ikjzd.com/w/1425.html
赛兔:https://www.ikjzd.com/w/2375
泛亚班拿:https://www.ikjzd.com/w/1262
尴尬 相亲对象是我的初恋男友:http://lady.shaoqun.com/m/a/271744.html
知我怀孕老公前女友趁虚而入:http://lady.shaoqun.com/m/a/270471.html
我是剩女疯狂爱上了闺蜜的男友:http://www.30bags.com/a/251158.html
故事:分手后,喝了一晚上的酒。醒来的时候被一个6岁的帅气弟弟拦住了。"我姐负责。":http://www.30bags.com/a/389407.html
辣眼睛!街头情侣的"不雅行为"被曝光,警察的话亮了:http://lady.shaoqun.com/a/372172.html
故事:分手后,喝了一晚上的酒。醒来的时候被一个6岁的帅气弟弟拦住了。"我姐负责。":http://lady.shaoqun.com/a/372173.html
No comments:
Post a Comment