Spring Hibernate 整合项目案例之@Entity注解
2012-03-05 19:00
399 查看
Hibernate: insert into users (username) values (?)
数据库中的表名为users-->对应java实体类Users{Users.java或者users.java(不区分大小写)}
或者使用注解@table(name=“users”)
列名username-->对应实体类Users中的属性username
实体Users类的代码:(Users.java)
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Users {
private int id;
private String username;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
配置文件beans.xml:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.wangwang" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.wangwang.model.Users</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
数据库中的表名为users-->对应java实体类Users{Users.java或者users.java(不区分大小写)}
或者使用注解@table(name=“users”)
列名username-->对应实体类Users中的属性username
实体Users类的代码:(Users.java)
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Users {
private int id;
private String username;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
配置文件beans.xml:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.wangwang" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.wangwang.model.Users</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
相关文章推荐
- spring+hibernate开发中关于项目中@Entity注解报错
- Spring Hibernate 整合项目案例之@Entity注解
- spring,hibernate,struts2三大框架注解方式整合案例
- 【SSH网上商城项目实战01】整合Struts2、Hibernate4.3和Spring4.2
- Struts2.3+Spring2.5.6+JPA(Hibernate3)项目移植入WebLogic12容器,并整合入MyEclipse10 图文详解
- Hibernate注解使用以及Spring整合
- 使用注解方式进行spring和hibernate整合
- Maven整合Spring,Spring mvc4.1.6,Hibernate4.3.10项目搭建总结
- Hibernate注解使用以及Spring整合
- 用STS工具创建springboot+nutz+beetl项目整合案例
- 菜鸟如何简单整合hibernate+spring(注解的方式)
- spring整合hibernate(注解、xml)applicationContext.xml配置
- spring + spring mvc + mybatis 整合项目 全注解方式
- 使用注解方式进行spring和hibernate整合
- spring 整合hibernate项目配置文件
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(一)整合配置
- Spring整合Hibernate时出现如下错误Caused by: java.lang.NoClassDefFoundError: javax/persistence/Entity
- spring整合hibernate(注解、xml)applicationContext.xml配置
- Spring整合Struts2和Hibernate+Maven(一)之Maven项目创建