当前位置: 代码迷 >> 综合 >> Spring入门第五讲(Spring整合Mybatis、Spring5.0中新增的注解)
  详细解决方案

Spring入门第五讲(Spring整合Mybatis、Spring5.0中新增的注解)

热度:55   发布时间:2023-12-15 15:37:19.0

Spring整合Mybatis

流程:
1、导入相关的jar 4+1 dao aop mybatis mybatis-spring 驱动包
2、配置Spring的配置文件和Mybatis的配置文件
3、配置数据源
4、配置SqlSessionFactoryBean对象,注入数据源+加载Mybatis的配置文件
5、配置映射文件所在的地址
6、配置事务

<!--1、创建数据源-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="...DriverManagerDataSource"><property name="driverClassName" value="${}"></property><property name="url" value="${}"></property><property name="username" value="${}"></property><property name="password" value="${}"></property>
</bean>
<!--2、创建SqlSessionFactory,注入数据源,加载Mybatis的配置文件-->
<bean id="ssf" class="...SqlSessionFactoryBean"><!--加载mybatis的配置文件--><property name="configLocation" value="classpath:sqlMapConfig.xml"></property><!--加载数据源--><property name="dataSource" ref="dataSource"></property>
</bean>
<!--3、创建mapper代理对象MapperFactoryBean:给单一的mapper接口创建代理对象MapperScannerConfigurer批量创建代理对象 -->
<bean class="...MapperScannerConfigurer"><!--配置xml扫描包--><property name="basePackage" value=""></property><!--配置SqlSessionFactoryBean--><property name="sqlSessionFactoryBeanName" value="ssf"></property>
</bean>
<!--4、声明式事务-->
<bean id="txManager" class="...DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property>
</bean>
<!--事务管理策略-->
<tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="query*" read-only="true"/><tx:method name="insert*" propagation="true"/><tx:method name="update*" propagation="true"/><tx:method name="delete*" propagation="true"/></tx:attributes>
</tx:advice>
<!--织入事务-->
<aop:config><aop:pointcut expression="execution(* service..*(..))" id="myPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
</aop:config>

Spring5.0中新增的注解

引入:使用Java文件通过注解的方式替代xml的配置

思考:在xml文件中所做的事有:扫描、创建对象、配置数据源等

/** 需要声明该类是一个配置类,作用和config.xml一样 Spring提供的新注解: @Configuration作用:指定当前类是一个配置类注意:如果该类作为了Annotation...的参数时,该注解可省略不写 @ComponentScan(@ComponentScans是数组的形式)作用:用于指定Spring在创建容器时需要扫描的包属性:--(点击查看源码说明其中的属性)value:和basePackages的作用一样,用于指定创建容器时需要扫描的包该注解的使用相当于在xml中:<context:component-scan base-package=""></context:component-scan> @Bean作用:用于把当前方法的返回值作为bean对象存入到spring的ioc容器中属性:name:用于指定bean的id。不写则默认为当前方法的名称注意:当方法中带有参数时,会去spring的容器中进行查找,查找的方式和Autowired一样,未找到则报错 */
@Configuration
//@ComponentScan(basePackages={})
//@ComponentScan(basePackages = "cn.yunhe.service")
@ComponentScan("cn.yunhe.service")
public class SpringConfig{
    @Bean(name="runner")public QueryRunner createQueryRunner(DataSource dataSource){
    return new QueryRunner(dataSource);}@Bean(name="dataSource")public DataSource createDateSource(){
    try{
    ComboPooledDataSource ds = new ComboPooledDataSource();ds.setDriverClass("");ds.setJdbcUrl("");ds.setUser("");ds.setPassword("");return ds;}catch(Exception e){
    throw new RunTimeException();}}
}

测试上述代码时发现,之前是通过ClassPathXml…来读取xml文件的,现在变成了java文件,该如何去读取对应的java文件进行容器创建呢?

答:在ApplicationContext的子类中除了ClassPath…之外还有一个Annotation…类用于加载被@Configuration标注的java文件,其它的不变。

注意:通过@Bean获取到的对象为单例模式,可通过在@Bean的下方添加@Scope改变管理方式—需演示

public class UserServiceTest{
    @Testpublic void queryAll(){
    //1.获取容器对象ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);//2.获取对象//3.执行查询方法}
}

思考:当配置类出现多个,例如:一个主配置类(SpringConfig),多个子配置类(jdbcConfig或者事务配置类等),该如何进行关联?

@ComponentScan({
    "cn.yunhe.service","cn.yunhe.config"})
@Import(JdbcConfig.class)//此时,JdbcConfig.class中不再需要@Configuration注解
public class SpringConfig{
    }

思考:数据库的配置类中关于数据库相关的数据又出现了硬编码问题,该如何处理?

方案:和之前一样,创建properties文件,进行引入 (需要创建properties文件)

@PropertySource(“classpath:jdbcConfig.properties”)进行文件的引入

//1.在jdbcConfig文件中指定对应的属性
public class JdbcConfig{
    @Value("${jdbc.diver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;@Bean(name="runner")public QueryRunner createQueryRunner(DataSource dataSource){
    return new QueryRunner(dataSource);}@Bean(name="dataSource")public DataSource createDateSource(){
    try{
    ComboPooledDataSource ds = new ComboPooledDataSource();ds.setDriverClass(driver);ds.setJdbcUrl(url);ds.setUser(username);ds.setPassword(password);return ds;}catch(Exception e){
    throw new RunTimeException();}}
}
//2.在主配置类中指定查找的路径
@Import("JdbcConfig.class")
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfig{
    }

思考:纯注解的方式也并没有简化开发,那么和xml配置的方式相比该如何选择呢?

方案:

? 1.遵循企业要求

? 2.结合使用,比如:@Service和@Autowired的使用结合xml配置文件

  相关解决方案