標籤:適配器 c3p0 connector query dao uri jms 上傳 自己
廢話不多少
項目結構
pom.xml配置例如以下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>test</groupId><artifactId>springmvc_spring_jpa</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springmvc_spring_jpa</name><url>http://maven.apache.org</url><!--各個jar的版本號碼 --><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.version>4.0.0.RELEASE</spring.version><spring.jpa.version>1.8.0.RELEASE</spring.jpa.version><shiro.version>1.2.2</shiro.version><mysql.connector.version>5.1.21</mysql.connector.version><aspectj.version>1.6.11</aspectj.version><c3p0.version>0.9.5-pre3</c3p0.version><slf4j.version>1.7.2</slf4j.version><log4j.version>1.2.16</log4j.version><junit.version>4.11</junit.version><jsonlib.version>2.4</jsonlib.version></properties><dependencies><!-- spring-jpa規範 --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId><version>${spring.jpa.version}</version></dependency><!--spring bean容器 --><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><!-- spring配置 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><!-- springweb串連 --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version><type>jar</type><scope>compile</scope></dependency><!-- spring webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!-- spring測試 --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><!-- spring切面 --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><!-- spring 資料庫連接 --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version><type>jar</type><scope>runtime</scope></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>4.2.7.SP1</version></dependency><!-- hibernate驗證 --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.1.3.Final</version></dependency><!-- 面向切面擴充 --><dependency><groupId> org.aspectj</groupId><artifactId> aspectjweaver</artifactId><version>${aspectj.version}</version></dependency><!-- 資料庫連接池 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>${c3p0.version}</version><type>jar</type><scope>compile</scope></dependency><!-- mysql串連 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.connector.version}</version><type>jar</type><scope>compile</scope></dependency><!-- 單元測試 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- Shiro security (許可權控制) --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-cas</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>${shiro.version}</version></dependency><!-- end of Shiro security --><!-- logging 日誌 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.2</version></dependency><!-- io包 --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>1.3.2</version></dependency><!-- fileUpload 檔案上傳 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.2.2</version></dependency><!-- redis緩衝 --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.6.2</version><type>jar</type><scope>compile</scope></dependency><!-- spring-redis --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.4.2.RELEASE</version></dependency></dependencies><!-- Build support --><build><plugins><!-- jdk版本號碼 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.0</version><configuration><source>1.7</source><target>1.7</target><encoding>utf-8</encoding></configuration></plugin><!-- 相關檔案編碼 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><encoding>utf-8</encoding></configuration></plugin><!-- tomcat版本號碼 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.0-beta-1</version><configuration><additionalProjectnatures><projectnature>org.eclipse.jdt.core.javanature</projectnature><projectnature>org.eclipse.m2e.core.maven2Nature</projectnature><projectnature>org.springframework.ide.eclipse.core.springnature</projectnature></additionalProjectnatures><additionalBuildcommands><buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand><buildcommand>org.eclipse.m2e.core.maven2Builder</buildcommand><buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand></additionalBuildcommands><uriEncoding>utf-8</uriEncoding><port>8020</port><path>/</path></configuration></plugin></plugins></build><!-- end of Build support --></project>
applicationContext.xml配置例如以下
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:task="http://www.springframework.org/schema/task"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"><!-- 配置者:JaneHuang --><!-- 啟動組件掃描,排除@Controller組件,該組件由SpringMVC設定檔掃描 --><context:component-scan base-package="com.kugou"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan><!-- 屬性檔案位置 --><bean id="annotationPropertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:config/properties/jdbc.properties</value><value>classpath:config/properties/common.properties</value><value>classpath:config/properties/log4j.properties</value><value>classpath:config/properties/redis.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="${jdbc.defaultAutoCommit}" /><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><!-- jdbc實體管理器 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource"><ref bean="dataSource" /></property></bean><!-- JPA實體管理工廠的配置 --><bean id="entityManagerFactory"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="dataSource" ref="dataSource" /><property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" /><property name="packagesToScan" ref="packagesToScan" /><!--待掃描的實體類包,不再須要persistence.xml了 --><property name="jpaProperties"><props><prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!--指定實現JPA的適配器 --><bean id="hibernateJpaVendorAdapter"class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"><property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /></bean><!-- 重要配置:啟用掃描並自己主動建立代理的功能 --><jpa:repositories base-package="com.kugou"transaction-manager-ref="transactionManager"entity-manager-factory-ref="entityManagerFactory" /><!-- 實體映射 --><bean id="packagesToScan"class="org.springframework.beans.factory.config.ListFactoryBean"><property name="sourceList"><list><value>com.kugou.security.entity</value></list></property></bean><!-- 配置redis線程池 --><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><!-- <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="maxActive" value="${redis.pool.maxActive}" /> <property name="maxWait" value="${redis.pool.maxWait}" /> --><property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /></bean><!-- 串連redis --><bean id="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}"p:pool-config-ref="poolConfig" /><!-- redis調用須要的bean --><bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"><property name="connectionFactory" ref="connectionFactory" /></bean> <!-- 事務配置 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean><!-- 註解式事務 --><tx:annotation-driven transaction-manager="transactionManager" /><!--spring檔案上傳 --><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="utf-8"></property><property name="maxUploadSize" value="204800" /></bean><!-- 定時器註解支援 --><task:annotation-driven /><!-- 線程池配置 --><bean id="taskExecutor"class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"><property name="corePoolSize" value="2" /><property name="maxPoolSize" value="5" /><property name="queueCapacity" value="6" /><property name="keepAliveSeconds" value="2000" /><property name="rejectedExecutionHandler"><bean class="java.util.concurrent.ThreadPoolExecutor$AbortPolicy" /></property></bean></beans>
JPA部分代碼
package com.kugou.security.dao;import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.CrudRepository;import org.springframework.data.repository.query.Param;import org.springframework.stereotype.Repository;import com.kugou.security.entity.SysUser;@Repositorypublic interface SysUserDao extends CrudRepository<SysUser,String>{@Query("from SysUser as s where s.username=:username")SysUser findByName(@Param("username")String username);}
redis相關代碼
package com.kugou.security.service.impl;import javax.annotation.Resource;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.RedisCallback;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.stereotype.Service;import com.kugou.security.entity.SysUser;import com.kugou.security.service.SysUserService;@Servicepublic class SysUserServiceimpl implements SysUserService {@ResourceRedisTemplate<String, SysUser> redisTemplate;public boolean addUser(final SysUser sysUser) {boolean result = this.redisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();byte[] key = serializer.serialize(sysUser.getId());byte[] name = serializer.serialize(sysUser.getNickname());return connection.setNX(key, name);}});return result;}public String get(final String userId) {String result = redisTemplate.execute(new RedisCallback<String>() {public String doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();byte[] key = serializer.serialize(userId);byte[] value = connection.get(key);if (value == null) {return null;}String name = serializer.deserialize(value);return name;}});return result;}}
其它我不寫了,跟我之前的文章好多反覆的代碼跟配置
springmvc+spring+jpa(hibernate)+redis+maven配置