spring整合hibernate加sqlite数据库

来源:互联网 发布:js 定时执行 编辑:程序博客网 时间:2024/06/04 15:14
package com.xiuye.dialect;import java.sql.Types;import org.hibernate.MappingException;import org.hibernate.dialect.Dialect;import org.hibernate.dialect.function.SQLFunctionTemplate;import org.hibernate.dialect.function.StandardSQLFunction;import org.hibernate.dialect.function.VarArgsSQLFunction;import org.hibernate.type.StandardBasicTypes;public class SQLiteDialect extends Dialect {public SQLiteDialect() {super();registerColumnType(Types.BIT, "integer");registerColumnType(Types.TINYINT, "tinyint");registerColumnType(Types.SMALLINT, "smallint");registerColumnType(Types.INTEGER, "integer");registerColumnType(Types.BIGINT, "bigint");registerColumnType(Types.FLOAT, "float");registerColumnType(Types.REAL, "real");registerColumnType(Types.DOUBLE, "double");registerColumnType(Types.NUMERIC, "numeric");registerColumnType(Types.DECIMAL, "decimal");registerColumnType(Types.CHAR, "char");registerColumnType(Types.VARCHAR, "varchar");registerColumnType(Types.LONGVARCHAR, "longvarchar");registerColumnType(Types.DATE, "date");registerColumnType(Types.TIME, "time");registerColumnType(Types.TIMESTAMP, "timestamp");registerColumnType(Types.BINARY, "blob");registerColumnType(Types.VARBINARY, "blob");registerColumnType(Types.LONGVARBINARY, "blob");// registerColumnType(Types.NULL, "null");registerColumnType(Types.BLOB, "blob");registerColumnType(Types.CLOB, "clob");registerColumnType(Types.BOOLEAN, "integer");registerFunction("concat", new VarArgsSQLFunction(StandardBasicTypes.STRING, "", "||", ""));registerFunction("mod", new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "?1 % ?2"));registerFunction("substr", new StandardSQLFunction("substr",StandardBasicTypes.STRING));registerFunction("substring", new StandardSQLFunction("substr",StandardBasicTypes.STRING));}@Overridepublic boolean supportsIdentityColumns() {return true;}@Overridepublic boolean hasDataTypeInIdentityColumn() {return false;}@Overrideprotected String getIdentityColumnString() throws MappingException {return "integer";}@Overrideprotected String getIdentitySelectString() throws MappingException {return "select last_insert_rowid()";}@Overridepublic boolean supportsLimit() {return true;}@Overrideprotected String getLimitString(String query, boolean hasOffset) {return new StringBuffer(query.length() + 20).append(query).append(hasOffset ? " limit ? offset ?" : " limit ?").toString();}@Overridepublic boolean supportsTemporaryTables() {return true;}@Overridepublic String getCreateTemporaryTableString() {return "create temporary table if not exits";}@Overridepublic boolean dropTemporaryTableAfterUse() {return false;}@Overridepublic boolean supportsCurrentTimestampSelection() {return true;}@Overridepublic boolean isCurrentTimestampSelectStringCallable() {return false;}@Overridepublic String getCurrentTimestampSelectString() {return "select current_timestamp";}@Overridepublic boolean supportsUnionAll() {return true;}@Overridepublic boolean hasAlterTable() {return false;}@Overridepublic boolean dropConstraints() {return false;}@Overridepublic String getAddColumnString() {return "add column";}@Overridepublic String getForUpdateString() {return "";}@Overridepublic boolean supportsOuterJoinForUpdate() {return false;}@Overridepublic String getDropForeignKeyString() {throw new UnsupportedOperationException("No drop foreign key syntax supported by SQLiteDialect");}@Overridepublic String getAddForeignKeyConstraintString(String constraintName,String[] foreignKey, String referencedTable, String[] primaryKey,boolean referencesPrimaryKey) {throw new UnsupportedOperationException("No add foreign key syntax supported by SQLiteDialect");}@Overridepublic String getAddPrimaryKeyConstraintString(String constraintName) {throw new UnsupportedOperationException("No add primary key syntax supported by SQLiteDialect");}@Overridepublic boolean supportsIfExistsBeforeTableName() {return true;}@Overridepublic boolean supportsCascadeDelete() {return false;}@Overridepublic boolean bindLimitParametersInReverseOrder() {return true;}}
<pre name="code" class="html"><beans default-destroy-method="close" default-lazy-init="true"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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><context:component-scan base-package="com.xiuye"></context:component-scan><bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><property name="jdbcUrl" value="jdbc:sqlite:csms.db"></property><property name="driverClass" value="org.sqlite.JDBC"></property><property name="user" value=""></property><property name="password" value=""></property><property name="initialPoolSize" value="3"></property><property name="maxPoolSize" value="10"></property><property name="minPoolSize" value="1"></property><property name="acquireIncrement" value="3"></property><property name="maxIdleTime" value="60"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="ds"></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">com.xiuye.dialect.SQLiteDialect</prop><prop key="hibernate.hbm2ddl.auto">update</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property><property name="mappingResources"><list><value>com/xiuye/entity/Test.hbm.xml</value></list></property></bean><bean class="org.springframework.orm.hibernate4.HibernateTemplate"id="hibernateTemplate"><property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="txManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><!-- dao 中有什么方法,那么要使用hibernate的HibernateTemplate功能或其他,那么dao中方法必须在次声明啊,否则报错 --><tx:method name="test*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="query*" read-only="true" /><tx:method name="execute*" propagation="REQUIRED" /></tx:attributes></tx:advice><aop:config proxy-target-class="true"><aop:advisor advice-ref="txAdvice" pointcut="within(com.xiuye.dao.*)" /></aop:config><mvc:annotation-driven></mvc:annotation-driven><!-- <mvc:default-servlet-handler/> --><!-- <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean></beans>



<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2016-7-10 14:18:56 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping>    <class name="com.xiuye.entity.Test" table="TEST">        <id name="uuid" type="java.lang.String">            <column name="UUID" />            <generator class="assigned" />        </id>        <property name="name" type="java.lang.String">            <column name="NAME" />        </property>            </class></hibernate-mapping>



0 0
原创粉丝点击