基于SSM的Maven项目(Redis和Mysql)配置文件整合
Posted 清泉***
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于SSM的Maven项目(Redis和Mysql)配置文件整合相关的知识,希望对你有一定的参考价值。
src/main/resources
1. db 文件夹
1.1 db.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.175.102:3306/zy_video2?userSSL=false jdbc.user=ZY103 [email protected] redis.host=192.168.175.102 redis.port=6379 redis.pass=123456 redis.maxTotal=30 redis.maxIdle=5
1.2 db.sql
2. mybatis 文件夹
2.1 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 开启下划线转驼峰 --> <settings> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> </configuration>
2.2 dao对应生成的Mapper文件
3. spring 文件夹
3.1 spring-dao.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 扫描并识别Spring 相关的注解: @service @Componnent @Repository...--> <context:component-scan base-package="com.zhiyou100" /> <!-- 加载 db.properties--> <context:property-placeholder location="classpath:db/db.properties"/> <!-- c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置sqlSessionFactory的相关配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 设置Mybatis配置文件的路径 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> <!-- 开启起别名,默认为首字母小写 --> <property name="typeAliasesPackage" value="com.zhiyou100.model"></property> <!-- 设置mapper 文件路径 --> <property name="mapperLocations" value="classpath:mybatis/*Mapper.xml"></property> </bean> <!-- 把dao 接口的实现类注入到Spring容器中,用过名字或者类型获取对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 把dao包下的Interface 的实现类注入到spring容器中 --> <property name="basePackage" value="com.zhiyou100.dao"></property> </bean> </beans>
3.2 spring-redis.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 读取配置文件的内容 --> <context:property-placeholder location="classpath:db/db.properties"/> <!-- 配置jpc JedisPoolConfig , 用来合理预估配置--> <bean id="jpc" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}"></property> <property name="maxIdle" value="${redis.maxIdle}"></property> </bean> <!-- 配置jcf , 链接工厂 , 用来生成redis链接对象 JedisConnectionFactory--> <bean id="jcf" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}"></property> <property name="port" value="${redis.port}"></property> <property name="password" value="${redis.pass}"></property> <property name="poolConfig" ref="jpc"></property> </bean> <!-- RedisTemplate 对象来进行对redis的各种操作 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jcf"></property> <!-- 设置编码格式为UTF-8 --> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> </bean> </beans>
3.3 sprinf-service.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 针对Service层的Spring配置 --> <!-- 1.包扫描 @service --> <context:component-scan base-package="com.zhiyou100.service"></context:component-scan> <!-- 2. 为方法提供事务支持 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据库连接池,事务是数据库中的 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 使用最简单的声明式事务操作,通过注解来完成 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
3.4 spring-web.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 包扫描 --> <context:component-scan base-package="com.zhiyou100.controller" /> <!-- 开启注解 --> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 返回的jsp文件的路径前缀 --> <!-- 放在WEB-INF下的.jsp别人不能通过url访问,可以提高数据的安全性 --> <property name="prefix" value="/WEB-INF/view/"></property> <!-- 返回的jsp文件的路径后缀 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 使用默认的handler ,支持静态的访问 --> <mvc:default-servlet-handler/> </beans>
4.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>VideoAdmin</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 对Spring的配置 --> <!-- 在服务器启动的时候去加载和解析Spring配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring的文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </context-param> <!-- 配置字符编码 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
5. 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>com.zhiyou100</groupId> <artifactId>RedisTest3</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- mybatis 的jar --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- mysql 的驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> <!-- mybatis 和Spring 集成的jar --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- 自动导入依赖的 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!-- spring事务 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- jackson 的jar包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.1</version> </dependency> <!-- redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.7.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webResource> <!-- 不打包 test 目录下的文件 --> <resource> <directory>src/test</directory> <excludes> <exclude>*</exclude> </excludes> </resource> </webResource> </configuration> </plugin> </plugins> </build> </project>
以上是关于基于SSM的Maven项目(Redis和Mysql)配置文件整合的主要内容,如果未能解决你的问题,请参考以下文章
Java项目:仿小米商城系统(前后端分离+java+vue+Springboot+ssm+mysql+maven+redis)
Java项目:个人博客系统(前后端分离+java+vue+Springboot+ssm+mysql+maven+redis)
Java项目:个人博客系统(前后端分离+java+vue+Springboot+ssm+mysql+maven+redis)
Java项目:成绩管理系统(前后端分离+java+vue+Springboot+ssm+mysql+maven+redis)