每一年都奔走在自己热爱里

没有人是一座孤岛,总有谁爱着你

Mybatis基础学习笔记(二)——Mybatis使用解析

Mybatis基础学习笔记

  • Mybatis的坐标

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.6</version>
    </dependency>
    
  • 建立数据库对应的实体类

  • 创建dao接口

  • 创建mapper映射文件,书写映射语句

    <?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">
    <!--名称空间,指示所映射的dao接口类,值是接口的全限定名称-->
    <mapper namespace="org.mybatis.example.BlogMapper">
        <!--id指示对应的方法名称-->
         <!--resultType指示查询结果的类型,在主配置文件中没有配置别名则需要使用全限定类目-->
      <select id="selectBlog" resultType="Blog">
          <!--在只有一个查询参数时可以随意设置参数名称-->
        select * from Blog where id = #{id}
      </select>
    </mapper>
    
  • Mybatis的主配置文件

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    	<!--引入mysql的配置文件-->
        <properties resource="JdbcDataSourceConfig.properties"/>
    	<!--别名在导入整个domain包时可以在mapper映射配置文件中不写全限定类名-->
        <typeAliases>
            <package name="com.km.domain"/>
        </typeAliases>
    
        <environments default="mysql">
            <environment id="mysql">
                <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>
    	<!--mapper映射文件的路径-->
        <mappers>
            <package name="com.km.dao"/>
            <!--使用mapper标签的resource属性导入xml配置文件用/分隔目录-->
            <!--使用mapper标签的class属性导入注解配置-->
        </mappers>
    </configuration>
    
  • 注意的事项:在xml配置文件中预留的字符需要替换为字符实体,如不能使用小于号(<)和大于号(>),这是因为会被误认为它们是标签。

  • //导入主配置文件
    InputStream in = Resources.getResourceAsStream("MybatisSpringConfig.xml");
    //使用工厂的创建者创建SqlSession的工厂
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
    //使用工厂获取SqlSession,SqlSession封装了执行sql语句的ff
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //动态代理获取ProductDao接口的代理对象
    ProductDao productDao = sqlSession.getMapper(ProductDao.class);
    //使用方法
    productDao.addXxx();
    //提交事务
    

sqlSession.commit();
//释放资源
sqlSession.close();
in.close();


- ```java
//第一个参数为需要配置方法的id坐标,namespace+方法名称id,第二个参数为传入的查询参数
Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
  • <!--一个动态sql查询的配置#{属性名}代表占位符,将获取参数类型对应的属性,将其作为查询参数-->
    <select id="findProductByPage" resultMap="productByPageMapper" parameterType="Condition">
            select * from products
            <where>
                <if test="id!=null ">
                    and id = #{id}
                </if>
                <if test="name!=null">
                    and name like #{name}
                </if>
                <if test="lp!=null">
                    and price >= #{lp}
                </if>
                <if test="hp!=null">
                    and  price &lt;= #{hp}
                </if>
            </where>
                    limit ${(currentPage-1)*6} , ${6};
    </select>
    
  • Mybatis默认为手动提交事务,在做数据库的增删改时需要自行手动提交事务

  • 在直接使用SqlSession执行CURD操作时Mybatis的核心时mapper映射,而不是dao接口,namespace与方法名id作为坐标指示执行的sql语句

  • SqlSessionFactory

    • SqlsessionFactory是重量级对象,创建此对象需要使用更多的资源和时间,应该是一个,单例的即可,SqlsessionFactory是一个接口,用于创建SqlSession。

    • //默认的SqlsessionFactory实现类
      public class DefaultSqlSessionFactory implements SqlSessionFactory 
      
  • Sqlsession

    • //boolean表示是否自动提交事务
      SqlSession openSession();
      SqlSession openSession(boolean var1);
      
    • //默认的sqlseesion实现类
      public class DefaultSqlSession implements SqlSession 
      
    • Sqlsession对象不是线程安全的,当多个方法线程调用时不安全

  • 最终调用PreparedStatement执行方法,底层还是调用JDBC
posted @ 2020-12-08 12:21  雨下整夜~  阅读(89)  评论(0)    收藏  举报