MyBatis 笔记
ORM object relation mapping
mybatis是半orm框架
MyBatis是基于JDK动态代理
MyBatis 框架使用
1.maven中导入依赖
<!--mysql-jdbc:mysql必选,JDBC驱动 --> <!--依赖传递导入:protobuf-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java-version}</version> </dependency> <!--mybatis:--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis-version}</version> </dependency>
2.编写mybatis.xml配置文件
mybatis的配置文件就是配置两个东西,第一个是数据库基本信息(数据库连接),第二个指定mapper文件的路径;
<?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> <!--mybatis环境设置--> <environments default="dev"> <!--dev 环境--> <environment id="dev"> <!--事务处理者(Spring中由transactionManager) --> <transactionManager type="jdbc"/> <dataSource type="pooled"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test_jdbc?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="52611314"/> </dataSource> </environment> </environments> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
3.编写对应的映射文件
mapper文件 配置内容
1>.接口的全类名
2>.结果映射配置(将对象属性与数据库字段匹配的规则)
3>.增删改查操作<?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 namespace="com.wzz.practise.dao.IUserDao"> <resultMap id="userMap" type="com.wzz.practise.service.bo.User"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="password" property="password"/> <result column="sex" property="sex"/> <result column="job_name" property="job_name"/> </resultMap> <select id="get" resultMap="userMap"> select * from user </select> <select id="queryByNameAndPassword" resultMap="userMap"> select * from user where name=#{name} and password=#{password} </select> <select id="queryALl" resultMap="userMap"> select * from user </select>
<!--下面采用trim -->
<select id="queryCondition" resultMap="userMap"> select * from user <trim prefix="where" prefixOverrides="and|or"> <if test="sex != null"> sex=#{sex} </if> <if test="job_name != null"> and job_name=#{job_name} </if> </trim> limit #{startIndex},#{size} </select> <!--涉及到key的问题,如果是mysql,设置了自增主键,就是在插入数据后,会把主键设置上,在插入方法时,可以将这个新增的id再复制给这个对象(java的参数传递方式)--> <insert id="addUser"> <selectKey order="AFTER" keyProperty="id" resultType="int"> select last_insert_id() </selectKey> insert into user(name,password,sex,job_name) values (#{name},#{password},#{sex},#{job_name}) </insert> <delete id="deleteUser"> delete from user where id=#{id} </delete> <delete id="deletePartUser"> delete from user <where> <foreach collection="ids" item="id" separator="or"> id=#{id} </foreach> </where> </delete> <update id="updateUser"> update users <set> <if test="name != null">name=#{name}</if> <if test="password != null">name=#{password}</if> <if test="sex != null">name=#{sex}</if> <if test="job_name != null">name=#{job_name}</if> </set> </update> </mapper>
小知识点:
多表查询:
一对一
查询方式:
1.嵌套结果,使用sql语句(join操作),将两个表的数据直接关联在一起查询;
2.嵌套查询,分为两步查询,第一步-->select * from 主表;第二步-->select * from 从表 where id=主表id(主表有几条它就会有几条);
示例1(对应1):
<?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 namespace="com.wzz.practise.dao.IUserDao"> <resultMap id="userMap" type="com.wzz.practise.service.bo.User"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="password" property="password"/> <result column="sex" property="sex"/> <result column="job_name" property="job_name"/> </resultMap> <resultMap id="studentMap" type="com.wzz.practise.service.bo.Student"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="gender" property="gender"/> <result column="birthday" property="birthday"/> </resultMap> <resultMap id="userAndStudent" type="com.wzz.practise.service.bo.User" extends="userMap"> <association property="student" resultMap="studentMap"/> </resultMap>
<select id="findSlave" resultMap="userAndStudent"> select <include refid="userColumn"> <property name="alias" value="t1"/> </include>, <include refid="studentColumn"> <property name="alias" value="t2"/> </include> from user t1 inner join student t2 on t1.id=t2.id; </select> </mapper>
示例2(对应2):
<?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 namespace="com.wzz.practise.dao.IUserDao"> <resultMap id="userMap" type="com.wzz.practise.service.bo.User"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="password" property="password"/> <result column="sex" property="sex"/> <result column="job_name" property="job_name"/> <association property="student" column="id" select="assist" fetchType="eager"/> </resultMap> <select id="findSlave" resultMap="userMap"> select * from user </select> <select id="assist" resultMap="studentMap" > select * from student where id=#{id} </select> </mapper>
**主要差别:resultMap可以是同样的,嵌套结果就是直接用sql查出对应数据,使用sql语句进行关联关系;嵌套查询是查询时可以用两条sql语句,第一条查所有主表信息,第二条根据关联条件进行查从表信息,关联有mybatis做,但是这样做的坏处在于查询结果类似于左连接的查询,不会查出两者交集。
一对多
查询方式:
嵌套结果:
嵌套查询:
示例1
示例2
**<sql>语句可以跨mapper文件使用
MyBatis缓存
Mybatis一级缓存:
默认开启且无法关闭
SqlSession范围,任何的不相关的更新操作也会使它失效,粒度较粗
MyBatis二级缓存:
默认不开启,需要显式开启
SqlSessionFactory范围,粒度更细,粒度为一个mapper名称空间
二级缓存清空条件是同一命名空间的增删改操作会清空本空间的缓存
<cache-ref>可以将两个名称空间的缓存关联(指使用一个名称空间)
预编译SQl
1.防止SQl注入
2.JDBC侧减少解析,只替换参数,数据库侧可以减少数据库解析sql的过程,减少生成执行过程;

浙公网安备 33010602011771号