静态代理:对象型属性,替身
懒加载.get()方法查询
property="scoreList" Student 类中的 成绩列表属性
ofType="Score" 集合中放置的类型
column="id" 学生id 数据表列名、
select=“com.qfedu.dao.ScoreDao.findAllScoreByStudentId” 调用ScoreDao 中对应的方法并且 将 student表中列名 id 学生id对应的值给到这个方法
–>
指定列名查询值,传入懒加载方法
select=“懒加载方法是方法路径”
fetchType=“lazy” 默认情况就是懒加载 因为你开启了全局懒加载
eager 立即加载 如果配置为 eager 则会覆盖全局的开启的懒加载功能
在 标签里设置 懒加载
开启全局懒加载
开启全局立刻加载
scoreaaa:Score{scoreid=2, couresename=‘语文’, score=88, studentid=2, student=null}
studentbbb:Student{id=2, name=‘四哥’, sex=‘F’, age=42, height=155.0, sAddress=‘铁岭’, scoreList=null}
懒加载总结:
同一结果集,打印大的,具体为null;
打印具体,执行懒加载方法,输出内容
一级缓存是在同一个SqlSession对象内的缓存
一级缓存以方法和参数当键名,
flushCache=“true” 每次清空当前SqlSessiond对象中所有缓存
增删改会清空所有 缓存 ,一级缓存 默认处于开启状态
mybatis-config.xml文件里设置二级缓存
接口方法使用二级缓存
@CacheNamespace(blocking = true) // 开启二级缓存
useCache="true" 使用缓存 一般不需要配置 默认就开启
同方法名同实参,查缓存
只有sqlSession 调用close(),commit() 方法 才会将数据提交到二级缓存,其他的sqlSession才能拿到
二级缓存覆盖所有sqlSession对象
public interface StudentDao2 { }
//@Test的使用 是该方法可以不用main方法调用就可以测试出运行结果,是一种测试方法
接口上写执行注解
@Update(“update student_tb set name = #{name},age=#{age},sex=#{sex} where id = #{id}”)
int updateStudent(Student student);
调用:int num = studentDao2.updateStudent(student);
// @Results相当于<resultMap @Result 相当于<result
@Select("select * from student_tb where id = #{id}")
@Results(id = "studentMap",value = {@Result(id = true,column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(column = "sex",property = "sex"),
@Result(column = "age",property = "age"),
@Result(column = "height",property = "height"),
})// @Results相当于<resultMap @Result 相当于<result
============================================
@ResultMap(“studentMap”) // 可以引用studentMap 对应的@Results
arg多个参数下标顺序
param多个参数长度顺序
@Param(“a”)存储实参值
// 2. 使用 arg0 arg1 或者 param1 param2
// 3. 先 将参数声明,再使用 @Param(“name”) String name,@Param(“sex”) String sex
@Select(“select * from student_tb where name like #{arg0} and sex = #{arg1}”)
@Select(“select * from student_tb where name like #{param1} and sex = #{param2}”)
@Results(id = “名字”,value = {内容}
内容:@Result(column = “name”,property = “name”),
PageHelper.startPage(每页多少行数据, 显示第几页); 查询结果自动分页
所在变量作用域,方法内自动分页
左外连接:左表中全部显示,即使右表中没有匹配
fetchType=FetchType.LAZY
many=@Many(select=“com.qfedu.dao.ScoreDao.findAllScoreByStudentId”,fetchType=FetchType.LAZY)
one=@One(select=“com.qfedu.dao.ScoreDao.findAllScoreByStudentId”,fetchType=FetchType.LAZY)
SPRING:
bean 像容器声明创建对象 此时创建对象是由静态工厂创建 而不是容器直接创建
class=“com.qfedu.factory.StaticStudentFactory” 创建对象的静态工厂
factory-method=“createStudent” 制定创建对象的静态工厂方法
通过实例工厂创建对象
factory-bean=“studentFactory” 引用实例工厂
factory-method=“creatStudent” 调用实例工厂的 creatStudent 创建自定义的Student对象 并放置到容器中
!-- 通过ref=“iStudentDao” 容器中idiStudentDao 的对象 设置到StudentServiceImpl 对应studentDao 属性内 -->
ApplicationContext 接口类型,代表应用上下文, 可通过其实例获得Spring容器中的bean对象
ClassPathXmlApplicationContext 从类的根路径获取文件
FileSystemXmlApplicationContext磁盘路径获取文件,就是绝对路径
getBean() 参数是字符串时,根据Bean的id;
参数是class类型时,根据类型在容器里匹配bean,有多个同类型的bean,此方法报错
//componentScan扫描
@Value赋值
@Value("${jdbc.driver")
private String driver;
// 使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@Bean(“dataSource”) //Spring会将当前方法的返回值以指定名称存储到Spring容器中
实例化Bean就是注册bean
//@Resource相当于@Qualifier+@Autowired
@Resource(name=“us”) //@Resource(name=“id”) 赋值
private UserDao userDao;
代理对象点接口方法,自动传参
创建代理对象要强转
Subject p = (Subject) jdkProxy.createProxy(realSubject);
Ticket p=(Ticket) Proxy.newProxyInstance(t12306.getClass().getClassLoader(),t12306.getClass().
getInterfaces(),new InvocationHandler(){
public Object invoke(Object proxy,Method method,Object[] args)throws Throwable{
System.out.println("啊啊啊代理对象开始调用 目标对象");
// 让目标对象执行方法 并且获取返回结果
Object result = method.invoke(t12306,args);
System.out.println("代理对象结束调用 目标对象");
return result;
}
});
类加载器,向内存中加载对象
类ID和三层注册生效
实现:在xml 声明 context:component-scan 使对应包下的 注入的注解(@Autowired @Qualifie @Resource )生成bean的注解(@Component@Repository @Service @Controller)生效
<!--
开启 自动注解功能 使 注入的注解(@Autowired @Qualifie @Resource )生成bean的注解(@Component@Repository @Service @Controller)生效
-->
<context:component-scan base-package="com.qfedu"></context:component-scan>
===============================================================
@ResponseBody
作用:@ResponseBody注解用于将Controller的方法返回的对象,通过springmvc提供的HttpMessageConverter接口转换为指定格式的数据如:json,xml等,通过Response响应给客户端。
@RequestBody
作用:@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容(json数据)转换为java对象并绑定到Controller方法的参数上。
什么是HttpMessageConverter
作用:负责将请求信息转换为一个对象(类型为 T),将对象(类型为 T)输出为响应信息。