bean

03-dbutils源码之BeanProcessor、RowProcessor、BasicRo...

99封情书 提交于 2020-03-01 12:58:53
在dbutils中, BeanProcessor 是一个很重要的类:将列和bean对象的属性进行匹配,将列的值赋予bean的对象。这个是使用了反射来进行的。 来看下类的outline: 从上图可以看出,有“创建对象”、“调用setter”、“获得类的属性描述符”等方法,还有一个最重要的方法mapColumnsToProperties(ResultSetMetaData,PropertyDescriptor),这个方法是将结果集和类的属性进行一个匹配。toBean是将一行记录转换成一个bean对象。toBeanList就是将多行记录变成bean对象的List集合。 下面看一下它的具体实现。 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License

Shiro session和Spring session一样吗?

烂漫一生 提交于 2020-03-01 12:08:53
出自:https://yq.aliyun.com/articles/114167?t=t1 1. 疑问 我们在项目中使用了spring mvc作为MVC框架,shiro作为权限控制框架,在使用过程中慢慢地产生了下面几个疑惑,本篇文章将会带着疑问慢慢地解析shiro源码,从而解开心里面的那点小纠纠。 (1) 在spring controller中,request有何不同呢 ? 于是,在controller中打印了request的类对象,发现request对象是org.apache.shiro.web.servlet.ShiroHttpServletRequest ,很明显,此时的 request 已经被shiro包装过了。 (2)众所周知,spring mvc整合shiro后,可以通过两种方式获取到session: 通过Spring mvc中controller的request获取session Session session = request.getSession(); 通过shiro获取session Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); 那么,问题来了, 两种方式获取的session是否相同呢 ?

【源码阅读】dbutil包中的BeanProcessor类

孤者浪人 提交于 2020-03-01 12:07:45
BeanProcessor通过名字就可以知道,该类是用来处理Bean类(JavaBean)的,它的作用是将数据库中的记录转化为对应的Bean对象。 下面先看一下该类的类图: 该类有三个成员变量:我们重点来看两个Map类型的变量: 1、primitiveDefaults:该变量的主要作用是保存Java基本数据类型的Class与其默认值的对应关系,然后当SQL语句get方法返回NULL值的时候就用从该Map中获取相应的默认值,下面的静态代码块用来初始化该Map: static { primitiveDefaults.put(Integer.TYPE, Integer.valueOf(0)); primitiveDefaults.put(Short.TYPE, Short.valueOf((short) 0)); primitiveDefaults.put(Byte.TYPE, Byte.valueOf((byte) 0)); primitiveDefaults.put(Float.TYPE, Float.valueOf(0f)); primitiveDefaults.put(Double.TYPE, Double.valueOf(0d)); primitiveDefaults.put(Long.TYPE, Long.valueOf(0L)); primitiveDefaults.put

Spring+MyBatis整合

一笑奈何 提交于 2020-03-01 09:59:05
一、准备工作   1.1准备jar包            建表语句: CREATE TABLE `t_customer` ( `id` int(32) NOT NULL AUTO_INCREMENT, `username` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `jobs` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `phone` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;   1.2编写配置文件:     1.2.1db.properties    #dataSource #Thu Mar 07 16:27:40 CST 2019 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/sm jdbc.username=root jdbc

spring整合mybatis

做~自己de王妃 提交于 2020-03-01 09:56:42
spring整合mybatis 1.spring整合mybatis,需要用到一些依赖包,比如 <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> 还有其他的一些,比如下面的,当然没有全部列出来。 <!-- 导入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector

Spring整合Mybatis原理简单分析

荒凉一梦 提交于 2020-03-01 09:55:51
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 配置数据库表对应的java实体类 --> <property name="typeAliasesPackage" value="com.test.pojo" /> <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --> <property name="mapperLocations" value="classpath:com/test/mapping/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.test.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> 上面是一般项目中的配置,根据该配置分析 1,获取SqlSessionFactory

prignMVC+myBatis整合—— 基于MapperFactoryBean

有些话、适合烂在心里 提交于 2020-03-01 09:54:30
学习本节内容请先看"MyBatis的基本应用"。地址: http://lydia-fly.iteye.com/admin/blogs/2152948 Spring与MyBatis整合需要引入mybatis-spring.jar文件包。 其提供了与整合相关的API: SqlSessionFactoryBean --为整合应用提供SqlSession对象资源 MapperFactoryBean --根据指定的Mapper接口生成Bean实例 MapperScannerConfigurer --根据指定包批量扫描Mapper接口并生成实例 SqlSessionFactoryBean: 在单独使用MyBatis时,所有操作都是围绕SqlSession展开的,SqlSession是通过SqlSessionFactory获取的,SqlSessionFactory又是通过SqlSessionFactoryBuilder创建生成的。 在SpringMvc+MyBatis整合时,同样需要SqlSession。SqlSessionFactoryBean这个组件通过原来的SqlSessionFactoryBuilder生成SqlSessionFactory对象,为整合应用提供SqlSession对象。 Java代码 <bean id= "myDataSource" class= "org.apache

Spring4基础 学习笔记(6) ---- Spring与Mybatis整合

戏子无情 提交于 2020-03-01 09:53:04
Mybatis的主配置文件的DataSource不用注册,在Spring容器中注册 mapper动态代理(接口名字对应到映射文件的<insert>等标签的id)替换的是 daoimpl。 对于Dao的生成: <!-- 生成 Dao 的代理对象 --> < bean id = "StudentDao2" class = "org.mybatis.spring.mapper.MapperFactoryBean" > < property name = "sqlSessionFactory" ref = "mySqlSessionFactory" ></ property > < property name = "mapperInterface" value = "dao.IStudentDao" ></ property > </ bean > <!-- 工厂 --> < bean id = "mySqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "configLocation" value = "classpath:mybatis-config.xml" ></ property > <!-- mybatis 主配置文件没有数据源

源代码解读Spring只读事务与读写事务的性能的差别

放肆的年华 提交于 2020-03-01 09:38:36
前言: 如果大家使用过Spring事务管理,会发现Spring提供的事务分为“只读”和“读写”事务两类。这不免就会疑问这两种事务会有什么不同?本文则通过对Spring和Hibernate源代码的剖析来找出这两种事务的区别。特别是运行性能方面的区别。 解读的源代码版本为 Spring 2.5.6.SEC01 ,Hibernate 3.3.2.GA。 Spring对事务的支持也分编程式和声明式,本文以基于Annotation方式的声明式事务为例: Spring的配置如下: < bean class ="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" > < property name ="proxyTargetClass" value ="true" ></ property > </ bean > < bean id ="entityManagerFactory" class ="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" > < property name ="persistenceUnitName" value ="entityManager" /> < property name =

Spring-理解IOC容器(DI)

妖精的绣舞 提交于 2020-03-01 08:20:03
Spring-理解IOC容器 序言 IoC粗理解 IoC细理解 Spring中IoC的应用 IoC容器 容器的两种表现形式 BeanFactory的IoC实现过程: IoC容器初始化过程 BeanDefinition的定位 BeanDefinition的载入 IoC容器的依赖注入 IoC小结 参考文章: 序言 IoC(Inversion of Control) 控制反转,两种实现: 依赖查找(DL) 依赖注入(DI) IoC包括 依赖查找(DL) 和 依赖注入(DI) ;只不过DL因为有 侵入性 (它需要用户自己去是使用 API 进行查找资源和组装对象),已经被抛弃。所以现在提到IoC,更多的想到的就是依赖注入(DI)了。 依赖注入(DI)包括Set注入和构造器注入!其实还有一个通过实现接口的方式实现依赖注入,不过不常用,就不说了。 注意:Java 使用 DI 方式实现 IoC 的不止 Spring,包括 Google 的 Guice,还有一个冷门的 PicoContainer(极度轻量,但只提供 IoC)。 如图所示: 但其实 IOC 和DI 相当于一回事,只不过是看待问题的角度不同而已: IOC: Spring 反向控制应用程序需要的资源。 DI: 应用程序依赖Spring为其提供资源。 IOC 是站在Spring 的角度,而DI 是站在应用程序的角度。 如下图所示: