SPRINGMVC+SPRING+JPA (Hibernate) +redis+maven configuration

Source: Internet
Author: User
Tags log4j

Not much nonsense.

Project structure


Pom.xml configurations such as the following

<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><!-- The version number of each 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 Specification--><dependency><groupid>org.springframework.data</groupid>< artifactid>spring-data-jpa</artifactid><version>${spring.jpa.version}</version></ dependency><!--Spring Bean container--><dependency><groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid><version>${spring.version}</version></ dependency><!--Spring Configuration--><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 Connection--><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 Test--><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 Facets--><dependency><groupid>org.springframework</ groupid><artifactid>spring-aop</artifactid><version>${spring.version}</version>< /dependency><!--Spring Database connection--><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 Verification--><dependency><groupid>org.hibernate </groupid><artifactid>hibernate-validator</artifactid><version>5.1.3.final</version ></dependency><!--aspect-oriented extension--><dependency><groupid> org.aspectj</groupid><artifactid> aspectjweaver</artifactid><version>${aspectj.version}</version></dependency><!-- Database Connection Pool--><dependency><groupid>com.mchange</groupid><artifactid>c3p0</artifactid ><version>${c3p0.version}</version><type>jar</type><scope>compile</scope ></dependency><!--MySQL Connection--><dependency><groupid>mysql</groupid>< Artifactid>mysql-connector-java</artifactid><version>${mysql.connector.version}</version> <type>jar</type><scope>compile</scope></dependency><!--Unit Test-->< dependency><groupid>junit</groupid><artifactid>junit</artifactid><version>${ junit.version}</version><scope>test</scope></dependency><!--Shiro Security (Privilege control)-- ><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 log-->< dependency><groupid>org.slf4j</groupid><artifactid>slf4j-log4j12</artifactid>< version>1.7.2</version></dependency><!--IO Packet--><dependency><groupid>commons-io </groupid><artifactid>commons-io</artifactid><version>1.3.2</version></dependency><!--fileUpload File Upload-- ><dependency><groupid>commons-fileupload</groupid><artifactid>commons-fileupload </artifactId><version>1.2.2</version></dependency><!--Redis Cache--><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 version number--><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><!--Related file encoding--><plugin><groupid >org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId>< configuration><encoding>utf-8</encoding></configuration></plugin><!--Tomcat version number-- ><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 configurations such as the following

<?

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/bean S http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.spring Framework.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 H Ttp://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframework.org/sche Ma/cache http://www.springframework.org/schema/cache/spring-cache-3.0.xsd HTTP://CX F.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/da TA/JPA http://www.springframework.org/schema/data/jpa/spring-jpa.xsd "><!--Configurator: Janehuang--><!-- Initiates a component scan, excludes the @controller component, which is scanned by the SPRINGMVC profile--><context:component-scan base-package= "Com.kugou" >< Context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/></context :component-scan><!--Properties File location--><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><!--Data source--><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 Entity Manager-- ><bean id= "JdbcTemplate" class= "org.springframework.jdbc.core.JdbcTemplate" ><property name= " DataSource "><ref bean=" DataSource "/></property></bean><!--JPA entity Management Factory configuration--><bean id= "Entitymanagerfactory" class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >< Property Name= "DataSource" ref= "DataSource"/><property name= "Jpavendoradapter" ref= " Hibernatejpavendoradapter "/><property name=" Packagestoscan "ref=" Packagestoscan "/><!--the entity class package to be scanned, No longer need to persistence.xml--><property na.Me= "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><!-- Specifies the adapter that implements JPA--><bean id= "Hibernatejpavendoradapter" class= " Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter "><property name=" Databaseplatform "value=" Org.hibernate.dialect.MySQLDialect "/></bean><!--Important configuration: Enable scanning and proactively create proxy functions--><jpa:repositories Base-package= "Com.kugou" transaction-manager-ref= "TransactionManager" entity-manager-factory-ref= " Entitymanagerfactory "/><!--entity mapping--><bean id=" Packagestoscan "class=" Org.springframework.beans.factory.config.ListFactoryBean "><property name=" SourceList "><list>< value>com.kugou.security.entity</value></list></property></bean><!--Configuring the Redis thread pool-- ><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><!--connect 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 "/><!--required bean-->< for Redis calls Bean id= "redistemplate" class= "org.springframework.data.redis.core.StringRedisTemplate" ><property name= " ConnectionFactory "ref=" ConnectionFactory "/></bean>    <!--transaction configuration-->    <bean id= "TransactionManager" class= "Org.springframework.orm.jpa.JpaTransactionManager" >       &NBSP;&LT;property name= "Entitymanagerfactory" ref= "entitymanagerfactory"/>    </bean><!-- Annotated transaction--><tx:annotation-driven transaction-manager= "TransactionManager"/><!--Spring File Upload-->< Bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >< Property Name= "Defaultencoding" value= "Utf-8" ></property><property name= "maxuploadsize" value= "204800" /></bean><!--Timer Annotations support--><task:annotation-driven/><!--thread pool configuration--><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=" "/><property name=" Rejectedexecutionhandler ">< Bean class= "Java.util.concurrent.threadpoolexecutor$abortpolicy"/></property></bean></beans>


JPA Section Code

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 related Code

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 S Tring 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;}}

Other I do not write, and my previous article a lot of repeated code and configuration

SPRINGMVC+SPRING+JPA (Hibernate) +redis+maven configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.