这两天简单整理了一下MyBatis

相关api和jar包这里提供一个下载地址,免得找了

链接:http://pan.baidu.com/s/1jIl1KaE 密码:d2yl

MYBATIS 简单整理与回顾

2.进行相关xml配置

放在根目录下

MYBATIS 简单整理与回顾

3.配置数据库映射文件
这里给个例子文件
 
<?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="com.jhdx.mapper.T_CustomerMapper">
 
      <!-- 通过编号查询用户 -->
      <select >
            select * from t_customer
      </select>
      <select >
      select * from t_customer where id=#{id}
      </select>
      <select >
      select * from t_customer where name like "%"#{name}"%"
      </select>
      <insert >
      insert into t_customer(name,age,tel) values(#{name},#{age},#{tel})
      </insert>
</mapper>
View Code
4.测试,读取配置文件
 
      String resource = "mybatis-config.xml";
      InputStream inputStream=null;
      inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      //创建session工厂
      SqlSession session=sqlSessionFactory.openSession();
      //调用接口方法
      T_CustomerMapper t_CustomerMapper=session.getMapper(T_CustomerMapper.class);
      T_Customer t_Customer = new T_Customer();
      t_Customer=t_CustomerMapper.getCustomerByName("1");
      System.out.println(t_Customer.getName());
View Code
这种是通过代理方式
 
 
当然也可以直接通过session读取xml写的方法
MYBATIS 简单整理与回顾

 

B.一些技巧
1.直接使用注解进行sql的编写和映射
在dao接口里直接使用注解
MYBATIS 简单整理与回顾

 

当然,在配置文件还需要配置一下
      <mappers>
            <!-- 配置映射文件 -->
            <mapper resource="com/jhdx/mapper/T_customerMapper.xml" />
            <mapper class="com.jhdx.mapper.T_customerMapper" />
      </mappers>
View Code
如上图,mappers里配置一个class即可
 
2.关于模糊查询
 
<select >
      select * from t_customer where name like "%"#{name}"%"
</select>
View Code

 

3.关于一对一的关联
a)对结果进行处理,实际只执行一条sql语句,api叫嵌套结果
MYBATIS 简单整理与回顾

 

 
配置一个resultMap即可
标签分别是数据库的字段与实体类的映射
<association></association>标签里是对对象的映射
注意javaType指的是实体类 column指的是 表连接时使用的字段 例如上图的例子中就是c.teacher_id=t.t_id 使用的是teacher_id
一般配置具体字段的标签使用<id></id>配置主键Id <result></result>配置其他的字段 里面的属性分别是 property对应的是实体类的成员变量 column对应的是数据库的字段
 
b)对过程进行处理,实际执行多条sql语句,api叫嵌套查询
MYBATIS 简单整理与回顾

 

如图,首先执行的是select * from Class where c_id=#{id}
接着对结果集进行封装 封装到teacher的时候再执行一条查询语句 注意多了一个select的属性 指的就是第二条查询语句
参数通过 teacher_id传递过去 也就是属性里column的值传递过去
 
4.关于一对多的关联
a)对结果进行处理,实际只执行一条sql语句,api叫嵌套结果
MYBATIS 简单整理与回顾

 

其他和上面的一对一一样 
只是配置 一对多 多的集合的时候 使用
<collection></collection>标签 注意属性了不是javaType而是ofType ,当然实体类也要配置相应的集合成员变量
然后column可以不指定 也可以指定 指定的列名是 根据这个列名可以查询到相关集合对象的那个列
里面的标签还是作相应 实体类的成员变量和数据库字段的对应
 
b)对过程进行处理,实际执行多条sql语句,api叫嵌套查询
 MYBATIS 简单整理与回顾

 

和1对1类似 多了一个 collection标签 注意select 选择的第二条查询语句id以及column是用来传值的
如果说实体类的属性没有设置全的话 查询语句要自己设置别名(与实体类一致) 否则会映射失败
 
  <resultMap >
    <id column="userId" jdbcType="INTEGER" property="userid" />
    <result column="userName" jdbcType="VARCHAR" property="username" />
    <result column="userPwd" jdbcType="VARCHAR" property="userpwd" />
    <collection property="contents" ofType="Content" column="userId" select="selectAllContents"></collection>
  </resultMap>
  <select >
  select * from content where userId=#{userId}
  </select>
View Code

 

5.关于sql标签
这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。它可以被静态地(在加载参数) 参数化. 不同的属性值通过包含的实例变化. 比如:
 
<sql > ${alias}.id,${alias}.username,${alias}.password </sql>
 
这个 SQL 片段可以被包含在其他语句中,例如:
 
<select >
select
<include ref>,
<include ref>
from some_table t1
cross join some_table t2
</select>
View Code
 
属性值可以用于包含的refid属性或者包含的字句里面的属性值,例如:
 
<sql >
${prefix}Table
</sql>
<sql >
from
<include ref/>
</sql>
<select >
select
field1, field2, field3
<include ref>
<property name="prefix" value="Some"/>
<property name="include_target" value="sometable"/>
</include>
</select>
View Code
5.关于动态sql
常用的有if,choose(when,otherwise),trim(where,set),foreach,bind
用法类似于jstl标签
详情还是翻看api吧,里面的例子简单明了,这里就不直接列举了
 
6.关于传入多个参数的问题
常用的有三种方法
1.使用#{0},#{1}....
DAO层的函数方法
Public User selectUser(String name,String area);

对应的Mapper.xml
<select >
    select  *  from user_user_t   where user_name = #{0} and user_area=#{1}
</select>
View Code

 

2.使用map进行传递(不建议,无法通过接口看到传递的是什么参数)
 
此方法采用Map传多参数.
Dao层的函数方法
Public User selectUser(Map paramMap);

对应的Mapper.xml
<select >
  select  *  from user_user_t  where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>

Service层调用
Private User xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put(“userName”,”对应具体的参数值”);
paramMap.put(“userArea”,”对应具体的参数值”);
User user=xxx. selectUser(paramMap);}
View Code

 

相关文章:

  • 2021-09-26
  • 2022-12-23
  • 2021-08-09
  • 2021-07-27
  • 2021-11-18
  • 2021-08-03
  • 2021-11-19
  • 2022-12-23
猜你喜欢
  • 2021-12-02
  • 2021-07-27
  • 2021-04-22
  • 2021-07-31
  • 2021-08-29
  • 2021-06-01
  • 2022-12-23
相关资源
相似解决方案