文章目录
续Day1
7.配置文件(记得修改依赖包名)
1)在maven-dao中添加applicationContext-dao.xml,database.properties,mybatis.config.xml三个配置文件
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描注解所在的包 -->
<context:component-scan base-package="com.manong.dao"/>
<!-- <context:property-placeholder location="classpath:database.properties"/>-->
<!-- 引入database.properties属性文件 -->
<!-- <context:property-placeholder location="classpath:database.properties"/>或者-->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location">
<value>classpath:database.properties</value>
</property>
</bean>
<!-- 配置数据源dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--驱动-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!--连接字符串-->
<property name="url" value="${jdbc.url}"/>
<!--数据库用户名-->
<property name="username" value="${jdbc.user}"/>
<!--数据库密码-->
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id ="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载MyBatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 加载SQL映射文件 -->
<property name="mapperLocations" value="classpath:mapper/**/*.xml"/>
</bean>
<!-- 加载mapper所在的包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描mapper接口所在的位置 -->
<property name="basePackage" value="com.manong.dao"/>
</bean>
</beans>
database.properties
#\u6570\u636E\u5E93\u9A71\u52A8
jdbc.driver = com.mysql.cj.jdbc.Driver
#\u6570\u636E\u5E93\u8FDE\u63A5url
jdbc.url = jdbc:mysql://localhost:3306/db_hotel_JT11?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
#\u6570\u636E\u5E93\u7528\u6237\u540D
jdbc.user = root
#\u6570\u636E\u5E93\u5BC6\u7801
jdbc.password = root
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="logImpl" value="LOG4J"/>
<!-- 自动映射 -->
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
<typeAliases>
<!-- 设置别名 -->
<package name="com.manong.entity"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 分页合理化参数,默认是false.=true时,pageNum<=0时查询第一页,pageNum>pages时,查询最后一页;-->
<!-- 默认为false是,直接根据参数进行查询-->
<property name="resonable" value="true"/>
</plugin>
</plugins>
</configuration>
2)给maven-service添加applicationContext-service.xml文件
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描注解所在的包 ,管理service-->
<context:component-scan base-package="com.manong.service"/>
<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3)给maven-web添加springmvc.xml和log4j.properties文件
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 加载控制器所在的包 -->
<context:component-scan base-package="com.manong.controller"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图前缀(页面在哪个目录下) -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 视图后缀(页面的后缀名是什么) -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 加载静态资源 -->
<mvc:resources mapping="/statics/**" location="/statics/"/>
<!-- 开启注解支持 -->
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 配置编码字符集 -->
<!-- 配置响应编码字符集 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 配置Jackson消息转换器 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!-- 格式化日期 -->
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd" />
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置文件解析器对象,要求id名称必须是multipartResolver -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件上传限制大小为10M -->
<property name="maxUploadSize" value="10485760"/>
</bean>
</beans>
log4j.properties
# rootLogger\u662F\u6240\u6709\u65E5\u5FD7\u7684\u6839\u65E5\u5FD7,\u4FEE\u6539\u8BE5\u65E5\u5FD7\u5C5E\u6027\u5C06\u5BF9\u6240\u6709\u65E5\u5FD7\u8D77\u4F5C\u7528
# \u4E0B\u9762\u7684\u5C5E\u6027\u914D\u7F6E\u4E2D,\u6240\u6709\u65E5\u5FD7\u7684\u8F93\u51FA\u7EA7\u522B\u662Finfo,\u8F93\u51FA\u6E90\u662Fcon
log4j.rootLogger=debug,con
# \u5B9A\u4E49\u8F93\u51FA\u6E90\u7684\u8F93\u51FA\u4F4D\u7F6E\u662F\u63A7\u5236\u53F0
log4j.appender.con=org.apache.log4j.ConsoleAppender
# \u5B9A\u4E49\u8F93\u51FA\u65E5\u5FD7\u7684\u5E03\u5C40\u91C7\u7528\u7684\u7C7B
log4j.appender.con.layout=org.apache.log4j.PatternLayout
# \u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u5E03\u5C40
log4j.appender.con.layout.ConversionPattern=%d{MM-dd HH:mm:ss}[%p]%c%n -%m%n
4)更新maven-web—>webapp---->WEB-INF—>web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 设置欢迎界面 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 上下文参数配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 使用*号通配符,通配符前面的字符要一致 -->
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<!-- 加载springmvc配置文件 -->
<servlet>
<!-- servlet名称,名称自定义,名称唯一 -->
<servlet-name>springmvc</servlet-name>
<!-- 前端控制器,SpringMVC核心控制器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- web项目启动,立即加载springmvc配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- springmvc配置文件位置 -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!-- servlet名称,名称自定义 -->
<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>
<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>
</web-app>
出现resources报错情况,在webapp同级创建一个resources文件夹即可,先这样办,之后项目会进行改进。
spring-mvc里的 mvc:resources及静态资源访问
5)clean文件,clean右侧所有文件
执行maven指令时失败,‘modules.module[10]’ specifies duplicate child module
8.测试
1.打开SQLyo创建gdb_hotel_Jt11.sql(文件名更改的时候记得在maven-dao/src/main/resources/database.properties的内容也要修改),再其中创建一个user表,根据maven-entity创建的user.java建立一个user数据随便填一下。
2.测试tomcat
- 关于出现 Error creating bean with name ‘sqlSessionFactory’ defined in URL的错误
还在修bug中…