關鍵 Bean 定義:
<context:property-placeholder location="classpath:database.properties"/> 讀取外部配置。<context:component-scan base-package="com.lingdong"/> 自動註冊 Bean。database.properties 注入(driverClass、url、user、password、initialPoolSize、maxPoolSize)。LocalSessionFactoryBean,引用 dataSource,configLocation 指向 hibernate.cfg.xml,mappingLocations 掃描 *.hbm.xml。HibernateTransactionManager + tx:advice 定義傳播行為(add*/update*/delete* REQUIRED,select* read-only)+ aop:config 切入 com.lingdong.service.*。<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
說明:方言用 MySQL5InnoDB,自動更新表結構,開啟 SQL 顯示與格式化,便於開發調試。
LocalSessionFactoryBean 同時指定 configLocation 與 mappingLocations 時,映射文件會合併而非覆蓋。execution(* com.xxx.service.*.*(..)) 精準切入 Service 層,避免誤切 DAO 或 Controller。show_sql,改用 P6Spy 或 Log4jdbc 記錄慢 SQL。