mybatis是一个半自动的持久层ORM框架
Object Relational Mapping【对象 关系 映射】,将Java中的对象与数据库中表建议映射关系
Mybatis是一个半自动化,需要手写SQL;Hibernate是全自动化,无需手写SQL
Mybatis是在JDBC基础之上封装的;JDBC是Java提供的能够直接使用Java代码来编写SQL语句并执行数据库操作的标准API,JDBC需要开发人员在Java代码中编写sql,耦合度高,mybatis可以通过xml方式和Java代码解耦
<select id="selectUser" resultType="User">
select id,user_name as userName from user where id = #{id}
select>
<resultMap id="userMap" type="com.lby.dto.User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
resultMap>
<select id="selectUser" resultMap="userMap">
select id,user_name from user where id = #{id}
select>
<resultMap id="userAndDeptResultMapAssociation" type="employee">
<!-- 定义主键字段与属性关联关系 -->
<id column="id" property="id"></id>
<!-- 定义非主键字段与属性关联关系-->
<result column="user_name" property="userName"></result>
<!-- 用戶所属部门,自定义关联关系-->
<association property="dept" javaType="com.lby.mybatis.pojo.Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap>
<resultMap id="userAndEmpResultMap" type="dept">
<id property="deptId" column="dept_id"></id>
<result property="deptName" column="dept_name"></result>
<collection property="empList" ofType="com.lby.mybatis.pojo.Employee">
<id column="id" property="id"></id>
<result column="last_name" property="lastName"></result>
<result column="email" property="email"></result>
<result column="salary" property="salary"></result>
</collection>
</resultMap>
<insert id="addUser" useGeneratedKeys="true" keyProperty="id" parameterType="User">
insert into user (id,user_name) values (#{id},#{userName})
insert>
@Param可以将接口的参数和xml中#{}的参数进行映射
public List<Employee> selectEmpByNamed(@Param("lName")String lastName, @Param("salary") double salary);
Integer updateByPrimaryKey(UserDTO dto);
<update id="updateByPrimaryKey" parameterType="UserDTO" >
update user
<set >
<if test="userName != null" >
user_name = #{userName,jdbcType=VARCHAR},
if>
set>
where id = #{id,jdbcType=BIGINT}
update>
<select id="queryUserList" resultMap="UserMap" parameterType="java.util.Map">
select
<include refid="Base_Column_List" />
from user u
where 1=1
<if test="userName != null">
and u.user_name = #{userName,jdbcType=VARCHAR}
if>
ORDER BY u.id
select>
public List<User> selectUserList(@Param("ids") List<Integer> ids);
<select id="selectUserByIds" resultType="User">
SELECT `id`, user_name FROM `tbl_employee`
<where>
`id` in
(
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
)
</where>
</select>
public User selectUserById(int id);
<select id="selectUserById" resultType="user">
SELECT id, user_name as userName FROM user WHERE id= #{id}
</select>
public List<User> selectAllUser();
<select id="selectAllUser" resultType="user">
SELECT id, user_name as userName FROM user
</select>
如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型。
@MapKey("id")
public Map<Integer,User> selectUserReturnMap();
<select id="selectUserReturnMap" resultType="map">
SELECT id, user_name as userName FROM user
</select>
@Data
public class User {
private int id;
private String username;
private List<Order> orders;
}
@Data
public class Order {
private int id;
private int userId;
private String product;
}
import java.util.List;
public interface UserMapper {
User getUserById(int userId);
}
<!-- UserMapper.xml -->
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="userResultMap" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<!-- 使用association的select属性实现分步查询 -->
<association property="orders" javaType="List" select="getOrdersByUserId"/>
</resultMap>
<select id="getUserById" resultMap="userResultMap" parameterType="int">
SELECT * FROM users WHERE id = #{userId}
</select>
<select id="getOrdersByUserId" resultMap="orderResultMap" parameterType="int">
SELECT * FROM orders WHERE user_id = #{userId}
</select>
</mapper>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<association property="dept"
select="com.atguigu.mybatis.mapper.DeptMapper.selectDeptByDeptId"
column="dept_id"
fetchType="eager">
association>
<sql id="emp_col">
id,
last_name,
email,
salary
sql>
<sql id="select_employee">
select
id,
last_name,
email,
salary
from
tbl_employee
sql>
<select id="selectEmpByOpr" resultType="employee">
<include refid="select_employee">include>
<where>
<if test="id != null">
and id = #{id}
if>
<if test="lastName != null">
and last_name = #{lastName}
if>
<if test="email != null">
and email = #{email}
if>
<if test="salary != null">
and salary = #{salary}
if>
where>
select>
<select id="selectEmpByOprTrim" resultType="employee">
<include refid="select_employee">include>
<trim prefix="where" suffixOverrides="and">
<if test="id != null">
id = #{id} and
if>
<if test="lastName != null">
last_name = #{lastName} and
if>
<if test="email != null">
email = #{email} and
if>
<if test="salary != null">
salary = #{salary}
if>
trim>
select>
<update id="updateEmpByOpr">
update
tbl_employee
<set>
<if test="lastName != null">
last_name=#{lastName},
if>
<if test="email != null">
email=#{email},
if>
<if test="salary != null">
salary=#{salary}
if>
set>
where
id = #{id}
update>
<select id="selectEmpByOneOpr" resultType="employee">
select
<include refid="emp_col">include>
from
tbl_employee
<where>
<choose>
<when test="id != null">
id = #{id}
when>
<when test="lastName != null">
last_name = #{lastName}
when>
<when test="email != null">
email = #{email}
when>
<when test="salary != null">
salary = #{salary}
when>
<otherwise>
1=1
otherwise>
choose>
where>
select>
<select id="selectEmpByIds" resultType="employee">
select
id,
last_name,
email,
salary
from
tbl_employee
<where>
id in(
<foreach collection="ids" item="id" separator=",">
#{id}
foreach>
)
where>
select>
<insert id="batchInsertEmp">
INSERT INTO
tbl_employee(last_name,email,salary)
VALUES
<foreach collection="employees" item="emp" separator=",">
(#{emp.lastName},#{emp.email},#{emp.salary})
foreach>
insert>
mybaits有二级缓存
<setting name="cacheEnabled" value="true"/>
Executor、StatementHandler 、ParameterHandler、ResultSetHandler,Mybatis的插件会对上面的四个组件进行动态代理。