环境: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
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>