步骤 2 : applicationContext.xml 步骤 3 : sql 语句 步骤 4 : TestSpring
设置自增长id为sequence
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.how2java.pojo">
<class name="Category" table="bigsword.category_">
<id name="id" column="id">
<generator class="native">
<param name="sequence">bigsword.category_sequence</param>
</generator>
</id>
<property name="name" />
</class>
</hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.how2java.pojo"> <class name="Category" table="bigsword.category_"> <id name="id" column="id"> <generator class="native"> <param name="sequence">bigsword.category_sequence</param> </generator> </id> <property name="name" /> </class> </hibernate-mapping>
设置连接oracle的
驱动 url 方言 账号密码 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="yyy" />
</bean>
<bean name="dao" class="com.how2java.dao.CategoryDAO">
<property name="sessionFactory" ref="sf" />
</bean>
<bean name="sf"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="ds" />
<property name="mappingResources">
<list>
<value>com/how2java/pojo/Category.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.show_sql=true
hbm2ddl.auto=update
</value>
</property>
</bean>
<bean name="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="bigsword" />
<property name="password" value="bigsword" />
</bean>
</beans>
首先用bigsword账号登录
创建sequence 创建table create sequence bigsword.category_sequence
create table bigsword.category_(
id number,
name varchar2(30)
)
create sequence bigsword.category_sequence create table bigsword.category_( id number, name varchar2(30) )
运行测试代码
package com.how2java.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.dao.CategoryDAO;
import com.how2java.pojo.Category;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
CategoryDAO dao = (CategoryDAO) context.getBean("dao");
Category c =new Category();
c.setName("category 1");
dao.save(c);
List<Category> cs= dao.find("from Category c");
System.out.println(cs);
}
}
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2019-10-20
hibernate有默认方言吗?
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|