0
点赞
收藏
分享

微信扫一扫

mybatis注解开发,原理以及部分属性

刘员外__ 2022-03-17 阅读 106
java

官网

1.日志文件

加入日志的依赖:导入log4j的jar包
1.在应用的类路径中创建一个名为log4j.properties的文件

# 全局日志配置
log4j.rootLogger=ERROR, stdout
# MyBatis 日志配置
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=D:/EE_2113/day58/resource/mylog.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

2.输出映射和输入映射

1.输出映射:resultType
		返回实体类,需要实体类全限定名称
		需要返回集合,集合存储数据类型的全限定名称
例:<select id="findAll" resultType="com.qf.pojo.Student">
		select  * from student
    </select>
2.输入映射:parameterType
		实体类型,实体类的全限定名称
例:<insert id="addStudent" parameterType="com.qf.pojo.Student">
    insert  into  student(name,age,address,birthday)
    values(#{name},#{age},#{address},#{birthday})
   </insert>

3. mybatis的占位符

1.占位符
   实体类型:#{实体类属性名称}
   基本数据类型:#{随便入值}

4.模糊查询

1.基本模糊查询
	'%${value}%,字符串拼接符号:${}
	${}:字符串拼接符号
	'%${value}%':值和% %直接进行拼接,尽量不使用这种符号
例:
	select * from student where name like  #{name}
	List<Student> list = studentMapper.findStudentByName("%张%");
	
2.封装vo实体模糊查询
  mybatis支持OGNL(对象图导航语言)语言
        访问的时候:queryVo对象.getStudent().getName()
        简写格式:#{student.name}
<select id="findUserByQueryVo" resultType="com.qf.pojo.Student"
    parameterType="com.qf.pojo.QueryVo">
   
 select * from student where name like #{student.name}
 </select>

5. mabatis插入数据时候,获取表中自增长主键id的值

mybatis插入数据的时候,获取表中自增长主键id的值
insert标签中有字标签selectkey
    属性:keyProperty:实体类中的属性值,通过id字段和表中id字段映射
        keyColumn:表中的主键字段  id字段
        order:什么时候执行sekectKey标签中的语句
        SELECT LAST INSERT_ID():获取最后一次自增长的主键到的id值
              值:AFTER:在执行插入之后,执行selectKey的语句                 BEFORE:插入数据之后执行              
        resultType:获取到的自增长主键的类型

6. 传统dao方式,使用mybatis操作

弊端:
	1.开发过程中需要配置核心配置文件,也需要映射文件,还需要提供接口实现类
	2.从内存角度考虑
		需要在业务层new对象,不断开辟内存空间
	3.程序之间出现了耦合性,解决耦合的最常用方法--反射

7. mybatis注解开发的使用流程(并非纯注解)

1.注解开发,不需要加载映射文件,
在核心配置文件下,使用映射器接口实现类的完全限定类名
<!--映射器-->
<mappers>
 <!--注解开发,不需要加载映射文件
     class属性:直接指定接口的全限定名称
     -->
     <mapper class="com.qf.mapper.StudentMapper">		 </mapper>
</mappers>
2.在接口中的方法上添加对应的标签注解注解以及sql语句
    @Select("select * from student")
  //@Select注解:查询的---替代了xx.xml映射文件中的select标签
    List<Student> findAll() ;

8. mybatis的动态代理如何实现的

1.读取mybatis核心配置文件
2.SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder():使用构建者设计模式
	//就是将核心配置文件中的内容封装到对应的类中
3.接口工厂
  SqlSessionFactory sqlSessionFactory = builder.build(inputStream);//加载流中的内容
  //上面操作中:build方法中-->创建了xml的核心配置对象
  //完成以下操作
  XMLConfigBuilder parser = new XMLConfigBuilder(inputStream,emvironment,properties);
  //调用parser()将核心文件中的所有标签内容解析封装到Configuration配置类中
  (1)底层执行的方法
      	public SqlSessionFactory build(Configuration config){
      return new DefaultSqlSessionFactory(config);
  }
  (2)public DefaultSqlSessionFactory(Configuration configuration){
      this.configuration = configuration;
  }
4.创建执行对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
	SqlSession:接口
        子实现类:DefaultSqlSession
            	给执行器接口Executor初始化
            	以及Configuration配置类进行初始化
            执行器接口Executor-->底层就是PreparedStatement预编译对象
5.获取接口代理对象
  StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
用到了动态代理设计模式
    day58 imags

9. 读取外部配置文件

1.在头部配置下面
  加载类路径下的其他properties配置文件
  属性:resource="指定资源目录下的配置文件名称"
		<properties resource="jdbc.properties"/>
2.通过${获取配置文件中的key对应的value}
	    <property name="driver" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>

10.类型别名–>别名的包扫描

1.核心配置文件
<typeAliases>
     <!--type:别名:尽量小写,不区分大小写的
          alias:当前这个实体类的全限定名称
          随着实体类越来越多,那么单个别名配置太麻烦了
      -->
       <!-- <typeAlias type="student" alias="com.qf.pojo.Student"/>-->
       <!--别名的包扫描
         它直接将实体了所在包指定,别名默认就是当前类名小写,(一般不区分大小写)
        -->
        <package name="com.qf.pojo"/>     
    </typeAliases>
2.映射配置中不需要写全限定名称,直接写别名

3.映射器
  Mapper的别名包扫描方式
  将包内的映射器接口实现全部注册为映射器
<mappers>
   <package name="com.qf.mapper"/>
</mappers>

举报

相关推荐

0 条评论