SSM 三大框架中的各种配置文件以及必要文件的模板
第一个框架mybatis的配置文件 默认名:mybatis-config.xml
放在\src\main\resources
路径下
<?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>
<properties resource="jdbc.properties"/>
<!--JDBC环境配置、选中默认环境-->
<environments default="MySqlDB">
<!--MySql数据库环境配置-->
<environment id="MySqlDB">
<!--事务管理-->
<transactionManager type="JDBC"/>
<!--连接池-->
<dataSource type="org.apache.ibatis.datasource.pooled.PooledDataSourceFactory">
<property name="driver" value="${jdbc.driver}"/>
<!-- &转义& -->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--mapper映射文件的路径-->
<mappers>
<mapper resource=""/>
</mappers>
</configuration>
mybatis配置小结:
Mybatis的数据库映射文件模板 默认名: mapper.xml
初始阶段和放在dao层下的一个mapper包里 和Dao是一一对应的
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace=""><!--映射接口的路径-->
</mapper>
第二大框架spring的配置文件 spring 整合mybatis的配置文件
默认命名:spring-mybatis.xml 放在\src\main\resources
路径下
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="jdbc.properties"/>
<!-- 定义数据源-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 定义session工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.ws.pojo"/>
<property name="mapperLocations" value="classpath:com/ws/dao/mapper/*.xml"/>
<property name="dataSource" ref="druidDataSource"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--扫描接口 spring自动给接口生成代理实现类-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ws.dao"/>
</bean>
<!--注解扫描-->
<!-- <context:component-scan base-package="com.ws"/>-->
<!--只扫描service包注解-->
<context:component-scan base-package="com.ws.service">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!--引入一个事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="druidDataSource"/>
</bean>
<!--配置各个方法的事务隔离级别-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="delete*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 将配置好的事务用aop切面织入service包中的所有方法中,注意*和com中间有空格-->
<aop:config>
<aop:pointcut id="point" expression="execution(* com.ws.service..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>
</aop:config>
</beans>
jdbc.properties:(默认放在\src\main\resources
路径下)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://地址:端口号/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
log4j :(默认放在\src\main\resources
路径下)
注意:要用log4j要导入log4j的依赖,并且必须写log4j配置文件,文件名也必须是log4j
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
springMVC+Spring+Mybatis:
首先需要在webapp下的WEB-INF下 写web.xml和
springMVC-servlet.xml
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!--指定listener读取文件 父的加载子的配置文件-->
<!-- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>-->
<!--配置DispatchServlet 将所有的servlet交给springMVC,加载springMVC-servlet的配置文件-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--默认加载springMVC-servlet.xml的配置文件 和web.xml一同在WEB-INF下面 指定的话就用下面的init-param-->
<!-- <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:</param-value>
</init-param>-->
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--解决乱码-->
<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>
<url-pattern>*.js</url-pattern>
</filter-mapping>-->
<!--主页面 欢迎页面-->
<!-- <welcome-file-list>
<welcome-file>hello</welcome-file>
</welcome-file-list>-->
</web-app>
然后是springMVC-servlet.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">
<!--告诉springMVC 那些包存在被注解的类-->
<context:component-scan base-package="com.ws.controller"/>
<!--注册注解开发驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置前后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/WEB-INF/page/"/>
</bean>
</beans>
最后是在\src\main\resources路径下写出spring-mybatis.xml的配置文件
然后三大框架的整合ssm项目就成了