mybatis----简单介绍和使用

     进公司后,公司用的Mysql数据库,持久层用的就是Mybatis,开始感觉没有接触过,应该挺难,可是现在看来全是自己吓自己,下面让我来简单介绍一下:

MyBatis是一个基于java的持久层框架

     MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis。MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。(百科查询)

我们把Mybatis的功能架构分为三层:
(1)API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。
(2)数据处理层:负责具体的SQL查找、SQL解析、SQL执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。
(3)基础支撑层:负责最基础的功能支撑,包括连接管理、事务管理、配置加载和缓存处理,这些都是共用的东西,将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。


    MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

接下来是简单的应用:
   mybatis的配置文件:
 conf.xml文件中的内容如下:
<?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>
    <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/mybatis" />
                <property name="username" value="root" />
                <property name="password" value="XDP" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- 注册userMapper.xml文件,
        userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml-->
        <mapper resource="me/gacl/mapping/userMapper.xml"/>
     </mappers>
</configuration>



接下来是实体层:

public class User {

    //实体类的属性和表的字段名称一一对应
    private int id;
    private String name;
  
    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }
}


接下来是sql语句书写的地方:

<?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="me.gacl.mapping.userMapper">
    
    <!-- 
        根据id查询得到一个user对象
     -->
    <select id="getUser" parameterType="int" 
        resultType="me.gacl.domain.User">
        select * from users where id=#{id}
    </select>
</mapper>



接下来是一个接口类(一般是dao层)用于逻辑层调用这个sql语句:
注意:方法名必须得和xml中的id一致
public interface UserMapper { 
    public User getUser(String Id); 


这样就完成了!!简单吧,在公司,像这些简单的增删改,都用一个mapper生成器生成了,不用自己写,但是像那些批量的插入,批量的修改,还有多表查询的sql语句,就得自己来写了,下面是一个批量插入的列子,这样写,不仅效率快,而且返回值就是你插入的条数,插入成功了几条,返回值就是几。

mapper.xml:
<insert id="betchInsert" parameterType="list">
   insert into settlement_detail (id, begin_date, charge_date,
      settlement_amount, merchant_id, merchant_name,
      merchant_style, contact_people, phonenum,
      state, is_deleted, bank_number,
      order_amount, total_coupons, total_score,
      platform_commission, total_back, total_commission_back,
      total_score_back, total_coupons_back, create_by,
      create_time, update_by, update_time
      )
    values
    <foreach collection="list" item="item" separator=",">
      <choose>
        <when test="item.id != null" >
          ( #{item.id},
        </when>
        <otherwise>
          '',
        </otherwise>
      </choose>
      <choose>
        <when test="item.begin_date != null" >
          #{item.begin_date},
        </when>
        <otherwise>
          now(),
        </otherwise>
      </choose>
      <choose>
        <when test="item.charge_date != null" >
          #{item.charge_date},
        </when>
        <otherwise>
          now(),
        </otherwise>
      </choose>
      <choose>
        <when test="item.settlement_amount != null" >
          #{item.settlement_amount},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.merchant_id != null" >
          #{item.merchant_id},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.merchant_name != null" >
          #{item.merchant_name},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.merchant_style != null" >
          #{item.merchant_style},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.contact_people != null" >
          #{item.contact_people},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.phonenum != null" >
          #{item.phonenum},
        </when>
        <otherwise>
          '',
        </otherwise>
      </choose>
      <choose>
        <when test="item.state != null" >
          #{item.state},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.is_deleted != null" >
          #{item.is_deleted},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.bank_number != null" >
          #{item.bank_number},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.order_amount != null" >
          #{item.order_amount},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.total_coupons != null" >
          #{item.total_coupons},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.total_score != null" >
          #{item.total_score},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.platform_commission != null" >
          #{item.platform_commission},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.total_back != null" >
          #{item.total_back},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.total_commission_back != null" >
          #{item.total_commission_back},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.total_score_back != null" >
          #{item.total_score_back},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.total_coupons_back != null" >
          #{item.total_coupons_back},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.create_by != null" >
          #{item.create_by},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.create_time != null" >
          #{item.create_time},
        </when>
        <otherwise>
          now(),
        </otherwise>
      </choose>
      <choose>
        <when test="item.update_by != null" >
          #{item.update_by},
        </when>
        <otherwise>
          0,
        </otherwise>
      </choose>
      <choose>
        <when test="item.update_time != null" >
          #{item.update_time},
        </when>
        <otherwise>
          now())
        </otherwise>
      </choose>
    </foreach>


  </insert>

对应的接口类:
int betchInsert(List<SettlementDetail> newSettlementDetails);

     对应的实体层太多,就不跟你们写了!就是这么简单,对应的批量update也是这么来的(foreach的放置位置很重要,放不对了,就会造成效率低下!!),对应的左联接,右联接语句,等等,你常用的sql语句都可以这么对应到相应的xml文件中,加油吧!!!




评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值