项目目录:
接下来看下ssm项目的配置:
mybatis-config.xml:
设置mybatis,数据库等信息在spring配置文件设置
<?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>
<!--使用jdbc的getGeneratedKeys获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!--使用列别名替换列名,默认为true-->
<setting name="useColumnLabel" value="true"/>
<!--开启驼峰命名转换 ,xx_y之间可变为xxY-->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
spring-dao:
<?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">
<!--1.配置整合mybatis-->
<!--1.配置数据库相关参数jdbc.properties属性-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2.配置数据库连接池-->
<bean id ="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}" ></property>
<property name="maxPoolSize" value="30"></property>
<property name="minPoolSize" value="10"></property>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"></property>
<!--获取链接超时时间-->
<property name="checkoutTimeout" value="10000" />
<!--获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--3.配置sqlSessionFactory对象(通过配置生产所需要的连接池)-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource" />
<!--扫描sql配置文件:mapper需要的xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<!--扫描entity包,使用别名就是在xml文件中不用引用类的限定名,只用简单类名(类的第一个字母小写)-->
<property name="typeAliasesPackage" value="storepro.entity" />
<!--配置mybatis的全局配置文件:mybatis-config.xml-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--4.配置扫描Dao-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!--给出需要扫描的DAO接口包-->
<property name="basePackage" value="storepro.dao" />
</bean>
</beans>
<!--扫描sql配置文件:mapper需要的xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
我们扫描后,在xml文件中将namespace和dao层接口名相等,id为dao层的方法名,完成了后通过mapper代理方式我们不需要直接创建实现类,而且不用将mapper和dao的接口放在一个文件夹且同名
spring-service:对service的配置
主要是对事务的配置和注解的扫描
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--扫描service包下所有使用注解的类型-->
<context:component-scan base-package="storepro.service" />
<!--配置事务管理器-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置基于注解的声明式事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
spring-web:对web层的配置,就是springmvc的配置。
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--配置SpringMVC-->
<!--1.开启SpringMVC注解模式(各层可以直接使用controller等注解使用)-->
<mvc:annotation-driven/>
<!--2.静态资源默认servlet处理-->
<!--①.加入对静态资源的处理:js,gif,png-->
<!--②.允许使用'/'做整体映射-->
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:default-servlet-handler/>
<!--3.定义视图解析器-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/html/" />
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".html" />
</bean>
<!--4.扫描web相关的bean,注解方式-->
<context:component-scan base-package="storepro.web"/>
</beans>
web.xml:
对前端控制器、spring文件读取、配置映射方式进行配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<!--配置前端控制器-->
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--整合spring配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<!--配置映射方式,就是请求映射到cntroller-->
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<!--默认匹配所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>