spring boot 添加扫包 集成 jpa

hibernate实体类:

@Entity
@Table(name = "tb_uc_account")
public class Account {

	@Id
	@GeneratedValue
	@Column(nullable = false, name = "ucac_fl_id")
	private long flId;
	@Column(nullable = false, name = "ucac_fl_name")
	private String username;
	@Column(nullable = false, name = "ucac_fl_password")
	private String password;
	@Column(nullable = false, name = "ucac_regist_time")
	private Date registDate;
	@Column(nullable = false, name = "ucac_status")
	private int status;

	/**
	 * 账号绑定信息
	 */
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
	@JoinColumn(name ="uab_fl_id")
	private List<AccountBind> bindInfos;

	
	/**
	 * 游戏账号信息
	 */
	@OneToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "aabi_fl_id")
	// @JoinColumn(name = "uab_user_id", referencedColumnName = "ucac_user_id")
	private List<AccountAppBind> appBindInfos;
	
	/**
	 * 一个用户玩过的游戏信息:
	 */
	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name = "uag_user_id")
	private List<AccountGameH> games;

	public Account() {
	}

	public Account(String username, String password, int status) {
		super();
		this.username = username;
		this.password = password;
		this.registDate = new Date(System.currentTimeMillis());
		this.status = status;
	}
	
	public long getFlId() {
		return flId;
	}

	public void setFlId(long flId) {
		this.flId = flId;
	}
。。。。。

@Table@Entity是hibernate-jpa的注解,那么spring boot的容器如何管理这些bean呢?如何识别呢?????


数据源配置类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= { "com.feiliu.monkey.open.repo.primary" }) //设置Repository所在位置
public class PrimaryConfig {

    @Autowired @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(primaryDataSource)
                .properties(getVendorProperties(primaryDataSource))
                .packages("com.open.domain.primary") //设置实体类所在位置
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }


通过配置类可以看到:通过EntityManagerFactoryBuilder类来添加需要注入的bean;  函数 .packages(“。。。”)里 是需要扫描的bean的包路径。和spring mvc 添加扫包路径一个意思。

那么可以猜测,spring容器一定对hibernate的注解实现了自己的处理,来注入这些bean。

参考:https://www.cnblogs.com/guscode/p/5518464.html


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值