详细Spring 的配置知识点

1.数据库连接属性文件的加载

如果把连接的数据直接写在配置文件中,则不灵活。通常会把一些配置写在properties文件或xml文件中。由程序来动态读取,使我们的程序更加灵活,健壮。
如果要读取properties文件中的数据到Spring的配置文件中,则采用如下方法:
在这里插入图片描述

2.MyBites注解配置

采用注解配置可以省略映射文件。但是注解配置目前还不完善,动态SQL使用上不完美。所以通常只用于查询的查询的方法上,如果需要用动态SQL,则还是建议用XML映射文件。
MyBites的3.3.0之前的版本注解还是不太完善,建议用MyBites的3.4.0以上版本,今天我们用的是3.4.5的版本,与Spring框架的整合的Jar包,建议用.1.3.0及以上版本。
在这里插入图片描述

配置方法

/**

  • 年级持久化接口
  • @author Administrator
    /
    public interface GradeDao {
    /
    *
    • 根据年级编号查询单个年级对象
    • @param id
    • many 一对多,查询出的数据封装集合中。
    • @return
      /
      @Results(id=“gradeMap”,value= {
      @Result(column=“gradeId”,property=“id”),
      @Result(column=“gradeName”,property=“name”),
      @Result(column=“gradeId”,property=“students”,
      many=@Many(select=“com.bdqn.dao.StudentDao.findByGId”)
      )
      })
      @Select(“select * from grade where gradeId=#{id}”)
      public Grade findById(int id);
      /
      *
    • 查询所有年级对象
    • @return
      /
      @Select(“select * from grade”)
      @ResultMap(“gradeMap”) //引用上一个Results
      public List find();
      /
      *
    • 添加年级信息
    • @param grade
    • @return
      /
      @Insert(“insert into grade values(null,#{name})”)
      public int add(Grade grade);
      /
      *
    • 根据编号删除年级对象
    • @param id
    • @return
      /
      @Delete(“delete from grade where gradeId=#{id}”)
      public int delete(int id);
      /
      *
    • 修改年级信息
    • @param grade
    • @return
      */
      @Update(“update grade set gradeName=#{name} where gradeId=${id}”)
      public int udpate(Grade grade);
      }

多对一的配置:

/**

  • 学生持久化接口
  • @author Administrator

*/
public interface StudentDao {

@Select("select * from student where studentNo=#{id}")
@ResultMap("studentMap")
public Student findById(int id);
/**
 * 
 * @param id
 * one  多对一的映射,封装一个外键对象。
 * @return
 */
@Results(id="studentMap",value={
	@Result(column="gradeId",property="grade",
		one = @One(select="com.bdqn.dao.GradeDao.findById")
	)
	
})
@Select("select * from student where gradeId=#{id}")
@ResultMap("studentMap")
public List<Student> findByGId(int id);

@Select("select * from student")
@ResultMap("studentMap")
public List<Student> find();

}

3.Spring中Bean的作用域

Spring 中,Bean默认都是单例模式(singleton)。如果需要改成非单例模式,则在Bean节点上添加属性:scope。![在这里插入图片描述
在这里插入图片描述
单例模式在IOC容器加载时就创建了Bean对象。

非单例模式在IOC容器加载时没有创建Bean对象,是在取对象时才去创建对象。

如果是注解的方式来创建Bean,则在类前加@Scope注解

在这里插入图片描述

4.Spring配置文件的拆分

项目规模变大,配置文件可读性、可维护性差,团队开发时,多人修改同一配置文件,易发生冲突,所以,开发中会把配置文件拆分成几个文件。

在这里插入图片描述
运行时再把它们整合成一个文件。配合的方式有:
1.在加载时同时加载多个文件
ApplicationContext cxt = new
ClassPathXmlApplicationContext(
“config/applicationcontext_all.xml”,
“config/applicationcontext_dao.xml”,
“config/applicationcontext_tx.xml”);
2.文件的命名按统一格式来,就可以采用通配符:()
ApplicationContext cxt = new
ClassPathXmlApplicationContext("config/applicationcontext_
.xml");

3.加载一个配置文件,在这个配置文件中应用import导入其他配置文件:
在这里插入图片描述

5.延迟加载

可以设定某个Bean延迟加载。提升程序运行时的性能。
1.在Bean上面添加属性:只是这一个Bean有延迟加载效果

在配置文件中创建对象 在配置文件中创建对象,对象在容器创建时创建
id:表示这个对象的名称,也可以用name属性, class:类路径
scope:prototype:非单例创建对象。默认是单例模式:singleton
lazy-init="true"属性表示对象延迟加载。

<bean id="stu" class="com.bdqn.entity.Student" lazy-init="true">

2.也可以在beans根切点上添加属性:

default-lazy-init=“true”

,所有的Bean都会延迟加载。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值