栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring常用注解开发以及注解整合Mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring常用注解开发以及注解整合Mybatis

注解驱动
注解启动时使用注解的形式代替xml配置 讲繁杂的spring配置文件从工程中消除 检化书写

注解驱动的弊端
为了达到注解驱动的目的 可能会将原先很简单的书写 变得复杂
XML中配置第三方开发的资源是很方便的 但是使用注解驱动无法在第三发开发的资源中进行编辑 因此增加开发工作量

启动注解功能
启动注解扫描 加载类中配置的注解项

	
在进行包容的扫描时 会对配置的包及其子包中所有文件扫描
扫描过程是以文件夹递归迭代的形式进行的
扫描过程仅读取合法的java文件
扫描时仅读取spring的可识别的注解
扫描结束后将可识别的有效注解转换为spring对应的资源加入IoC容器

无论是注解格式 还是XML配置格式 最终都是将资源加载到IoC容器中
从加载效率上来说注解优于XML配置文件

文件的项目结构

注解整合一下MyBatis

业务类使用注解形式声明bean 属性采用注解注入
建立独立的配置管理类 分类管理外部资源 根据功能进行分类 并提供对应的方法获取bean
使用注解形式启动bean扫描 加载所有注解的资源(bean)
使用AnnoationConfigApplicationContext对象加载所有的启动配置类 内部使用导入方式进行关联

public class MybatisConfig {
//  @Autowired  进行自动装配
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.XS.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    @Bean
    public MapperScannerConfigurer   getMapperScannerConfigurer(){
        MapperScannerConfigurer  msc  = new MapperScannerConfigurer();
        msc.setbasePackage("com.XS.dao");
        return msc;
    }
}

SpringConfig

@Configurable
@ComponentScan("com.XS")
@PropertySource("classpath:jdbc.properties")
@import({JDBCConfig.class,MybatisConfig.class})
public class SpringConfig {
}

AccountDao

public interface AccountDao {

    @Insert(" inset into account(name,money)values (#{name},#{money})")
    void save(Account account);

    @Delete("delete from account where  id = #{id}")
    void delete(Integer id);

    @Update("update account set name = #{name} , money=#{money} where id=#{id}")
    void update(Account account);

    @Select("select * from account")
    List   findAll();

    @Select("select * from account where id = #{id}")
    Account findById(Integer id);
}

Bean的定义
名称 Compponent Controller Service Repository
类注解
作用 设置该类为spring管理的bean

value
定义bean的访问id
IoC

bean的非应用类型属性注入
@Value
类型 属性注解 方法注解
@Value("$(jdbc.username)")
private string username;

value值仅支持非引用类型数据  赋值时的所有参数全部赋值
value值支持读取properties文件中的属性值  通过类型将properties中数据传入类中
value值支持SpEl
@vlaue注解如果添加在属性上方  可以省略set方法(set方法的目的是为了属性值)

value(默认) 定义对应的属性值或参数值

JDBC

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("dataSource")
    public DataSource getDataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

第三方bean配置与管理
@import
导入第三方bean作为spring控制资源
@import 注解在同一个类上 仅允许添加一次 如果需要导入多个 使用数组形式进行设定
在被导入的类中可以继续使用@import导入其他资源
@Bean资源所在的类使用导入的形式进入spring容器 无需声明为bean

@import({JDBCConfig.class,MybatisConfig.class})

bean加载控制
@DependsOn
类注解 方法注解
作用 bean定义的位置(类上或方法上)
控制bean的加载顺序 使其指定bean加载完毕后再加载

配置再方法上 使@DependsOn指定的bean优先于@Bean配置的bean进行加载
配置再类上 使@DependsOn指定的bean优先于当前类所有@Bean配置的bean进行加载
配置再类上 使@DependsOn指定bean优先于@Component等配置的bean进行加载

主函数main

public class app {
    public static void main(String[] args) {
            ApplicationContext    ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

            AccountService accountService1 = (AccountService) ctx.getBean("accountService");
            Account   ac = accountService1.findById(2);
            System.out.println(ac);
    }
}


纯注解格式
@Configuration  @ComponentScan
设置当前类为spring核心配置加载类

核心配合类用于替换Spring核心配置文件  此类可以设置空的  不设置变量与属性
bean扫描工作使用注解@ComponentScan替代

使用AnnotationConfigApplicationContext  加载 对应的配置  时字节码文件
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/292985.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号