一、 數據庫鏈接配置 jeesite.properties 中java
一個的時候mysql
#mysql database setting jdbc.type=mysql jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://xx.xx.xx.xx:3306/xxxxx?useServerPrepStmts=true&cachePrepStmts=true&prepStmtCacheSize=30&prepStmtCacheSqlLimit=256&useUnicode=true&characterEncoding=utf-8 jdbc.username=xxxxx jdbc.password=xxxxx
多個的時候
web
#mysql database setting jdbc.type=mysql jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://xx.xx.xx.xx:3306/xxxxx?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull jdbc.username=xxxxx jdbc.password=xxxxx #mysql database setting jdbc.type_117=mysql jdbc.driver_117= com.mysql.jdbc.Driver jdbc.url_117= jdbc:mysql://yy.yy.yy.yy:3306/yyyyy?useServerPrepStmts=true&cachePrepStmts=true&prepStmtCacheSize=30&prepStmtCacheSqlLimit=256&useUnicode=true&characterEncoding=utf-8 jdbc.username_117=yyyyy jdbc.password_117=yyyyy
二、/src/main/webapp/WEB-INF/web.xml 中添加配置spring
一個的時候
sql
<filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
多個的時候
數據庫
<filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>openSessionInViewFilter_117</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory_117</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter_117</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
三、配置spring-context.xml 緩存
一個的時候
session
<!-- 定義Hibernate Session工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.thinkgem.jeesite"/><!-- 若是多個,用「,」分隔 --> <property name="hibernateProperties"> <props> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="net.sf.ehcache.configurationResourceName">${hibernate.ehcache.configFile}</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.search.default.directory_provider">filesystem</prop> <prop key="hibernate.search.default.indexBase">${hibernate.search.default.indexBase}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> </props> </property> </bean>
多個的時候
app
<!-- 定義Hibernate Session工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.thinkgem.jeesite"/><!-- 若是多個,用「,」分隔 --> <property name="hibernateProperties"> <props> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="net.sf.ehcache.configurationResourceName">${hibernate.ehcache.configFile}</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.search.default.directory_provider">filesystem</prop> <prop key="hibernate.search.default.indexBase">${hibernate.search.default.indexBase}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> </props> </property> </bean> <!-- 定義Hibernate Session工廠 --> <bean id="sessionFactory_117" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource_117"/> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> </props> </property> </bean>
四、 定義鏈接webapp
一個的時候
<!-- 數據源配置, 使用 BoneCP 數據庫鏈接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 數據源驅動類可不寫,Druid默認會自動根據URL識別DriverClass --> <property name="driverClassName" value="${jdbc.driver}" /> <!-- 基本屬性 url、user、password --> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${jdbc.pool.minIdle}" /> <property name="minIdle" value="${jdbc.pool.minIdle}" /> <property name="maxActive" value="${jdbc.pool.maxActive}" /> <!-- 配置獲取鏈接等待超時的時間 --> <property name="maxWait" value="60000" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小(Oracle使用) <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> --> <!-- 配置監控統計攔截的filters --> <property name="filters" value="stat" /> </bean>
多個的時候
<!-- 數據源配置, 使用 BoneCP 數據庫鏈接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 數據源驅動類可不寫,Druid默認會自動根據URL識別DriverClass --> <property name="driverClassName" value="${jdbc.driver}" /> <!-- 基本屬性 url、user、password --> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${jdbc.pool.minIdle}" /> <property name="minIdle" value="${jdbc.pool.minIdle}" /> <property name="maxActive" value="${jdbc.pool.maxActive}" /> <!-- 配置獲取鏈接等待超時的時間 --> <property name="maxWait" value="60000" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小(Oracle使用) <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> --> <!-- 配置監控統計攔截的filters --> <property name="filters" value="stat" /> </bean> <!-- 數據源配置, 使用 BoneCP 數據庫鏈接池 --> <bean id="dataSource_117" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 數據源驅動類可不寫,Druid默認會自動根據URL識別DriverClass --> <property name="driverClassName" value="${jdbc.driver_117}" /> <!-- 基本屬性 url、user、password --> <property name="url" value="${jdbc.url_117}" /> <property name="username" value="${jdbc.username_117}" /> <property name="password" value="${jdbc.password_117}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${jdbc.pool.minIdle}" /> <property name="minIdle" value="${jdbc.pool.minIdle}" /> <property name="maxActive" value="${jdbc.pool.maxActive}" /> <!-- 配置獲取鏈接等待超時的時間 --> <property name="maxWait" value="60000" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小(Oracle使用) <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> --> <!-- 配置監控統計攔截的filters --> <property name="filters" value="stat" /> </bean>
五、配置dao層
增長一個dao 使本身寫的方法繼承
dao:
public class BaseDao_117<T> { @Autowired @Qualifier("sessionFactory_117") private SessionFactory sessionFactory; /** * 實體類類型(由構造方法自動賦值) */ private Class<?> entityClass; /** * 構造方法,根據實例類自動獲取實體類類型 */ public BaseDao_117() { entityClass = Reflections.getClassGenricType(getClass()); } /** * 獲取 Session */ public Session getSession(){ return sessionFactory.getCurrentSession(); } /** * 強制與數據庫同步 */ public void flush(){ getSession().flush(); } /** * 清除緩存數據 */ public void clear(){ getSession().clear(); } }
本身的dao層方法
public class MonitorDataDao extends BaseDao_117{}