Hibernate/Spring初配置方法详解
1、下载Jar包并导入
2、配置文件
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5311')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5311><?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/hibernate/model/Student.hbm.xml"/>
<mapping class="com.hibernate.model.Teacher"/>
</session-factory>
</hibernate-configuration>
3、实体类
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1690')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1690>
@Entity
//@Table(name="_teacher")
public class Teacher {
private String id;
private String name;
private String title;
public Teacher() {
super();
}
public Teacher(String id, String name, String title) {
super();
this.id = id;
this.name = name;
this.title = title;
}
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Spring初配置
1.首先依然是导入相关的jar包。
2.创建配置文件:applicationContext.xml。
2.1.导入schema。
2.2
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8287')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8287><bean id="user" class="com.spring.pojo.User"></bean>注:
1.BeanFactory和ApplicationContext的主要区别:BeanFactory延迟加载所有的 Bean,直到 getBean()方法被调用的时候 Bean才会被创建;ApplicationContext则会预载入单例 Bean,当调用 getBean()方法的时候, Bean已经被创建成功。
2.配置bean的时候其中可以有id和name属性,id具有唯一性,在使用中必须和 java 中命名变量一样去命名id的值,但是name属性值则没有要求。name 属性并不是必须的,当不写 name 属性时,spring 容器会自动生成 name 属性值。那么在获取该 bean的时候,name属性和class属性值相同。
3. 通过alias节点可以为bean指定一个别名
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9600')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9600><bean name="userDao" class="com.spring.dao.UserDao"></bean>
<alias name="userDao" alias="ud" />
您可能感兴趣的文章:
Hibernate/Spring初配置方法详解
Spring 学习笔记--强烈推荐
linux中SSH环境搭建详解
SpringMVC整合EhCache实现对象缓存的详解
快速部署 Spring PetClinic 到函数计算平台
Xml解析校验引起的依赖问题
java中spring集成mybatis的使用方法
Hibernate含List属性的持久化类的CRUD操作范例
Java中Hibernate单向(1-N)映射实例详解
Hibernate学习笔记之基本配置详解