基于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>
时间: 2024-10-07 15:33:36

基于SSM的Maven项目(Redis和Mysql)配置文件整合的相关文章

eclipse中maven项目交付svn忽略配置文件

本文出处:http://blog.csdn.net/chaijunkun/article/details/34805385,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建议在原始出处查看此文. eclipse与maven插件的结合为我们快速搭建开发环境提供了便捷条件,然而maven编译出来的class文件.配置文件和打包文件实际上都不需要进行版本控制,团队中每个人的开发环境可能不太一样,将.settings目录和.project等文件同步到svn甚至还会引起冲突和不便

maven项目使用mybatis+mysql

1.添加依赖,在pom.xml中添加 <!--mybatis核心包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!--MySQL的JDBC驱动包--> <dependency> &l

maven项目创建4 dao层整合

项目配置文件要放在打包成war包的web项目中,注配置文件在去copy逆向工程 创建文件步骤 1    SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-

idea搭建SSM的maven项目(tomcat容器)

一.创建maven的web项目 (1)选择项目的骨架 (2)写项目的坐标 (3)maven的设置 设置maven的本地仓库,以及配置文件的位置,同时点击+号,填入archetypeCatalog和internal,让maven读取本地配置即可. 然后点击next (4)创建完成 点击finish 二.添加目录并导入依赖 (一)添加java目录,main目录下创建java目录,并且将其标记为Sources Root (2) 在main文件夹下创建resources目录,并将其标记为Resource

maven 项目 spring mvc + jdbc 配置文件

<?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:mvc="http://www.springframework.org/schema/

Maven项目自动生成mybaties配置文件

1.把mysql-connector-java-5.1.31.jar包放到C盘的mysqljar文件夹下 2.在generatorConfig.xml文件中配置实体.dao.service等包 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Confi

如何解决IntelliJ在打包Maven项目时不打包配置文件

在pom文件中加上你的配置文件夹目录: <build> <resources> <resource> <directory>src/res</directory> </resource> </resources></build>

解决maven项目Invalid bound statement (not found)的方法

用IDEA 做的ssm 的maven项目,登陆时出现上图问题. 原因是它读取不到DevUserMapper.xml文件和取它xml文件,后面查询在编译好的文件中,xml文件并没有引入进来,这就是导致出现这个问题的原因. 解决方法: 在pom.xml文件中的<build>标签中添加以下代码: <resources> <resource> <directory>src/main/java</directory> </resource> &

3.手动搭建Maven项目

# 0.写在前面 首先回忆一下我们之前都做了什么:     1.成功在本机安装Maven软件,并配置了环境变量     2.成功配置了Maven的配置文件(conf/settings.xml文件),保证Maven能够通过我们指定的国内镜像下载jar文件,并将jar文件都保存在我们指定的文件夹当中        并且在创建Maven项目的时候默认使用JDK 1.8版本进行创建     3.成功配置了Eclispe下的Maven开发环境,并且保证能够快速创建一个基于Eclispe的Maven项目