Spring3.1+Hibernate4.1整合
2015-06-09 15:46
621 查看
环境:MyEclipse2014
步骤:
1.创建项目,导入mysql connector到lib下
2.右击项目名->MyEclipse->Project facets->install Spring... 弹出窗口后,之间点击finish.
3.同步骤2->install hibernate ,出现窗口后点击next(如图)
next,这里我使用的是JDBC.
点击finish.
4.利用反向工程(reverse engineering,具体使用请百度).创建bean
5.这里就不写service了,直接在dao上测试出结果,创建DAO
6.别急着测试,这里还有小问题,applicationContext.xml里应该添加
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="glut"></context:component-scan>
(我这里的包明明是以glut.*命名的),完整代码:
最后,运行JUnit即可.
步骤:
1.创建项目,导入mysql connector到lib下
2.右击项目名->MyEclipse->Project facets->install Spring... 弹出窗口后,之间点击finish.
3.同步骤2->install hibernate ,出现窗口后点击next(如图)
next,这里我使用的是JDBC.
点击finish.
4.利用反向工程(reverse engineering,具体使用请百度).创建bean
5.这里就不写service了,直接在dao上测试出结果,创建DAO
package glut.dao; import javax.annotation.Resource; import glut.bean.Admin; import org.hibernate.SessionFactory; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; @Component("adminDAO") public class AdminDAO { private SessionFactory sessionFactory; @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); AdminDAO ad = context.getBean(AdminDAO.class); ad.add(new Admin()); } public void add(Admin admin) { System.out.println("Adding"); } public SessionFactory getSessionFactory() { return sessionFactory; } @Resource(name = "sessionFactory") public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
6.别急着测试,这里还有小问题,applicationContext.xml里应该添加
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="glut"></context:component-scan>
(我这里的包明明是以glut.*命名的),完整代码:
<?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:p="http://www.springframework.org/schema/p" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="user"></property> <property name="password" value="1234"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="annotatedClasses"> <list> <value>glut.bean.Admin</value></list> </property></bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <context:annotation-config></context:annotation-config> <context:component-scan base-package="glut"></context:component-scan> </beans>
最后,运行JUnit即可.
相关文章推荐
- Java Solution (Basic Calculator) ,
- eclipse里将web工程打包成WAR
- 【Java】使用Junit测试线程过程中出现的小问题
- eclipse打开文件位置插件
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
- java基础复习之六:数据类型的转换
- spring中的命名空间
- Ubuntu配置eclipse
- Spring常用注解,自动扫描bean
- maven+springMVC+mybatis+junit详细搭建过程
- SpringMVC整合fastjson-1.1.41
- spring使用注解实现AOP
- 杭电ACM2023java做法
- spring的编程式事务、XML配置事务、注解实现事务
- Mybatis学习(5)与spring3集成
- spring的数据源配置DBCP,C3PO,BoneCP
- 如果debug 启动spring 时跳到concurrentHashMap中……
- Spring Bean的5种作用域
- java 中有几种类型的流?JDK 为每种类型的流提供了一些抽象类以供继承, 请说出他们分别是哪些类?
- Ubuntu系统下安装Myeclipse10.6