<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value></property>
<property name="url"><value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs</value></property><property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="mappingResources">
<list>
<value>com/spring/text/Login.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
</bean>
<bean id="login" class ="com.spring.text.HelloBean">
<property name="name">
<value>accps</value>
</property>
</bean>
<bean id="loginDao" class="com.spring.text.LoginDAO">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
</beans>
======================================
public class LoginDAO {
DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
==========================
测试能否得到数据源
***************************************
public class SpringHibernateDemo
{
public static void main(String[] args) throws Exception {
ApplicationContext context = new
FileSystemXmlApplicationContext("applicationContext.xml");
LoginDAO login = (LoginDAO) context.getBean("loginDao");
System.out.println(login.getDataSource().getConnection());
}
有个地方一定要注意就是application.xml 中的datasoruce
不要换行,or会发生no sutiable driver一个小的问题往往要发很多时间去审查。。
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value></property>
<property name="url"><value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs</value></property><property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="mappingResources">
<list>
<value>com/spring/text/Login.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
</bean>
<bean id="login" class ="com.spring.text.HelloBean">
<property name="name">
<value>accps</value>
</property>
</bean>
<bean id="loginDao" class="com.spring.text.LoginDAO">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
</beans>
======================================
public class LoginDAO {
DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
==========================
测试能否得到数据源
***************************************
public class SpringHibernateDemo
{
public static void main(String[] args) throws Exception {
ApplicationContext context = new
FileSystemXmlApplicationContext("applicationContext.xml");
LoginDAO login = (LoginDAO) context.getBean("loginDao");
System.out.println(login.getDataSource().getConnection());
}
有个地方一定要注意就是application.xml 中的datasoruce
不要换行,or会发生no sutiable driver一个小的问题往往要发很多时间去审查。。
本文展示了一个使用Spring框架整合Hibernate的配置实例,包括数据源、会话工厂及DAO层的配置方式。通过具体XML配置文件及Java代码,演示了如何在Spring中设置数据库连接并实现基本的数据访问操作。
2302

被折叠的 条评论
为什么被折叠?



