Spring Spring整合

1 Spring整合Mybatis

(1) 项目中导入整合需要的jar包

<dependency>
    <!--Spring操作数据库需要该jar包-->
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.10.RELEASE</version>
</dependency>
<dependency>
    <!--Spring与Mybatis整合的jar包,这个jar包mybatis在前面,是Mybatis提供的。-->
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.0</version>
</dependency>
//注:spring-context要和spring-test版本一致

(2) 创建Spring的主配置类

//配置类注解
@Configuration
//包扫描,主要扫描的是项目中的AccountServiceImpl类
@ComponentScan("com.itheima")
public class SpringConfig {
}

(3) 创建数据源的配置类

//在配置类中完成数据源的创建
public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;
    
    @Bean
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

(4) 主配置类中读properties并引入数据源配置类

@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
//读取properties配置文件,主配置类中所读取的properties配置文件,第三方配置类也可读取其中信息。
@Import(JdbcConfig.class)
//导入dataSource的bean。
public class SpringConfig {
}

(5) 创建Mybatis配置类并配置SqlSessionFactory

public class MybatisConfig {
    //定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象。
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        //参数为dataSource,dataSource中有数据库连接信息。
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        //设置模型类的别名扫描
        //代替了初始化类型别名部分(<typeAliases>),即为实体类设置别名部分。
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        //设置数据源
        //代替了mybatis配置文件中初始化dataSource部分(<dataSource>),即数据库连接部分。
        ssfb.setDataSource(dataSource);
        return ssfb;
    }
    //定义bean,返回MapperScannerConfigurer对象。
    //初始化Mapper接口和映射文件,这个是在获取到SqlSession以后执行具体操作的时候用,所以它和SqlSessionFactory创建的时机都不在同一个时间,需要单独管理。
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //设置Dao层所在包(或者说mapper文件所在文件夹)
        //代替了mybatis配置文件中初始化映射配置部分(<mappers>),即mapper(Dao接口)扫描部分。
        msc.setBasePackage("com.itheima.dao");
        //MapperScannerConfigurer会扫描这个包中的所有接口,把每个接口都执行一次getMapper方法,得到每个接口的dao对象(即mapper代理),创建好的dao对象放入到spring的容器中。
        //所以这意味着spring配置类可以不扫描Dao层接口,因为这句代码已经将dao对象的bean放到容器中了,但不建议不写。
        return msc;
    }
}

(6) 主配置类中引入Mybatis配置类

@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
//引入多个第三方配置类需使用数组格式。
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}

(7) 编写运行类

//在运行类中,从IOC容器中获取Service对象,调用方法获取结果。
public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        AccountService accountService = ctx.getBean(AccountService.class);
        //直接调用所需要使用的service或dao对象。
        //MapperScannerConfigurer会扫描所有dao接口,把每个dao接口都执行一次getMapper方法,得到每个接口的dao对象(即mapper代理),创建好的dao对象放入到spring的容器中。
        //在使用service对象时,其中的dao属性会自动被从Ioc容器中被注入,所以dao对象和service对象都可以直接被使用。
        Account ac = accountService.findById(1);
        System.out.println(ac);
    }
}
 

2 Spring整合Junit

//未整合之前,@Test不能使用@Autowired等spring的功能。

(1) 引入依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    //版本过低会导致不能使用。
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.10.RELEASE</version>
</dependency>

(2) 编写测试类

//在test\java下创建一个AccountServiceTest
//设置类运行器
//Junit运行后是基于Spring环境运行的,所以Spring提供了一个专用的类运行器,这个务必要设置,这个类运行器就在Spring的测试专用包中提供的,导入的坐标就是这个东西SpringJUnit4ClassRunner。
@RunWith(SpringJUnit4ClassRunner.class)
//当使用纯注解开发时,加载对应的配置类。
@ContextConfiguration(classes = {SpringConfiguration.class})
//当使用xml开发时,加载对应的配置文件。
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class AccountServiceTest {
//支持自动装配注入bean
    @Autowired
    private AccountService accountService;
  
    @Test
    public void testFindById(){
        System.out.println(accountService.findById(1));
    }
    @Test
    public void testFindAll(){
        System.out.println(accountService.findAll());
    }
}

 

posted @ 2023-10-17 21:00  10kcheung  阅读(22)  评论(0)    收藏  举报