mybatis大框架

Posted 素而安然

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis大框架相关的知识,希望对你有一定的参考价值。

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.以上是关于mybatis大框架的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis超强大的动态SQL大全

Mybatis超强大的动态SQL大全

MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段

Mybatis框架第二篇

mybatis大框架

Mybatis的架构原理与6大核心流程