spring入门与mybatis-spring环境搭建

概述

spring是一个针对bean的生命周期进行管理的轻量级容器,即管理类的实例化及属性的赋值的容器

spring使用初体验(基于xml)

类的实例化

  1. 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="demo1" class="com.Demo1"/>
</beans>
  1. JavaBean
public class Demo1 {
    public void test1(){
        System.out.println("Spring1");
    }
}
  1. 测试
public class Spring1Test {

    @Test
    public void test1(){
        /*
        加载spring-config.xml配置文件,参数为配置文件路径
         */
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
        /*
        通过spring来获取demo1的实例,第一个参数为配置的id,第二个参数为目标class
         */
        Demo1 demo1 = applicationContext.getBean("demo1", Demo1.class);
        /*
        测试是否生成实例
         */
        demo1.test1();
    }
}

属性赋值

  1. 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="demo1" class="com.Demo1">
        <property name="name" value="张三"/>
        <property name="age" value="10" />
    </bean>
</beans>
  1. JavaBean
public class Demo1 {
     private String name;
     private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void test1(){
         System.out.println("Spring1");
     }
     public void test2(){
         System.out.println("name:" + name + "\tage:" + age);
     }
}
  1. 测试
@Test
    public void test2(){
        /*
        加载spring-config.xml配置文件,参数为配置文件的路径
         */
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
        /*
        通过spring来获取demo1的实例,第一个参数为加载的id,第二个参数为目标class
         */
        Demo1 demo1 = applicationContext.getBean("demo1", Demo1.class);
        /*
        测试属性命名是否成功
         */
        demo1.test2();
    }

Bean的管理

哪种类可以由Spring管理

  1. 在未传入参数时必须有无参构造器
  2. 不能是抽象类
  3. 对于私有拥有私有构造器的类,spring也可以加载(spring会创建一个可访问的构造器)
    在这里插入图片描述

scope属性

singleton

默认值在配置文件被加载时实例化,且只实例化一次

prototype

在getBean调用时实例化,每次调用getBean实例化一个新对象

属性的注入

set方法的注入

使用property标签,该标签会找到set方法来给属性赋值

  1. 简单类型(8种基本类型和String):使用name和value属性给属性赋值
<property name="" value="" />
  1. 对象/引用类型:通过ref属性来指定对象,目标对象也要在配置文件中配置好,ref属性值为目标bean的id
<bean id="bean1" class=""></bean>
<bean id="bean2" class="">
	<property name="bean1" ref="bean1" />
</bean>
  1. 数组:通过array标签,对于简单类型,中使用value双标签,中间写值;对于复杂类型,使用ref单标签,bean属性指定目标bean的id
<array>
	<value>value1</value>
	<value>value2</value>
	<value>value3</value>
	<value>value4</value>
</array>
<array>
	<ref bean="" />
	<ref bean="" />
	<ref bean="" />
</array>
  1. 集合:list标签,set标签与array使用方法一致,map标签中的entry标签属性key和value对应键值,properties类型与map相似,使用props标签内嵌套prop标签,key对应键,标签体对应值
<list>
	<value></value>
</list>
<set>
	<value></value>
</set>
<map>
	<entry key="" value=""></entry>
</map>
<props>
	<prop key="">value</prop>
</props>

构造方法的注入

带参构造方法给属性赋值,使用constructor-arg标签,value属性对应值,index属性对应多个参数时的参数索引

<constructor-arg value=""></constructor-arg>

针对接口编程(接口类型的注入)

在上层调用下层时,只调用下层的接口,对于具体的实现交给spring,在配置文件中,配置对应的实现类,然后进行对应接口属性的注入
在这里插入图片描述

好处

解耦,提高代码可维护性,需要改换实现时方便
如JDBC规范提供了一套接口(statement,resultset,connection),这些接口的实现交由数据库,所以在改换数据库时,改一个驱动即可

基于注解配置

使用context:component-scan标签,属性base-package为包名,来对包进行扫描,对加有注解的类进行管理

注入bean

  1. @Component:没有特殊意义
  2. @Controller:用于控制器类
  3. @Responsitory:用于DAO类
  4. @Service:用于Service类,服务层

注入属性

  1. @Value:简单属性
  2. @Autowired:对象属性,自动装配
  3. @Resource:对象属性,手动指定

spring集成mybatis环境搭建

依赖

数据源(druid),mybatis-spring,spring-jdbc,spring-test测试

数据源配置

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value=""/>
        <property name="url" value=""/>
        <property name="username" value=""/>
        <property name="password" value=""/>
</bean>

SqlSessionFactory定义

  1. 这种方式使用Mapper工厂实体的方式只配置单个Dao
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
</bean>
<bean id="roleDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.rbac.dao.RoleDao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
  1. 这种方式使用Mapper扫配配置器的方式扫描包中的所有Dao,推荐
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.rbac.dao" />
    </bean>

spring-test配置

在测试类上声明注解,对于内部的属性即可通过注入赋值,免去加载配置文件的步骤

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")

配置事务

配置依赖spring-tx
方法一:在Service类上使用@EnableTranctionManagement,在类或方法上使用@Transactional
方法二:在applicationContext.xml中增加tx的命名空间,使用<tx:annotation-driven />,在类或方法上使用@Transaction

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值