mybatis概念
-
概念:一个持久层框架
-
作用:ORM将sql语句映射成实体类
-
特点:
1、灵巧
2、半自动化
3、适用中小型项目的开发
mybatis入门

1、创建mybatis-config.xml文件
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
2、创建映射文件UserMapper.xml
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="user">
<select id="selectUser" resultType="com.hemi.mybatis.bean.User">
select * from user where uid=1;
</select>
</mapper>
3、获取xml配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
4、创建SqlSessionFactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
5、获取SqlSession
SqlSession sqlSession = factory.openSession();
6、调用SqlSession的selectOne(命名空间.id名称)
Object object = sqlSession.selectOne("user.selectUser");
7、关闭SqlSession
sqlSession.close();
增删改
增

<insert id="insertUser" parameterType="com.hemi.mybatis.bean.User">
<!-- 通过#{属性名}来获取对象的值 -->
insert into user (username,password) values(#{username},#{password});
</insert>
改
<update id="updateUser" parameterType="com.hemi.mybatis.bean.User">
update user set username=#{username},password=#{password} where uid=#{uid}
</update>
删

<delete id="deleteUser" parameterType="int">
delete from user where uid=#{value}
</delete>
映射器接口开发
一、定义一个接口
public interface TypeMapper {
Type selectType(int typeid);
}
二、定义一个mapper.xml的映射文件
mapper文件的要求:
1.namespace的值就是对象接口的全类名,类名要和xml文件的名称一致
2.id的值就是抽象方法名
3.resultType的值必须和抽象方法的返回值一致(多条则与集合的泛型类一致)
4.parameterType的值和抽象方法的参数类型一致
注意:mapper.xml文件的约束是mapper.dtd,不是config.dtd
三、使用
将mybatis入门步骤中的步骤六改为如下代码:
TypeMapper mapper=sqlSession.getMapper(TypeMapper.class);
Type type=mapper.selectType(1);
动态SQL
如果
SELECT * FROM good INNER JOIN type ON good.type = type.typeid其中1 = 1
<if test = "gname!= null and gname!=''" >
and gname like oncat('%',#{gname},'%')
</if>
注意:
1、字符串的拼接建议使用concat来代替${}(可以防止SQL注入)
2、判断条件中获取数据不用加#{},与EL表达式不一样
where
where可以自动去除第一个and和or
<where>
<if test = "gname!= null and gname!=''" >
and gname like concat('%',#{gname},'%')
</ if>
<if test = "typename!= null and typename!=''" >
and typename like concat('%',#{typename},'%')
</ if>
</ where>
否则选择
作用:组合使用,相当于if else if else
<select id = "findActiveBlogLike"
resultType = "Blog" >
SELECT * FROM BLOG WHERE state ='ACTIVE'
<select>
<when test = "title!= null" >
AND title like #{title}
</ when>
<when test = "author!= null and author.name!= null" >
AND author_name like #{author.name}
</ when>
<otherwise>
AND status= 1
</ otherwise>
</ choose>
</ select>
组
update good
<set>
<if test = "gname!= null and gname!=''" >
gname =#{gname}
</ if>
<if test = "gprice!= null and gprice!=''">
gprice =#{gprice}
</ if>
</ set>
where gid =#{gid}
trim
作用:去除多余字符串
两种常见的用法
1、where 去除第一个条件的 and/or
prefix:字首前缀
prefixOverrides:去除第一个指定的字符串
select * from good
<trim prefix = "where" prefixOverrides = "and/or" >
<! - 添加哪里在前面,并且去除第一个和 - >
<if test = "gname!= null and gname!=''" >
and gname like concat('%',#{gname},'%')
</ if>
<if test = "typename!= null and typename!=''" >
and typename concat('%',#{typename},'%')
</ if>
</ trim>
2、set
prefix
suffixOverrides:去除最后指定的字符串
update good
<trim prefix = "set" suffixOverrides = "," >
<! - 添加set在前面,并且去除最后一个, - >
<if test = "gname!= null and gname!=''" >
good.gname =#{good.gname}
</ if>
<if test = "gprice!= null and gprice!=''" >
gprice =#{gprice}
</ if>
</ trim>
foreach
作用:动态循环拼接SQL部分内容
1、open属性代表在集合前面添加的字符串
2、close属性代表在集合后面添加的字符串
3、separator属性代表集合分割使用的字符串
4、collection代表被循环的集合,值可以是list,map,arrays
常用方法:
<select id = "selectGoodByGid" parameterType = "list" resultType = "Good" >
select gid,gname,gprice,count,type typeid from good
<where>
gid in
<foreach item = "item" collection = "list" open = "(" separator = "," close = ")" >
#{gid}
</ foreach>
</ where>
</ select>
resultMap(结果映射)
作用:
1.可以将表字段与javabean对象属性名进行映射
2.将映射抽取出来,可以给多个语句所使用
<!-- type:最终返回的数据类型 id:resultMap的名称 -->
<resultMap type="Good" id="GoodResultMap">
<!-- 建议将主键设置为id -->
<id column="good_id" property="gid"/>
<!-- 通过result将数据库字段与java对象的属性进行一一映射 -->
<result column="good_name" property="gname"/>
<result column="good_price" property="gprice"/>
<!--
下面的映射可以不用写,因为mybatis中默认设置autoMappingBehavior为PARTIAL,局部自动映射,但是属性名必须和字段名一致
-->
<!-- <result column="count" property="count"/> -->
</resultMap>
映射的原理
1.先拿到类型指定的类的Class对象
2.通过反射newInstance创建对象
3.获取属性中属性名,通过反射的方式调用对应的set()方法对象
4.调用方法对象的invoke()方法,将查询到的数据赋值进去
5.返回封装好的对象
关联嵌套结果(结果映射与结果映射相互嵌套)
<resultMap type= "Good" id = "GoodResultMap" >
<id column= "good_id" property= "gid" />
<result column= "good_name" property= "gname" />
<结果 列= “good_price” 属性= “gprice” />
<! - 一对一的映射关系,选择关联 - >
<! - 内部嵌套 - >
<association property = "type" javaType = "Type" >
<id column = "typeid " property = "typeid" />
<result column = "typename" property = "typename" />
</ association>
</ resultMap>
上面的关联部分可以改造为<association property="type" javaType="Type" resultMap="TypeResultMap"></association>
<! -结果映射外部嵌套- >
<resultMap type= "Type" id = "TypeResultMap" >
<! -建议将主键设置为ID - >
<id column= "typeid" property= "typeid" / >
<result column = "typename" property = "typename" />
</ resultMap>
关联嵌套查询
<result column type = "Good" id = "GoodResultMap1" >
<id column = "good_id" property = "gid" />
<result column = "good_name" property = "gname" />
<result column = "good_price" property = "gprice" />
<! - column:要传递给下面的语句的参数 - >
<! - javaType:该关联返回的数据类型 - >
<! - select:另一个查询语句的statementId- - >
<association property = "type" javaType = "Type" select = "selectTypeByTypeId" column = "type" />
</ resultMap>
<select id = "typeTypeByTypeId" resultType = "Type" >
select * from type where typeid =#{value}
</select>
集合嵌套结果
<colleciton property = "javabean中的属性名" javaType = "List" ofType = "list集合中的元素类型" >
<id column = "主键字段名" property = "元素类型的属性名" />
<result column = "字段名" property = "元素类型的属性名" />
</ colleciton>
集合嵌套查询
<colleciton property = "javabean中的属性名" javaType = "List" ofType = "list集合中的元素类型" select = "selectUser" column = "uid" />
<select id = "selectUser" resultMap = "User" >
select * from user where uid =#{value}
</select>
注意:
自动映射在主配置文件中配置了
<settings>
<! - 虽然默认是PARTIAL,但是建议写上 - >
<setting name = “autoMappingBehavior” value = "PARTIAL" />
</ settings>
缓存
一级缓存(同一个SqlSession中执行同一个语句)
默认开启
二级缓存(同一个命名空间中,不同的SqlSession执行同一个语句)
配置1,在主配置文件<settings>中设置cacheEnabled="true"和aggressiveLazyLoading="false"
配置2,映射文件中配置<cache></cache> ,要使用二级缓存的语句中添加useCache="true"
注意:调用session.close()时候才会将数据写入二级缓存
网友评论