标签:getbean on() jar包 under lips java项目 xmla ber oid
spring整合hibernate步骤如下:
@Entity @Table(name="t_user") public class User implements Serializable{ @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
<hibernate-mapping> <class name="cn.wh.vo.User" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <property name="age"/> </class> </hibernate-mapping>
<hibernate-configuration> <session-factory> <property name="myeclipse.connection.profile">mysql</property> <property name="connection.url"> jdbc:mysql://localhost:3306/hibernate4 </property> <property name="connection.username">root</property> <property name="connection.password">1111</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping resource="cn/sxt/vo/User.hbm.xml" /> </session-factory> </hibernate-configuration>
public class UserDaoImpl implements UserDao{ private SessionFactory sessionFactory; @Override public List<User> findAll() { return sessionFactory.openSession().createQuery("from User").list(); } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
public class UserServiceImpl implements UserService { private UserDao userDao; @Override public List<User> findAll() { return userDao.findAll(); } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 整合hibernate第一种及配置方式 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean> --> <!-- 整合hibernate第二种方式 --> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/hibernate4"/> <property name="username" value="root"/> <property name="password" value="1111"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 映射文件映射 <property name="mappingLocations"> <list> <value>classpath:cn/wh/vo/User.hbm.xml</value> </list> </property> --> <property name="annotatedClasses"> <list> <value>cn.wh.vo.User</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <bean id="userDao" class="cn.wh.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userService" class="cn.wh.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> </beans>
public class UserServiceTest { @Test public void testFindAll(){ ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); UserService userService = ac.getBean(UserService.class); List<User> list = userService.findAll(); System.out.println(list.size()); } }
@Entity @Table(name="t_user") publicclass User implements Serializable{ @Id @GeneratedValue(strategy=GenerationType.AUTO) privateintid; private String name; privateintage; publicint getId() { returnid; } publicvoid setId(int id) { this.id = id; } public String getName() { returnname; } publicvoid setName(String name) { this.name = name; } publicint getAge() { returnage; } publicvoid setAge(int age) { this.age = age; } } |
标签:getbean on() jar包 under lips java项目 xmla ber oid
原文地址:http://www.cnblogs.com/forever2h/p/6757105.html