MyBatismybatis大框架

MyBatis   开源之数量持久化层框架

                实体类及SQL语句之间确立映射关系

相同:MyBatis前身是IBatis,本是Apache的一个开源之项目,

冲SQL语法,简单好学 ,是耦合度降低,方便程序调式

次:搭建Mybatis的开支步骤:

a:下载mybatis-3.3.3.jar包并导入工程

b.编写MyBatis核心配置文件(configuration.xml)

c.创建实体类-POJO

d.DAO层-SQL映射文件(mapper.xml)

e.创建测试类

     读取全局配置文件mybatis-config.xml

     创建SqlSessionFactory对象,读取配置文件

     创建SqlSession对象

     调用mapper文件进行数量操作

 

自之大布局(全局配置里)

<configuration>
<properties resource="database.properties"/>
<environments default="development">
   <settings>
      <setting name="LogImpl" value="LOG4J">  </setting>
   </settings>
       <environment id="development">
             <transactionManager type="JDBC"></transactionManager>
             <dataSource type="POOLED">
                   <property name="driver" value="${driver}"></property>
                   <property name="url" value="${url}"></property>
                    <property name="username" value="${user}"></property>
                   <property name="userpwd" value="${password}"></property>
             </dataSource>
    </environment>
</environments>

<mappers>
    <mapper resource="cn/smbms/dao/user/UserMapper"
</mappers>
</properties>
</configuration>

小配置:

在dao层:

<mapper namespace="cn.smbms.dao.user.UserMapper"> 
     <!--查询用户表的记录数-->
     <select id="count" resultType="int">
            select count(1) as count from login
     </select>


</mapper>

在userMapperTest:

public class UserMapperTest{

private Logger logger=Logger.getLogger(UserMapperTest.class);

@Test


public void test(){


     string resource="mybatis-config.xml"


     int count=0;


     SqlSession sqlsession=null;


try{


//1.获取mybatis-config.xml输入流


      Inputstream is=Resources.getResourceAsStream(resource)


//2.创建SqlSessionFactory对象,完成对配置文件的读取


      SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is).


//3.创建SqlSession


      sqlSession=factory.openSession();


//4.调用mapper文件来对数据文件进行操作,必须将mapper文件引入到


     count=sqlSession.selectOne("cn.smbms.dao.user.UserMapp");

     logger.debug("UserMapper"+count);
}catch(IoException e){
     e.printStackTrace();
}finally{
     sqlSession.close();

}

优缺点:

优点:

    与JDBC相比,减少了50%上述之代码量

    小巧易学

    提供XML标签,支持编写动态SQL

    提供映射标签

缺点:

   数据库移植性差

   编写工作量大

 

 

MyBatis专注让SQL本身,是一个十足灵活得DAO层解决方案

适用于性能要求较高还是要求多变的互联网项目 

季:::::基本要素:::::

  三独基本要素:

  1. 中心接口和好像:

       SqlSessionFactoryBuilder

       SqlSessionFactory

       SqlSession

  2. mybatis-config.xml 

  3.mapper.xml    SQL映射文件

 

 

共用的Util工具类

MyBatisUtil:

public class MyBatisUtil{
      private static SqlSessionFactory factory;
      static{
           try{
                     InputStream is=Resources.getResourceAsStream("");
                     factory=new SqlSessionFactoryBuilder().bulid(is);
               }catch(IOException e){
                     e.printStackTrace();
              }

        }
     public static SqlSession createSqlSession(){

         return factory.openSession(false);
     }

     public static void closeSqlSession(SqlSession  sql){
           if(null!=sqlSession){
             sqlSession.close();
          }


}
  }

主干配置文件:

environments元素:

1.象征MyBatis的基本上效运行条件,将SQL映射到大半独不等的数目及。

2.子元素节点:environment,但是要指定其中一个默认运行环境(通过default指定)

    transactionManager-事物管理器

   <transactionManager type=”[JDBC|MANAGED]”/> 
======>>MANAGED(托管)

3.dataSource元素使用基本的JDBC数据源接口来配置JDBC连接对象的资源

   三栽内建的数额源类型:

    <dataSource type=”[UNPOOLED|POOLED|JNDI]”/>

 

 

 复杂对象:map

 所有目标 ===  增删改查 ===一级缓存二级缓存

public interface StudentDao {
    //查询所有信息
    public List<Student> selectStudent() throws Exception;
    //主键id查询
    public Student getById(int ID);
    //添加学生
    public int addStudent(Student stu) throws Exception;
    //修改学生
    public int upStudent(Student stu) throws Exception;
    //删除学生
    public int delStudent(int ID) throws Exception;
   //模糊条件查询:学生姓名是豆,并且年龄在10岁以上的
    public List<Student> ConditionStudent(Student stu) throws Exception;
    //findStudent<map>集合  多条件查询1
    public List<Student> findStudent(Map<String,Object> map);
    //按照索引号查询 多条件查询2
    public List<Student>  ConditionParameters(String StuName,int StuAge) ;
    //智能标签if  检测用户是否录入学生姓名和年龄
    public List<Student> findByIf(Student stu) throws Exception;
    //智能标签choose  检测用户是否录入学生姓名和年龄
    public List<Student> findByChoose(Student stu) throws Exception;
    //智能标签foreach  检测In(或者)
    public List<Student> findByForeach(int[] arg) throws Exception;
    //智能标签foreach List<Integer>  检测In(或者)
    public List<Student> ForeachList(List<Integer> list) throws Exception;
    //智能标签foreach List<Student>  检测In(或者)
    public List<Student> ForeachListStudent(List<Student> list) throws Exception;
    //if-set
    public  int getset(Student stu);
    //01.查询指定老师教的学生 多对多
    public RTeacher getStudentByTid(int id);
    //根据父分类编号,检索该父类编号对应的所有子分类的集合==============递归方法
    public List<Category> getChildrenByPid(int pid);
}

微布置文件里:

<mapper namespace="cn.zixin.dao.StudentDao">
    <!--二级缓存第二步-->
    <cache/>
    <!--SQL片段-->
    <sql id="columns">
         ID,StuName,StuAge,StuTime
    </sql>
    <!--查询所有的resultMap的用法:-->
    <resultMap id="studentmap" type="Student">
        <id column="ID" property="ID"></id>
    </resultMap>
    <select id="selectStudentS" resultMap="studentmap">
        select <include refid="columns"></include> from student
    </select>
    <!--查询所有-->
    <select id="selectStudent" resultType="Student">
        select * from student
    </select>
    <!--通过id查询-->
    <select id="getById" resultType="Student">
        select * from student where ID=#{ID}
    </select>
    <!--添加学生-->
    <insert id="addStudent">
         INSERT INTO student(StuName,StuAge,StuTime) VALUE (#{StuName},#{StuAge},#{StuTime})
    </insert>
    <!--修改学生-->
    <update id="upStudent">
        UPDATE student SET StuName=#{StuName} WHERE ID=#{ID}
    </update>
    <!--删除学生-->
    <delete id="delStudent">
        DELETE from student where ID=#{ID}
    </delete>

    <!--模糊查询学生-->
    <!--SELECT * from student WHERE StuName like concat('%',#{StuName},'%') AND StuAge>#{StuAge}-->
    <!--可以被SQL注入到 SELECT * from student WHERE StuName like '%${StuName}%' AND StuAge>#{StuAge}-->
    <select id="ConditionStudent" resultType="Student">
      SELECT * from student WHERE StuName like '%' #{StuName} '%' AND StuAge>#{StuAge}
   </select>
    <!--  MAP集合查询   多条件查询-->
    <select id="findStudent" resultType="Student">
       select * from student where StuName like '%' #{StuName} '%' AND StuAge>#{StuAge}
   </select>

    <!-- <resultMap id="Student" type="Student">
     <id property="id" column="id"></id>
         <result column="StuName" property="StuName"></result>
         <result property="StuAge" column="StuAge"></result>

     </resultMap>
 -->
    <!--根据索引查询-->
    <select id="ConditionParameters" resultType="Student">
        SELECT * FROM student WHERE StuName like '%' #{0} '%'   and StuAge >#{1}
    </select>
    <!--智能标签if  检测用户是否录入学生姓名和年龄-->
    <select id="findByIf" resultType="Student">
        SELECT * FROM student
        <where>
            <if test="StuName!=null">       <!--用户录入的姓名字段-->
                AND StuName LIKE '%'#{StuName}'%'
            </if>
            <if test="StuAge!=null">
                AND StuAge>#{StuAge}
            </if>
        </where>
    </select>
    <!--智能标签Choose  检测用户是否录入学生姓名和年龄-->
    <select id="findByChoose" resultType="Student">
        SELECT * FROM student
        <where>
            <choose>
                <when test="StuName!=null">
                    AND StuName LIKE '%'#{StuName}'%'
                </when>
                <when test="StuAge!=null">
                    AND StuAge>#{StuAge}
                </when>
                <otherwise>
                    AND 1=2
                </otherwise>
            </choose>
        </where>
    </select>

    <!--智能标签Foreach Array  检测In(或者)-->
    <select id="findByForeach" resultType="Student">
        SELECT * FROM student
        <where>
            <if test="array.length>0">
                ID IN
                <foreach collection="array" open="(" close=")" separator="," item="aD">
                    #{aD}
                </foreach>
            </if>
        </where>
    </select>

    <!--智能标签Foreach List<Integer> 检测In(或者)-->
    <select id="ForeachList" resultType="Student">
        SELECT * FROM student
        <where>
            <if test="list.size>0">
                ID IN
                <foreach collection="list" open="(" close=")" separator="," item="ID">
                    #{ID}
                </foreach>
            </if>
        </where>
    </select>

    <!--智能标签Foreach List<student>  检测In(或者)-->
    <select id="ForeachListStudent" resultType="Student">
        SELECT * FROM student
        <where>
            <if test="list.size>0">
                ID IN
                <foreach collection="list" open="(" close=")" separator="," item="stu">
                    #{stu.ID}
                </foreach>
            </if>
        </where>
    </select>
    <!--if-set-->
    <update id="getset" >
        UPDATE student
        <set>
            <if test="StuName!=null" >StuName=#{StuName},</if>
            <if test="StuAge!=null">StuAge=#{StuAge},</if>
        </set>
       WHERE ID=#{ID}
    </update>

    <!--查询指定老师教的学生 多对多-->
    <resultMap id="stuMapper" type="RTeacher">
        <id column="tid" property="tid"></id>
        <result column="tname" property="tname"></result>
        <collection property="stus" ofType="RStudent">
            <id column="sid" property="sid"></id>
            <result column="sname" property="sname"></result>
        </collection>
    </resultMap>
    <select id="getStudentByTid" resultMap="stuMapper" parameterType="RTeacher">
        SELECT sname,sid,tid,tname
        FROM student2,teacher,teacher_student
        WHERE student2.sid=teacher_student.studentid
        AND teacher.tid=teacher_student.teacherid
        AND tid=#{tid}
    </select>

测试类里:

    //查询
    @Test
    public void findsStudent() {
        List<Student> list = myBatis.getSession().selectList("selectStudentS");
        for (Student item : list) {
            System.out.println(item.getStuName());
        }
    }

    //id查询
    @Test
    public void getById() {
     /*  //映射关系第一种Class<T>类型
       Class<StudentText> aClass = StudentText.class;
        //映射关系第二种
       try {
            Class<StudentText> name = ( Class<StudentText>)Class.forName("cn.zixin.test.StudentText");
       } catch (ClassNotFoundException e) {
            e.printStackTrace();
       }*/
        //映射关系第三种 getMapper
        SqlSession session = myBatis.getSession();
        StudentDao dao = session.getMapper(StudentDao.class);
        Student byId = dao.getById(1);
        System.out.println(byId.getStuName());
        //Student stu= session.selectOne("getById",1);
        // System.out.println(stu.getStuName());
        session.commit();
        session.close();
    }

    //增加
    @Test
    public void addStudent() throws Exception {
        SqlSession session = myBatis.getSession();
        StudentDao dao = session.getMapper(StudentDao.class);
        Student stu = new Student();
        stu.setStuName("豆豆");
        stu.setStuAge(29);
        stu.setStuTime(new Date());
        dao.addStudent(stu);
        session.commit();  //增删改 手动提交事务
        System.out.println("ok");
        session.close();
    }

    //修改
    @Test
    public void upStudent() throws Exception {
        SqlSession session = myBatis.getSession();
        StudentDao dao = session.getMapper(StudentDao.class);
        Student stu = new Student();
        stu.setStuName("豆豆");
        stu.setID(2);
        dao.upStudent(stu);
        session.commit();  //增删改 手动提交事务
        System.out.println("ok");
        session.close();
    }

    //删除
    @Test
    public void delStudent() throws Exception {
        SqlSession session = myBatis.getSession();
        StudentDao dao = session.getMapper(StudentDao.class);
        dao.delStudent(2);
        session.commit();      //增删改 手动提交事务
        System.out.println("ok");
        session.close();
    }

    //模糊查询
    @Test
    public void ConditionStudent() throws Exception {
        SqlSession session = myBatis.getSession();
        StudentDao dao = session.getMapper(StudentDao.class);
        Student stu = new Student();
        stu.setStuName("豆");
        stu.setStuAge(30);
        List<Student> list = dao.ConditionStudent(stu);
        for (Student item : list) {
            System.out.println(item.getStuName());
        }
        session.commit();  //增删改 手动提交事务
        System.out.println("ok");
        session.close();
    }

    //findStudent<map>集合  多条件查询
    @Test
    public void mapsStudent() throws Exception {
        SqlSession session = myBatis.getSession();
        StudentDao dao = session.getMapper(StudentDao.class);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("StuName","豆");
        map.put("StuAge",20);
        List<Student> list = dao.findStudent(map);
        for (Student item : list) {
            System.out.println(item.getStuName());
        }
        System.out.println("ok");
        session.close();
    }

    //按照索引号查询
   @Test
    public void ConditionParameter() {
        String StuName = "豆";
        int StuAge = 20;
        List<Student> list = myBatis.getSession().getMapper(StudentDao.class).ConditionParameters(StuName,StuAge);
        for (Student stu : list) {
            System.out.println(stu.getStuName());
        }
    }
    @Test
    //智能标签if
    public void findByIf() throws Exception {
        Student stu = new Student();
        stu.setStuName( "豆");
        stu.setStuAge( 29);
        List<Student> list = myBatis.getSession().getMapper(StudentDao.class).findByIf(stu);
        for (Student item : list) {
            System.out.println(item.getStuName());
        }
        System.out.println("ok");
    }
    @Test
    //智能标签choose
    public void findBychoose() throws Exception {
        Student stu = new Student();
        stu.setStuName( "豆");
        stu.setStuAge( 29);
        List<Student> list = myBatis.getSession().getMapper(StudentDao.class).findByChoose(stu);
        for (Student item : list) {
            System.out.println(item.getStuName());
        }
        System.out.println("ok");
    }

    @Test
    //智能标签foreach array
    public void findByForeach() throws Exception {
        int[] arg={2,5};
        List<Student> list = myBatis.getSession().getMapper(StudentDao.class).findByForeach(arg);
        for (Student item : list) {
            System.out.println(item.getStuName());
        }
        System.out.println("ok");
    }

    @Test
    //智能标签foreach List<Integer>
    public void foreachList() throws Exception {
        List<Integer> lists=new ArrayList<Integer>();
        lists.add(2);
        lists.add(5);
        List<Student> list = myBatis.getSession().getMapper(StudentDao.class).ForeachList(lists);
        for (Student item : list) {
            System.out.println(item.getStuName());
        }
        System.out.println("ok");
    }

    @Test
    //智能标签foreach List<Student>
    public void foreachListStudent() throws Exception {
        List<Student> lists=new ArrayList<Student>();
        Student s1=new Student();
        s1.setID(2);
        Student s2=new Student();
        s2.setID(5);
        lists.add(s1);
        lists.add(s2);
        List<Student> list = myBatis.getSession().getMapper(StudentDao.class).ForeachListStudent(lists);
        for (Student item : list) {
            System.out.println(item.getStuName());
        }
        System.out.println("ok");
    }

    @Test
    // 多表联查 一对多 单条SQL
    public void selectOne() throws Exception {
        Dept dept = myBatis.getSession().getMapper(IdeptDao.class).getEmpsByDeptNO(1);
        System.out.println(dept.getDeptname());
        for (Emp emp:dept.getEmps()){
            System.out.println(emp.getEmpname());
        }
        System.out.println("ok");
    }

    @Test
    // 多表联查 一对多 多条SQL
    public void selectmoen() throws Exception {
        Dept dept = myBatis.getSession().getMapper(IdeptDao.class).getEmpsByDeptNoMutilSQL(1);
        System.out.println(dept.getDeptname());
        for (Emp emp:dept.getEmps()){
            System.out.println(emp.getEmpname());
        }
        System.out.println("ok");
    }
    @Test
    // 多对一 根据员工的编号,获取员工姓名 和 部门名称  单条SQL
    public void selectManyToOne() throws Exception {
        Emp emp = myBatis.getSession().getMapper(IdeptDao.class).getEmpByEmpNo(1);
        System.out.println(emp.getEmpname());
        System.out.println(emp.getDept().getDeptname());
        System.out.println("ok");
    }
    @Test
    // 多对一 根据员工的编号,获取员工姓名 和 部门名称  多条SQL
    public void selectMany() throws Exception {
        Emp emp = myBatis.getSession().getMapper(IdeptDao.class).getEmpByEmpNoMutilSQL(1);
        System.out.println(emp.getEmpname());
        System.out.println(emp.getDept().getDeptname());
        System.out.println("ok");
    }
    @Test
    //if-set
    public void testIsSet() throws Exception {
        Student stu=new Student();
        stu.setStuName("欣欣");
        stu.setStuAge(10);
        stu.setID(1);
        int i = myBatis.getSession().getMapper(StudentDao.class).getset(stu);
        System.out.println(i);
    }
    @Test
    //多对多 查询指定老师教的学生
    public void testManyToMany() throws Exception {
        RTeacher teacher = myBatis.getSession().getMapper(StudentDao.class).getStudentByTid(1);
        System.out.println(teacher.getTname());
        for (RStudent stu:teacher.getStus()){
            System.out.println(stu.getSname());
        }
        System.out.println("ok");
    }
    @Test
    //自连接测试=======递归
    public void testSelf() throws Exception {
        List<Category> list= myBatis.getSession().getMapper(StudentDao.class).getChildrenByPid(0);
        for (Category item : list) {
            System.out.println(item);
        }
        System.out.println("ok");
    }
    @Test
    //一级缓存 存在性的证明
    public void testOneLevelCache() throws Exception {
            List<Student> list = myBatis.getSession().getMapper(StudentDao.class).selectStudent();
            for (Student item : list) {
                System.out.println(item);
            }
        System.out.println("============一级缓存");
        List<Student> list1 = myBatis.getSession().getMapper(StudentDao.class).selectStudent();
        for (Student item : list1) {
            System.out.println(item);
        }


        System.out.println("ok");
    }

    @Test
    //增删改对一级缓存的影响
    public void testUpdateHashSomeThinngToOneLevelCache() throws Exception {
        Student Stu=new Student();
        Stu.setID(6);
        Stu.setStuName("件件");
        Stu.setStuAge(12);
        int i = myBatis.getSession().getMapper(StudentDao.class).addStudent(Stu);
        System.out.println(i);
        System.out.println("============一级缓存");

        System.out.println("ok");
    }

一对多 === 多对多

接口类里:

public interface IdeptDao {
    //一对多 单条SQL实现方法
    public Dept getEmpsByDeptNO(int deptno);
    //一对多 多条SQL实现方法
    public Dept getEmpsByDeptNoMutilSQL(int deptno);
    //多对一 单条SQL 根据员工的编号,获取员工姓名 和 部门名称
    public Emp getEmpByEmpNo(int empno);
    //多对一 多条SQL 根据员工的编号,获取员工姓名 和 部门名称
    public Emp getEmpByEmpNoMutilSQL(int empno);
}

当自身之微布置里:

<mapper namespace="cn.zixin.dao.IdeptDao">
    <!--一对多 单条SQL实现方法-->
    <resultMap id="deptMapper" type="Dept">
            <id column="deptno" property="deptno"></id>
            <result column="deptname" property="deptname"></result>
        <collection property="emps" ofType="Emp">
            <id property="empno" column="empno"></id>
            <result column="empname" property="empname"></result>
        </collection>
    </resultMap>

    <select id="getEmpsByDeptNO" resultMap="deptMapper">
          select dept.deptno,deptname,empno,empname from dept,emp where dept.deptno=emp.deptno and dept.deptno=#{deptno}
    </select>


    <!--一对多 多条SQL实现方法-->
    <resultMap id="deptMappers" type="Dept">
        <id column="deptno" property="deptno"></id>
        <result column="deptname" property="deptname"></result>
        <collection property="emps" ofType="Emp" select="deptBy" column="deptno"></collection>
    </resultMap>

    <select id="deptBy" resultType="Emp">
        SELECT * FROM emp WHERE  deptno=#{deptno}
    </select>

    <select id="getEmpsByDeptNoMutilSQL" resultMap="deptMappers">
        select deptno,deptname from dept where deptno=#{deptno}
    </select>

    <!--多对一 根据员工的编号,获取员工姓名 和 部门名称 单条SQL-->
    <resultMap id="empMapper" type="Emp">
        <id column="empno" property="empno"></id>
        <result column="empname" property="empname"></result>
        <association property="dept" javaType="Dept">
           <id column="deptno" property="deptno"></id>
            <result column="deptname" property="deptname"></result>
        </association>

    </resultMap>

    <select id="getEmpByEmpNo" resultMap="empMapper">
        select dept.deptno,deptname,empno,empname from dept,emp where dept.deptno=emp.deptno and empno=#{empno}
    </select>


   <!-- 多对一 多条SQL 根据员工的编号,获取员工姓名 和 部门名称-->
    <resultMap id="empMutiMapper" type="Emp">
        <id column="empno" property="empno"></id>
        <result column="empname" property="empname"></result>
        <association property="dept" javaType="Dept" select="selectDeptByDeptNo" column="deptno">
        </association>

    </resultMap>
    <select id="selectDeptByDeptNo" resultType="Dept">
        SELECT * from Dept where deptno=#{deptno}

    </select>

    <select id="getEmpByEmpNoMutilSQL" resultMap="empMutiMapper">
        select deptno,empno,empname from emp where empno=#{empno}
    </select>

</mapper>

 

注:所有的测试类里还有所有的接口测试

 

MyBatis
本是apache的一个开源项目iBatis,
2010年是项目由于apache software foundation 迁移至了google
code,并且改名为MyBatis 。2013年11月搬至Github。

iBATIS一词来源于”internet”和”abatis”的组成,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL
Maps和Data Access Objects(DAO)

 

咱们拿Mybatis的功效架构分为三交汇:

(1)API接口层:提供给外部使用的接口API,开发人员通过这些当地API来决定数据库。接口层一接接调用请求虽见面调用数据处理层来就具体的数额处理。

(2)数据处理层:负责具体的SQL查找、SQL解析、SQL执行和实行结果映射处理等。它至关重要的目的是根据调用的求完成同样不成数据库操作。

(3)基础支撑层:负责最基础的效应支持,包括连续管理、事务管理、配置加载与缓存处理,这些还是公私的事物,将他们抽取出来作为最基础之机件。为上层的数处理层提供极基础的支持。

 

MyBatis
最精锐的特性有即是她的动态语句功能。如果你以前发采取JDBC或者类似框架的

更,您就会见知道把SQL语句极连接在齐是多的痛,要管不可知忘却空格或不要在

columns列后面省略一个逗号等。动态语句能够完全缓解掉这些痛苦。

尽管同动态SQL一起坐班无是当初步一个party,但是MyBatis确实能够通过在其他映射SQL语句被

使强劲的动态SQL来改善这些状况。

动态SQL元素对于其余利用过JSTL或者类似于XML之类的文本处理器的人数来说,都是甚熟悉

的。在直达一致版本被,需要了解与读书十分多的素,但于MyBatis 3
中产生了重重之改善,现在特

结余差不多二分之一之素。MyBatis使用了依据强大的OGNL表达式来祛除了大部分元素。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

网站地图xml地图