web.xml
web.xml配置基本的信息
- 指定spring配置文件
- 指定springmvc配置文件,
- 配置全局字符编码过滤器
rest风格filter设置,用于将POST请求转为DELETE或者PUT ```xml <!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd“ >
Archetype Created Web Application contextConfigLocation classpath:applicationContext.xml characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 forceRequestEncoding true forceResponseEncoding true hiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter characterEncodingFilter / hiddenHttpMethodFilter / org.springframework.web.context.ContextLoaderListener dispatcherServlet org.springframework.web.servlet.DispatcherServlet 1 dispatcherServlet /
<a name="NrxDL"></a>
## springMVC.xml
springMVC的配置文件,在web.xml中可以指定springMVC的配置文件路径,有两种方式指定
1. 显示通过contextConfigLocation属性指定处理
![image.png](https://cdn.nlark.com/yuque/0/2021/png/21630109/1624090602403-0c52b9b3-5a0e-4dd9-b1b3-2ebe18fa32b5.png#clientId=u7db80866-a1a4-4&from=paste&height=247&id=u29a3dc91&margin=%5Bobject%20Object%5D&name=image.png&originHeight=493&originWidth=1270&originalType=binary&ratio=2&size=65020&status=done&style=none&taskId=u3d46a704-64da-46f9-8d19-840f5d27f18&width=635)
2. 不指定contextConfigLocation属性,默认会在web.xml同级目录下找${servlet-name}-servlet.xml文件,用作springmvc的配置文件
![image.png](https://cdn.nlark.com/yuque/0/2021/png/21630109/1624090695177-a05acbe0-3740-4306-8f07-e18257f44277.png#clientId=u7db80866-a1a4-4&from=paste&height=344&id=ub4f95b0e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=688&originWidth=2009&originalType=binary&ratio=2&size=128570&status=done&style=none&taskId=uca8dd552-c959-44b1-ba3c-b3cdf1cae6e&width=1004.5)<br />对springmvc做基本的配置
1. 配置要扫描的包
1. 配置视图解析器
1. springmvc无法处理的请求交给tomcat处理
1. 配置路径自动映射等功能
```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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--spring mvc 的配置文件, 包含网站跳转逻辑的控制,配置-->
<!--配置扫描的包-->
<context:component-scan base-package="com.lyc" use-default-filters="false">
<!--只扫码@Controller配置的类-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--两个标准配置-->
<!--将springmvc不能处理的请求交给tomcat-->
<mvc:default-servlet-handler/>
<!--能支持springmvc更高级的功能,如JSR303校验、快捷的Ajax、映射动态请求-->
<mvc:annotation-driven/>
</beans>
spring.xml
配置spring
- 需要扫描的包
- 数据源配置
- 整合mybatis
事务
<?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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--spring 的配置文件-->
<!--配置要扫描的包,除了controller包归springmvc管,其它包都是spring扫描管-->
<context:component-scan base-package="com.lyc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置数据源-->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--整合mybatis-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!--指定mybatis配置文件位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--指定数据源-->
<property name="dataSource" ref="comboPooledDataSource"/>
<!--指定mapper文件的路径-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--配置mybatis的dao接口扫码路径-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有dao接口,放入IOC容器中-->
<property name="basePackage" value="com.lyc.crud.dao"/>
</bean>
<!--配置事务-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<!--要控制的数据源-->
<property name="dataSource" ref="comboPooledDataSource"/>
</bean>
<!--要AOP事务切入的方法-->
<aop:config>
<!--配置要切入的方法-->
<!--所有访问权限控制 com.lyc.crud.service.. 包下的所有包,*(..) 所有类下的任意参数方法-->
<aop:pointcut id="txPointcut" expression="execution(* com.lyc.crud.service..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!--配置事务增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--所有方法都是事务方法-->
<tx:method name="*"/>
<!--所有get开头的方法,read-only,查询方法提高效率-->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
mybatis.xml
mybatis配置可以在spring的配置文件中配置,但是为了清晰一点,有的配置还是由专门的mybatis配置文件来配置
<?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>
<typeAliases>
<package name="com.lyc.crud.dao"/>
</typeAliases>
</configuration>
mybatis generator
mybatis的生成器,通过配置,可以快速生成Java bean对象和基本的CRUD语句
引入jar包<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
配置生成规则 ```xml <?xml version=”1.0” encoding=”UTF-8”?> <!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8" />
<!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<!-- 注释生成器 -->
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 必须要有的,使用这个配置链接数据库 @TODO:是否可以扩展 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm_crud"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成javabean 对象目录 -->
<javaModelGenerator targetPackage="com.lyc.crud.entity" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaModelGenerator>
<!-- 指定SQL MAPPER目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 指定生成的DAO目录 -->
<javaClientGenerator targetPackage="com.lyc.crud.dao" type="XMLMAPPER"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- ============================================================================== -->
<!--domainObjectName="User"-->
<table tableName="tbl_emp" domainObjectName="Employ">
</table>
<table tableName="tbl_dept" domainObjectName="Department"></table>
<!-- ============================================================================= -->
</context>
新建一个类,启动该类main方法,执行下列代码
```java
public class MBG {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mybatis-generator-config.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}