复习资料1

1.实体类为什么都用包装类型?
举个例子,一个Student类,其成绩属性,我们设置为
private Integer socre;
而不设置为
private int socre;
这是因为有的学生会出现考0分,而有的学生缺考,这样,就没有办法去区分了。
包装类型 Integer 和 基本类型 int 的最大区别就是包装类可以设置为 null。
缺考设置为null,就ok了。

2.有@component直接注解类,为什么还要@bean呢,有什么区别吗?
还有@AutoWired,@Qualifier, @Resource, @Value,@Scope,@ComponentScan,@Bean,@Import,@PropertySource,@Configuration,@Entity,的作用?
@component把资源让 spring 来管理。相当于在 xml 中配置一个 bean。
@AutoWired自动按照类型注入。当使用注解注入属性时,set 方法可以省略。
@Qualifier在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。
@Resource直接按照 Bean 的 id 注入。它也只能注入其他 bean 类型。
@Value注入基本数据类型和 String 类型数据的
@Scope指定 bean 的作用范围。@Configuration用于指定当前类是一个 spring 配置类,当创建容器时会从该类上加载注解。
@ComponentScan用于指定 spring 在初始化容器时要扫描的包。作用和在 spring 的 xml 配置文件中的:<context:component-scan base-package="com.itheima"/>是一样的。
@Bean该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器。
@Import用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration 注解。当然,写上也没问

@PropertySource用于加载.properties 文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到properties 配置文件中,就可以使用此注解指定properties 配置文件的位置。
@RunWith(SpringJUnit4ClassRunner.class)junit 给我们暴露了一个注解,可以让我们替换掉它的运行器
@ContextConfiguration(locations= {"classpath:bean.xml"})使用@ContextConfiguration 指定 spring 配置文件的位置

3.@ResponseBody和@RequestParam的作用?

4.springmvc的运行流程?
1、  用户发送请求至前端控制器DispatcherServlet。
2、  DispatcherServlet收到请求调用HandlerMapping处理器映射器。
3、  处理器映射器找到具体的处理器(可以根据xml配置、注解进行查找),生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。
4、  DispatcherServlet调用HandlerAdapter处理器适配器。
5、  HandlerAdapter经过适配调用具体的处理器(Controller,也叫后端控制器)。
6、  Controller执行完成返回ModelAndView。
7、  HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet。
8、  DispatcherServlet将ModelAndView传给ViewReslover视图解析器。
9、  ViewReslover解析后返回具体View。
10、 DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。
11、 DispatcherServlet响应用户。

5.mybits操作数据库的注解?
@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@ResultMap:实现引用@Results 定义的封装
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
@SelectProvider: 实现动态 SQL 映射
@CacheNamespace:实现注解二级缓存的使用

6.从表和主表的区别?
外键的箭头指向的是从表,主键所在的表叫主表,外键所在的表叫从表

7.SelectKey相当于什么?
新增用户后,同时还要返回当前新增用户的 id 值,因为 id 是由数据库的自动增长来实现的,所以就相
当于我们要在新增后将自动增长 auto_increment 的值返回。
<insert id="saveUser" parameterType="USER">
    <!-- 配置保存时获取插入的 id -->
    <selectKey keyColumn="id" keyProperty="id" resultType="int">
        select last_insert_id();
    </selectKey>
insert into user(username,birthday,sex,address) 
values(#{username},#{birthday},#{sex},#{address})
</insert>

8.怎样实现延迟加载?
使用 assocation 实现延迟加载,在resultMap中添加
<!-- 它是用于指定从表方的引用实体属性的 -->
<association property="user" javaType="user"select="com.itheima.dao.IUserDao.findById"column="uid"></association>
同时我们需要在 Mybatis 的配置文件 SqlMapConfig.xml 文件中添加延迟加载的配置。
<!-- 开启延迟加载的支持 -->
<settings>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>
使用 Collection 实现延迟加载,同样也在resultMap中添加
<!-- collection 是用于建立一对多中集合属性的对应关系
     ofType 用于指定集合元素的数据类型
     select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
     column 是用于指定使用哪个字段的值作为条件查询
--> 
<collection property="accounts" ofType="account"select="com.itheima.dao.IAccountDao.findByUid" column="id"></collection>

9.useCache=true,表示使用一级缓存,在xml文件中在哪里呢?
<select id="findById" resultType="UsEr" parameterType="int" useCache="true">
    select * from user where id = #{uid}
</select>

10.typeAliases的作用?
<typeAliases>
    <!-- 批量别名定义,扫描整个包下的类,别名为类名(首字母大写或小写都可以) --> 
    <package name="com.itheima.domain"/>
</typeAliases>

11.怎样开启二级缓存?
在 SqlMapConfig 中开启二级缓存支持
<!-- 配置二级缓存 --> 
<settings>
    <!-- 开启二级缓存的支持 --> 
    <setting name="cacheEnabled" value="true"/>
</settings>
在持久层接口中使用注解配置二级缓存
@CacheNamespace(blocking=true)//mybatis 基于注解方式实现配置二级缓存

12.实例化 Bean 的三种方式和spring 的三种依赖注入用来干什么的?
实例化 Bean 的三种方式是用来生成相应类的对象。
依赖注入它是 spring 框架核心 ioc 的具体实现。我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,
实例化 Bean 的三种方式
第一种方式:使用默认无参构造函数,
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"/>
第二种方式:spring 管理静态工厂-使用静态工厂的方法创建对象
public class StaticFactory {
  public static IAccountService createAccountService(){
    return new AccountServiceImpl();
} }
<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="createAccountService"></bean>
第三种方式:spring 管理实例工厂-使用实例工厂的方法创建对象
public class InstanceFactory {
  public IAccountService createAccountService(){
    return new AccountServiceImpl();
} }
<bean id="instancFactory" class="com.itheima.factory.InstanceFactory"></bean> 
<bean id="accountService"factory-bean="instancFactory"factory-method="createAccountService"></bean>
spring 的三种依赖注入
第一种方式:构造函数注入
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> 
    <constructor-arg name="name" value="张三"></constructor-arg>
    <constructor-arg name="age" value="18"></constructor-arg> 
    <constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
第二种方式:set 方法注入
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> 
    <property name="name" value="test"></property> 
    <property name="age" value="21"></property> 
    <property name="birthday" ref="now"></property>
</bean>
第三种方式:接口注入
有些时候资源并非来自于自身系统,而是来自于外界,比如数据库连接资源完全可以在Tomcat下配置,然后通过JNDI的形式去获取它,这样数据库连接资源是属于开发工程外的资源,这个时候我们可以采用接口注入的形式来获取它
还有一种叫自动装配:@Autowired

13.使用 DBAssit 作为持久层解决方案,在mybits中用xml的方式配置事务
<!-- 配置 dbAssit 此处我们只注入了数据源,表明每条语句独立事务--> 
<bean id="dbAssit" class="com.itheima.dbassit.DBAssit">
    <property name="dataSource" ref="dataSource"></property>
</bean>

14.spring中的aop事务相关的知识?
aop:config:作用:用于声明开始 aop 的配置
aop:aspect:作用:用于配置切面。
aop:pointcut:作用:用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。
<aop:config> 
  <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))"id="pt1"/>
    <aop:aspect id="txAdvice" ref="txManager">
      <!-- 配置环绕通知 --> 
      <aop:around method="transactionAround" pointcut-ref="pt1"/>
   </aop:aspect>
</aop:config>
<!-- 配置前置通知:在切入点方法执行之前执行
<aop:before method="beforePrintLog" pointcut-ref="pt1" ></aop:before>-->
<!-- 配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个
<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>-->
<!-- 配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>-->
<!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行
<aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>-->
<!-- 配置环绕通知 详细的注释请看Logger类中-->
<aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>
注解配置事务
@Aspect//表明当前类是一个切面类
@Before作用:把当前方法看成是前置通知。
@AfterReturning作用:把当前方法看成是后置通知。
@AfterThrowing作用:把当前方法看成是异常通知。
@After作用:把当前方法看成是最终通知。@After("execution(* com.itheima.service.impl.*.*(..))")
@Around作用:把当前方法看成是环绕通知。@Around("execution(* com.itheima.service.impl.*.*(..))")
@Pointcut作用:指定切入点表达式,@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
@Around("pt1()")//注意:千万别忘了写括号
@EnableAspectJAutoProxy不使用 XML 的配置方式

15.配置事务的步骤?
第一步:配置事务管理器
<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 注入 DataSource --> 
      <property name="dataSource" ref="dataSource"></property>
</bean>
第二步:配置事务的通知引用事务管理器    
<tx:advice id="txAdvice" transaction-manager="transactionManager"></tx:advice>
第三步:配置事务的属性
<tx:attributes>
    <tx:method name="*" read-only="false" propagation="REQUIRED"/>
    <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
第四步:配置 AOP 切入点表达式
<aop:config>
    <!-- 配置切入点表达式 --> 
    <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))"id="pt1"/>
</aop:config>
第五步:配置切入点表达式和事务通知的对应关系
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>

16.springMVC的常用注解?
@RequestMapping:
@RequestMapping("/useRequestParam")把请求中指定名称的参数给控制器中的形参赋值。
@RequestBody:用于获取请求体内容。直接使用得到是 key=value&key=value...结构的数据。get 请求方式不适用。
public String useRequestBody(@RequestBody(required=false) String body)
@PathVariable:用于绑定 url 中的占位符。例如:请求 url 中 /delete/{id},这个{id}就是 url 占位符。
@RequestMapping("/usePathVariable/{id}")
public String usePathVariable(@PathVariable("id") Integer id)
@RequestHeader:用于获取请求消息头
public String useRequestHeader(@RequestHeader(value="Accept-Language",required=false)String requestHeader)
@CookieValue:用于把指定 cookie 名称的值传入控制器方法参数。
public String useCookieValue(@CookieValue(value="JSESSIONID",required=false) String cookieValue)
@ModelAttribute:出现在方法上,表示当前方法会在控制器的方法执行之前,先执行。它可以修饰没有返回值的方法,也可以修饰有具体返回值的方法。出现在参数上,获取指定的数据给参数赋值。
@SessionAttribute:用于多次执行控制器方法间的参数共享。
@RequestMapping("/springmvc")
@SessionAttributes(value ={"username","password"},types={Integer.class}) 
public class SessionAttributeController

17.@ResponseBody 注解实现将 controller 方法返回对象转换为 json 响应给客户端,其中放在@ResponseBody注解放在方法上方和放在返回值类型前面有啥区别没?
没有区别,都是将对象转换为 json 响应给客户端

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值