81,092
社区成员
发帖
与我相关
我的任务
分享
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
default-lazy-init="true">
<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="com.skynet.cdw" />
<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">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="autoCommitOnClose" value="true" />
<property name="checkoutTimeout" value="${cpool.checkoutTimeout}" />
<property name="initialPoolSize" value="${cpool.minPoolSize}" />
<property name="minPoolSize" value="${cpool.minPoolSize}" />
<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}" />
</bean>
<!-- Jpa Entity Manager 配置 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" />
<!-- <property name="packagesToScan" value="com.skynet.cdw.**.entity"/> -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="generateDdl" value="false" />
</bean>
</property>
</bean>
<!-- 事务管理器配置, Jpa单数据源事务 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- 使用annotation定义事务 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
<tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.skynet.cdw..service..*.*(..))"/>
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
</aop:config>
<!-- 整个平台的安全框架考虑使用 spring security框架来做 ,基础框架阶段暂时不需要启用-->
<!-- <import resource="spring-security.xml"/> -->
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="cdwPersistence"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.skynet.cdw.platform.entity.Resource</class>
<properties>
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.jdbc.fetch_size" value="50" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
package com.skynet.cdw.common.dao;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import org.springframework.util.Assert;
/**
* 底层数据持久层公共实现类
* @param <T> 实体类型
* @param <ID> 主键类型
*/
public class BaseDaoImpl<T, ID extends Serializable> implements BaseDao<T, ID>
{
private Class<T> persistentClass;
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
private EntityManager em;
@SuppressWarnings({"unchecked", "rawtypes"})
public BaseDaoImpl()
{
this.persistentClass =
(Class)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public Class<T> getPersistentClass()
{
return persistentClass;
}
/**
* 对象实体 持久化操作
*/
public T save(T entity)
{
Assert.notNull(entity, "entity 不能为 null");
em.persist(entity);
return (T)entity;
}
/**
* 对象实体 持久化更新
*/
public T update(T entity)
{
Assert.notNull(entity, "entity 不能为 null");
em.merge(entity);
return (T)entity;
}
/**
* 删除操作
*/
public void delete(T entity)
{
Assert.notNull(entity, "entity 不能为 null");
em.remove(entity);
}
/**
* 根据ID主键进行查询
*/
public T findById(ID id)
{
Assert.notNull(id, "id 不能为空");
return (T)em.find(getPersistentClass(), id);
}
/**
* 根据id数组进行查询
* idName : ids 对应的属性名称
*/
@SuppressWarnings("unchecked")
public List<T> findByIds(String idName, ID[] ids)
{
Assert.notEmpty(ids, "ids 不能为空");
String hql = "from " + getPersistentClass().getName() + " as model where model." + idName + " in(:ids)";
return em.createQuery(hql).setParameter("ids", ids).getResultList();
}
/**
* 进行原始sql查询
*
* @param sql
* @return
*/
@SuppressWarnings("unchecked")
public List<T> createQuery(String sql)
{
Assert.notNull(sql, "sql 不能为 null");
Query query = em.createQuery(sql);
if (query != null)
{
return query.getResultList();
}
return new ArrayList<T>();
}
/**
* 查询所有数据
*/
@SuppressWarnings("unchecked")
public List<T> findAll()
{
StringBuffer sql = new StringBuffer("from ");
sql.append(getPersistentClass().getName());
sql.append(" obj");
return em.createQuery(sql.toString()).getResultList();
}
/**
* 获取总记录数
*/
public int findRowCount()
{
StringBuffer sql = new StringBuffer("select count(*) from ");
sql.append(getPersistentClass().getName());
sql.append(" obj");
Object object = em.createQuery(sql.toString()).getResultList().get(0);
if (object != null)
{
return Integer.parseInt(object.toString());
}
return 0;
}
/**
* 根据传入的sql进行更新操作
*/
@SuppressWarnings("rawtypes")
public Integer updateBySql(String sql, Class cls)
{
Assert.notNull(sql, "sql 不能为 null");
Assert.notNull(cls, "cls 不能为 null");
Query query = em.createNativeQuery(sql, cls);
return query.executeUpdate();
}
}
package com.skynet.cdw.platform.dao.impl;
import org.springframework.stereotype.Repository;
import com.skynet.cdw.common.dao.BaseDaoImpl;
import com.skynet.cdw.platform.dao.ResourceDao;
import com.skynet.cdw.platform.entity.Resource;
@Repository
public class ResourceDaoImpl extends BaseDaoImpl<Resource, Integer> implements ResourceDao
{
}
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.skynet.cdw.platform.**" ></context:component-scan>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 视图解释类 -->
<bean id="freemarkerViewResolver" class="com.skynet.cdw.common.springmvc.SimpleFreeMarkerViewResolver">
<property name="suffix" value=".html"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="exposeRequestAttributes" value="false"/>
<property name="exposeSessionAttributes" value="false"/>
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 将页面模板定义到jar包下,模块化方便管理和扩展 -->
<property name="templateLoaderPath" value="/WEB-INF/page/admin/"/>
<property name="freemarkerVariables">
<map>
<!-- 将默认的根路径存入到request中 -->
<entry key="appBase" value="/admin/"/>
</map>
</property>
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
<prop key="whitespace_stripping">true</prop>
<!-- <prop key="auto_import">/ftl/jeecms/index.ftl as p,/ftl/spring.ftl as s</prop> -->
</props>
</property>
</bean>
package com.skynet.cdw.platform.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.skynet.cdw.common.service.BaseService;
import com.skynet.cdw.platform.dao.ResourceDao;
import com.skynet.cdw.platform.entity.Resource;
@Service
@Transactional
public class ResourceService implements BaseService<Resource, Integer>
{
@Autowired
private ResourceDao resourceDao;
public List<Resource> findAll()
{
return resourceDao.findAll();
}
public List<Resource> findById(Integer id)
{
// TODO Auto-generated method stub
return null;
}
@Transactional
public void save(Resource entity)
{
resourceDao.save(entity);
Resource res = new Resource();
res.setName("资源管理");
resourceDao.save(res);
}
public void delete(Resource entity)
{
}
public void delete(Integer id)
{
// TODO Auto-generated method stub
}
public void update(Resource entity)
{
// TODO Auto-generated method stub
}
public ResourceDao getResourceDao()
{
return resourceDao;
}
public void setResourceDao(ResourceDao resourceDao)
{
this.resourceDao = resourceDao;
}
}