一、Mybatis简介
1、使用数据连接池初始化连接资源
2、将sql语句抽取到xml配置文件中
3、使用反射、内省等底层技术,自动将实体与表进行属性字段的自动映射。
mybatis:是一个优秀Java持久层框架,程序员只需要关心sql语句。mybatis隐藏了jdbc繁杂的api。mybatis自动完成实体与表之间的映射关系
Mybatis官网
http://www.mybatis.org/mybatis-3/
二、MyBatis开发步骤:
添加Mybatis坐标
创建user表
编写User实体
编写映射文件UserMapper.xml(sql语句)
编写核心文件SqlMapConfig.xml(核心配置)
编写测试类
三、语法碎解
创建resources下包时,例: com/itheima/mapper
web.xml配置时(如果是maven分模块开发,加载applicationContext.xml):<param-value>classpath*:applicationContext*.xml</param-value>
1)Mybatis核心配置文件
<properties resource="jdbc.properties" /> 加载jdbc配置文件
<environments defalut=""> 指定默认的环境名称
<environment id=""> 指定当前的环境名称
<transactionManager type="JDBC" /> 指定当前数据源类型是JDBC是JDBC
<dataSource type="POOLED" > 指定当前数据类型是连接池
<property name="driver" value"${jdbc.driver}" /> 数据源配置的基本参数
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
自定义别名
<typeAliases>
<typeAlias type="com.itheima.domain.User" alias ="user" />
</typeAliases>
基本数据类型myBatis以自动定义别名 , 例:java.lang.Integer-->int
加载映射文件:<mapper resource="XX" />
属性:
resource:使用相对路径的资源引用
url: 使用完全限定资源定位符 url="file:///var/..."
class: 使用映射器接口实现类的完全限定类名 ,注解使用
package: 将包内的映射器接口实现全部注册为映射器 <package name="XX">
2)测试
Mybatis的Dao实现(代理方式)
Mapper接口开发需要遵循以下规范
1、Mapper.xml文件中的namespace与mapper接口的全限定名相同
2、Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3、Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4、Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
//获取资源
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//创建sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//创建会话
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//获取资源
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();//直接调方法名即可
关流
sqlSession.close()
3)Mybatis的动态sql
<where></where> //相当于where 1=1
if
<if test="id!=0"></if>
foreach
<foreach collection="list" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
//sql抽取
<sql id="XX"></sql>
//引用sql
<include refid="XX"></include>
4) Mybatis核心配置文件深入
typeHandlers标签:数据库类型转Java类型
有时需要自定义类型转换
具体做法:实现org.apache.ibatis.type.TypeHandler接口 或者继承一个很便利的类org.apache.ibatis.type.BaseTypeHandler,然后可以选择性的将它映射到一个JDBC类型。例如需求:一个Java中的Date数据类型,我想将之存到数据库的时候存成一个1970年至今的毫秒数,取出来时转换成Java的Date,即Java的Date与数据库的varchar毫秒值之间的转换。
开发步骤
1、定义转换类继承类BaseTypeHandler<T>
2、覆盖4个未实现的方法,其中setNonNullParameter为Java程序设置数据到数据库的回调方法,getNullableResult为查询时 mysql的字符串类型转换成Java的Type类型的方法
3、在Mybatis核心配置文件中进行注册
<typeHandlers>
<typeHandler handler="com.itheima.typeHandler.StringTypeHandler" />
</typeHandlers>
4、测试转换是否正确
5) mybatis核心配置文件深入
plugins标签
mybatis可以使用第三方插件来对功能进行扩展,分页助手PageHelper是将分页的复杂操作进行封装,使用简单的方式即可获得分页的相关数据
开发步骤
1、导入通用PageHelper的坐标
<!--分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
2、在mybatis核心配置文件中配置PageHelper插件
<!--配置分页助手插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
//配置方言
<property name="dialect" value="mysql" />
</plugin>
</plugins>
3、测试分页数据获取
PageHelper.startPage(,);
获得与分页相关的参数
PageInfo<User> pageInfo = new PageInfo<User>(userList);
当前页:pageInfo.getPageNum()
每页条数: pageInfo.getPageSize()
...
6)Mybatis多表查询
Mybatis多表配置方式
一对一:使用<resultMap>配置
<resultMap id="orderAndUser" type="com.itheima.domian.Order">
<id column="oid" property="oid" />
<result column="ordertime" property="ordertime" />
<result column="total" property="total" />
<association property="user" javaType="com.itheima.domian.User" >
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</association>
</resultMap>
一对多:使用<resultMap>+<collection>
<resultMap id="userOrder" type="user">
<id column="uid" property="id" />
<result column="username" property="username" />
<result column="password" property="password" />
<collection property="orderList" ofType="order" >
<id column="oid" property="oid" />
<result column="ordertime" property="ordertime" />
<result column="total" property="total" />
</collection>
</resultMap>
//JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型。
多对多:使用<resultMap>+<collection>
只是多了一个中间表,参考一对多
7)Mybatis的注解开发
@Insert: 实现新增
@Update: 实现更新
@Delete: 实现删除
@Select: 实现查询
@Result: 实现结果集封装
@Results: 可以与@Result一起使用,封装多个结果集
@One: 实现一对一
@Many: 实现一对多结果集封装
Mybatis使用注解实现复杂映射
@Results:代替标签<resultMap>,该注解可以使用单个@Result注解,也可以使用@Result集合,使用格式:@Results({@Result(),@Result})或者(@Result())
@Result 代替了<id>和<Result>标签
@Result属性介绍
column:数据库的列名
property: 需要装配的属性名
one: 需要使用的@One注解(@Result(one=@one) ())
many: 需要使用的@Many注解 (@Result(many=@Many) ())