<web-app> <display-name>Archetype Created Web Application</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring beans 配置文件所在目录 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- spring mvc 配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Encoding --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </webapp>
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="locations"> <list> <value>classpath:settings.properties</value> </list> </property> </bean> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${db.driverClassName}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="initialSize" value="3" /> <property name="minIdle" value="3" /> <property name="maxActive" value="20" /> <property name="maxWait" value="60000" /> <!--<property name="filters" value="stat,wall" />--> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.demo.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- tx --> <!--<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">--> <!--<property name="dataSource" ref="dataSource" />--> <!--</bean>--> <!--<tx:annotation-driven transaction-manager="transactionManager" />--> <!-- druid --> <!--<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">--> <!--<property name="slowSqlMillis" value="3000" />--> <!--<property name="logSlowSql" value="true" />--> <!--<property name="mergeSql" value="true" />--> <!--</bean>--> <!--<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">--> <!--<property name="dbType" value="mysql" />--> <!--</bean>--> </beans>
<?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:annotation-config /> <!-- 启动注解驱动的spring mvc 功能 --> <mvc:annotation-driven/> <context:component-scan base-package="com.mmall.controller"/> <context:component-scan base-package="com.mmall.service"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
在WEB-INF下创建views文件夹
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="safeRowBoundsEnabled" value="true"/> <setting name="cacheEnabled" value="false" /> <setting name="useGeneratedKeys" value="true" /> </settings> </configuration>
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <!--<appender name="permission" class="ch.qos.logback.core.rolling.RollingFileAppender">--> <!--<file>${catalina.home}/logs/permission.log</file>--> <!--<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">--> <!--<FileNamePattern>${catalina.home}/logs/permission.%d{yyyy-MM-dd}.log.gz</FileNamePattern>--> <!--</rollingPolicy>--> <!--<layout class="ch.qos.logback.classic.PatternLayout">--> <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>--> <!--</layout>--> <!--</appender>--> <!----> <!--<logger name="xxx" level="INFO">--> <!--<appender-ref ref="permission"/>--> <!--</logger>--> <!-- TRACE < DEBUG < INFO < WARN < ERROR --> <root level="INFO"> <appender-ref ref="STDOUT" /> </root> </configuration>
验证:
@Controller @RequestMapping("/test") @Slf4j public class TestController { @RequestMapping("/hello") @ResponseBody public String hello(){ log.info("hello"); return "hello, permission"; } }
启动项目,发现mytis木有相关文件
<?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="com.mmall.dao.TestDao"> </mapper>
启动,OK
原文:https://www.cnblogs.com/yintingting/p/8893775.html