MyBatis学习总结

MyBatis:类似于Hibernate的ORM持久化框架,支持普通SQL查询,存储过程以及高级映射。MyBatis通过使用简单的XML或注解用于配置和原始映射,将接口和POJO对象映射成数据库中的记录。

由于MyBatis是基于JDBC做了简单映射包装,从性能角度看:

JDBC>MyBatis>Hibernate

Hibernate: 开放源代码的对象关系映射框架

ORM:对象关系映射(Object Relational Mapping,简称ORM)是通过使用描述对象和数据库之间映射的元数据,将面向对象语言程序中的对象自动持久化到关系数据库中。

POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称。没有业务逻辑。



在db.properties里设置数据库信息,放在src下

在src在写batis config文件,比如sqlMapConfig.xml

<properties resource="db.properties"/>    获取db.properties里的属性

 <typeAliases>

<typeAlias type="basic.mybatis.bean.User" alias="User"/>

<package name="basic.mybatis.bean"/>

</typeAliases>

//别名:可以将某个具体类别名,也可以将整个包添加进去,则包里的类全部可以作为别名使用

  <environments default="development">

    <environment id="development">

      <transactionManager type="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>

//environment id="development" 每个数据库环境有唯一的id,可以在build SqlSessionFactory时指定

添加其他非dtd约束方法:preferences->xml catalog->add->File System->找到后确定,在key中加入“\文件名”;在xml的design中,beans右键,edit namespace,add->specify new namespace,browser->select xml catalog entry->找到对应约束,将除文件名之外的路径复制到namespace name中,填写唯一prefix,确定退出后保存即可。


  <mappers>

    <mapper  resource="mapper/UserMapper.xml"/> 

    <mapper url="file:///Users/pipita/eclipse-workspace/MyBatisCollection/src/mapper/UserMapper.xml"/>

    <mapper class="basic.mybatis.mapper.UserMapper"/>

    <package name="basic.mybatis.mapper"/>

</mappers>

resource指定相对路径;url指定绝对路径;class指定唯一的映射类;package指定包,该包下的类都可以使用;后两者需要将mapper文件放在包下

前两者使用的时候不需要mapper类,使用类似

User user = session.selectOne("UserMapper.selectUserById", 1);

这种即可

而后两者需要mapper类和xml文件,使用时类似

UserMapper mapper = session.getMapper(UserMapper.class);

User user = mapper.selectUserById(1);


那么Mybatis的使用方法大致可以分为两种,一种是不要mapper.class接口,直接在mapper.xml文件中写sql语句,在使用时直接通过namespace.id来指定方法,传入参数,进行增删改查。


另一种则是建立mapper接口,并将对应的mapper.xml文件放入同一个包中,通过接口类获得mapper对象,使用mapper接口的方法调用mapper.xml中对应的sql语句,进行增删改查。

这种方法,可以直接使用generator来根据数据库中的表直接生成bean类、接口文件以及xml文件。

generator的配置文件可以参考http://mybatis.org/generator/configreference/xmlconfig.html来写。调用方法:

List<String> warnings = new ArrayList<String>();

boolean overwrite = true;

File configFile = new File("src/config.xml");

ConfigurationParser cp = new ConfigurationParser(warnings);

Configuration config = cp.parseConfiguration(configFile);

DefaultShellCallback callback = new DefaultShellCallback(overwrite);

MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

myBatisGenerator.generate(null);

注意:generator出的文件含有大量无用代码,而且可能会出现一些未知问题。


整体来看,mybatis将数据库中的表与bean类对应,通过mapper接口声明操作方法,在对应的mapper xml中实现sql语句。调用时,使用session通过接口的class获得接口对象,调用方法,获得sql语句的返回结果。

即:bean类对象作为输出结果或输入参数,通过sqlMapConfig.xml->InputStream->SqlSessionFactory->session->mapper.class->mapper.xml来获取所需结果。


一些其他的操作方法:

<sql id="myselect">

select * from user 

</sql>

将一段代码放入sql中,之后引用使用

<include refid="myselect"/>


like "%"#{value}"%"


select * 

from user 

<where

<if test="u_sex != null and u_sex != ''">

AND u_sex = #{u_sex} 

</if>

<if test="u_username != null and u_username != ''">

AND u_username like "%"#{u_username}"%" 

</if>

<if test="u_sex != null">

AND u_cid = #{u_cid}

</if>

</where>

select * 

from user 

<trim prefix="where" suffixOverrides="and|or"

<if test="u_sex != null and u_sex != ''">

AND u_sex = #{u_sex} AND 

</if>

<if test="u_username != null and u_username != ''">

AND u_username like "%"#{u_username}"%" AND 

</if>

<if test="u_sex != null">

AND u_cid = #{u_cid} AND 

</if>

</trim>

if条件生效则加入if中语句,不生效则不加入

trim可以将where语句中的and 或 or 去掉(包括前面和后面的),保证当有条件语句没有生效时语句是正确的,而where只能将前面的and去掉


<resultMap type="UserVoMore" id="uservomorelist">

<id property="u_id" column="u_id"/>

<result property="u_username" column="u_username"/>

<result property="u_sex" column="u_sex"/>

<association property="country" javaType="Country">

<id property="id" column="c_id"/>

<result property="c_countryname" column="c_countryname"/>

</association>

</resultMap>

两张表查找,一对一,使用association

<resultMap type="CountryVoMore" id="countryvomore">

<id property="Id" column="c_id"/>

<result property="c_countryname" column="c_countryname"/>

<result property="c_capital" column="c_capital"/>

<collection property="userList" ofType="User">

<id property="u_id" column="u_id"/>

<result property="u_username" column="u_username"/>

</collection>

</resultMap>

一对多使用collection

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容