- sessionFactory.getCurrentSession();
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- id="WebApp_ID" version="3.0">
- <display-name>DevelopFramework</display-name>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/webservice/*</url-pattern>
- </servlet-mapping>
- </web-app>
- <?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:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- 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-3.1.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <context:component-scan base-package="com.huawei.inoc.framework" />
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- </list>
- </property>
- </bean>
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="${driverClass}" />
- <property name="jdbcUrl" value="${jdbcUrl}" />
- <property name="user" value="${user}" />
- <property name="password" value="${password}" />
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations" value="classpath:/com/huawei/inoc/framework/model/**/*.hbm.xml" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- <prop key="hibernate.jdbc.fetch_size">50</prop>
- <prop key="hibernate.jdbc.batch_size">25</prop>
- <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
- </props>
- </property>
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- <jaxws:endpoint id="helloWorld" implementor="#helloWorldWebserviceImpl" address="/HelloWorld" />
- <jaxws:client id="client"
- serviceClass="com.huawei.inoc.dummy.webservice.IDemoSupport"
- address="http://localhost:8080/Dummy/webservice/getDate" />
- </beans>
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="${driverClass}" />
- <property name="jdbcUrl" value="${jdbcUrl}" />
- <property name="user" value="${user}" />
- <property name="password" value="${password}" />
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations" value="classpath:/com/huawei/inoc/framework/model/**/*.hbm.xml" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- <prop key="hibernate.jdbc.fetch_size">50</prop>
- <prop key="hibernate.jdbc.batch_size">25</prop>
- <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
- </props>
- </property>
- </bean>
- <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- public interface IGenericDAO<T> {
- void insert(T t);
- void delete(T t);
- void update(T t);
- T queryById(String id);
- List<T> queryAll();
- }
- public abstract class GenericDAO<T> implements IGenericDAO<T> {
- private Class<T> entityClass;
- public GenericDAO(Class<T> clazz) {
- this.entityClass = clazz;
- }
- @Autowired
- private SessionFactory sessionFactory;
- @Override
- public void insert(T t) {
- sessionFactory.getCurrentSession().save(t);
- }
- @Override
- public void delete(T t) {
- sessionFactory.getCurrentSession().delete(t);
- }
- @Override
- public void update(T t) {
- sessionFactory.getCurrentSession().update(t);
- }
- @SuppressWarnings("unchecked")
- @Override
- public T queryById(String id) {
- return (T) sessionFactory.getCurrentSession().get(entityClass, id);
- }
- @Override
- public List<T> queryAll() {
- String hql = "from " + entityClass.getSimpleName();
- return queryForList(hql, null);
- }
- @SuppressWarnings("unchecked")
- protected T queryForObject(String hql, Object[] params) {
- Query query = sessionFactory.getCurrentSession().createQuery(hql);
- setQueryParams(query, params);
- return (T) query.uniqueResult();
- }
- @SuppressWarnings("unchecked")
- protected T queryForTopObject(String hql, Object[] params) {
- Query query = sessionFactory.getCurrentSession().createQuery(hql);
- setQueryParams(query, params);
- return (T) query.setFirstResult(0).setMaxResults(1).uniqueResult();
- }
- @SuppressWarnings("unchecked")
- protected List<T> queryForList(String hql, Object[] params) {
- Query query = sessionFactory.getCurrentSession().createQuery(hql);
- setQueryParams(query, params);
- return query.list();
- }
- @SuppressWarnings("unchecked")
- protected List<T> queryForList(final String hql, final Object[] params,
- final int recordNum) {
- Query query = sessionFactory.getCurrentSession().createQuery(hql);
- setQueryParams(query, params);
- return query.setFirstResult(0).setMaxResults(recordNum).list();
- }
- private void setQueryParams(Query query, Object[] params) {
- if (null == params) {
- return;
- }
- for (int i = 0; i < params.length; i++) {
- query.setParameter(i, params[i]);
- }
- }
- }
- public interface IUserDAO extends IGenericDAO<User> {
- public User queryByName(String userName);
- }
- @Repository
- public class UserDAO extends GenericDAO<User> implements IUserDAO {
- public UserDAO() {
- super(User.class);
- }
- @Override
- public User queryByName(String userName) {
- String hql = "from User u where u.name = ?";
- return queryForObject(hql, new Object[] { userName });
- }
- }
- @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
- public interface IBookService {
- void addBook(Book book);
- }
- public class BookService implements IBookService {
- @Autowired
- private IBookDAO bookDAO;
- @Override
- public void addBook(Book book) {
- bookDAO.insert(book);
- }
- }
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:beans.xml")
- @Transactional
- public class BookDAOTest {
- @Autowired
- private BookDAO bookDAO;
- public void testQueryByIsbn() {
- String isbn = "123";
- Book result = bookDAO.queryByIsbn(isbn);
- String name = result.getName();
- assertEquals("thinking in java", name);
- }
- public void testInsert() {
- Book book = new Book();
- book.setName("bai ye xing");
- book.setIsbn("be bought yesterday");
- bookDAO.insert(book);
- }
- public void testDelete() {
- String id = "test_1";
- Book target = bookDAO.queryById(id);
- bookDAO.delete(target);
- }
- public void testUpdate() {
- String id = "test_1";
- Book target = bookDAO.queryById(id);
- target.setName("i am changeid");
- bookDAO.update(target);
- }
- public void testQueryById() {
- String id = "test_1";
- Book target = bookDAO.queryById(id);
- String name = target.getName();
- assertEquals("thinking in java", name);
- }
- public void testQueryAll() {
- List<Book> books = bookDAO.queryAll();
- assertEquals(3, books.size());
- }
- }
- @Autowired
- private BookDAO bookDAO;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:beans.xml")
- @Transactional
- private void setQueryParams(Query query, Object[] params) {
- if (null == params) {
- return;
- }
- for (int i = 0; i < params.length; i++) {
- query.setParameter(i, params[i]);
- }
- }
- String hql = "from User u where u.name = ?";
- Query query = sessionFactory.getCurrentSession().createQuery(hql);
- query.setParameter(0, name);
- String hql = "from User u where u.name = :name";
- Query query = this.getSession().createQuery(hql);
- query.setParameter("name", name);