1. 程式人生 > >spring配置和映射文件

spring配置和映射文件

1.0 key值 context ini 實體類 Coding ng- app ati

配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd" default-autowire="byType">
<!-- 配置數據源-連接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:195.168.1.195:1521/orcl"></property>
<property name="username" value="t_demo"></property>
<property name="password" value="123456"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="8"></property>
<property name="minIdle" value="5"></property>
</bean>

<!-- 配置SessionFactory -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 引入數據源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 引入映射文件 -->
<property name="packagesToScan">
<!-- 相當於<mapping class="全限定名" /> -->
<list>
<!-- 給出實體類所在範圍 -->
<value>com.oak.entity</value>
</list>
</property>

<!-- 配置參數 -->
<property name="hibernateProperties">
<!-- 集合註入 -->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- 自動建表key值固定的 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<!-- 掃描註解 -->
<context:component-scan base-package="com"></context:component-scan>
<!-- 日誌打印 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

映射文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- hibernate-mapping標簽中有個package屬性,表示持久化類所在的包 package="com.oak.pojo"
如果此處加了package屬性,則class標簽的屬性name值只寫類名即可,
如果hibernate-mapping標簽中沒加package屬性
則class標簽的name值為 全限定名(表名+類名)-->
<hibernate-mapping>
<!-- 對應類和表 -->
<class name="com.oak.entity.Goods" table="T_GOODS">
<!-- 對應的屬性和字段 -->
<id name="id" column="id">
<generator class="native"></generator>
</id>
<!-- 給非主屬性和非主字段 -->
<property name="goodsname" column="goodsname"></property>
<property name="weight" column="weight"></property>
<property name="price" column="price"></property>
<property name="color" column="color"></property>
<property name="shangtime" column="shangtime" type="java.sql.Date"></property>
<property name="stock" column="stock"></property>

<many-to-one name="brand" class="com.oak.entity.Brand">
<column name="BRANDID"></column>
</many-to-one>

<many-to-one name="smc" class="com.oak.entity.SmallClass">
<column name="SID"></column>
</many-to-one>
</class>
</hibernate-mapping>

spring配置和映射文件