SSM 整合
1、导入 Jar 包
如果是 WEB 工程就导入相关 Jar 包。如果是 Maven 工程就引入相关依赖。
2、写配置
-
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"> <!--########################################项目整合spring############################--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--##################################项目整合springMVC###############################--> <!--前端控制器--> <servlet> <servlet-name>StudentHandler</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-Controller.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StudentHandler</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--字符编码过滤器--> <filter> <filter-name>characterEncodingFilter</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>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--支持REST风格的过滤器--> <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
-
springmvc-config.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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"/> <property name="suffix" value=".jsp"/> </bean> <!--################################springmvc基础配置(2个标配)#####################--> <!--扫描动态资源--> <mvc:annotation-driven></mvc:annotation-driven> <!--扫描静态资源,将SpringMVC不能处理的请求交给tomcat--> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!-- 注解扫描,SpringMVC的配置文件只扫描Controller (use-default-filters="false":禁用默认的规则)--> <context:component-scan base-package="com.cpl.controller" use-default-filters="false"> <!-- 只扫描控制器 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--使用新版本注解,让mvc支持返回json,默认返回字符串--> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> <value>application/xml;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--文件上传解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <!--设置上传文件大小的最大限制 B--> <property name="maxUploadSize" value="10485760"></property> <!-- 上传文件的解析 --> <property name="resolveLazily" value="true"/> </bean> <!--配置SpringMvc拦截器--> <mvc:interceptors> <mvc:interceptor> <!--/**拦截所有文件夹及文件夹里的子文件夹; /*拦截所有文件夹,不含子文件夹; /是web项目的根目录 --> <mvc:mapping path="/**/"/><!--拦截哪些请求--> <mvc:exclude-mapping path="/login"/> <!--哪些不拦截--> <mvc:exclude-mapping path="/static/**"></mvc:exclude-mapping> <bean class="com.cpl.Utils.MyInterceptor"></bean><!--拦截器所在位置--> </mvc:interceptor> </mvc:interceptors> </beans>
-
spring.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" 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"> <!--Spring主要扫描 dao 层 、 service 层 ,排除掉 Controller 层--> <context:component-scan base-package="com.cpl"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter> </context:component-scan> <!--1.加载数据库信息--> <context:property-placeholder location="classpath:DB.properties"/> <!--2.配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--##################################配置MyBatis操作数据库#####################--> <!--3.配置SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--指定mapper映射文件位置--> <property name="mapperLocations" value="classpath:com/cpl/mapper/*.xml"/> <!--指定Mybatis全局配置文件--> <property name="configLocation" value="classpath:mybatis_conf.xml"/> </bean> <!-- 4.配置mapper扫描器,把每一个mapper接口的实现加入到ioc容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定mapper接口所在包 --> <property name="basePackage" value="com.cpl.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!--第四部 也可这么写 : <mybatis-spring:scan base-package="com.cpl.mapper"></mybatis-spring:scan> --> </beans>
逆向工程
逆向工程配置:generator.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">
<generatorConfiguration>
<!--
targetRuntime="MyBatis3":生成复杂的逆向工程
targetRuntime="MyBatis3Simple" :生成简单的逆向工程
-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<commentGenerator>
<!--
suppressAllComments属性值:
true:自动生成实体类、SQL映射文件时没有注释
true:自动生成实体类、SQL映射文件,并附有注释
-->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/2019Home"
userId="root" password="2105251354">
</jdbcConnection>
<!--
forceBigDecimals属性值:
true:把数据表中的DECIMAL和NUMERIC类型,
解析为JAVA代码中的java.math.BigDecimal类型
false(默认):把数据表中的DECIMAL和NUMERIC类型,
解析为解析为JAVA代码中的Integer类型
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- javaModelGenerator:生成POJO
targetProject属性值:实体类的生成位置
targetPackage属性值:生成实体类所在包的路径
-->
<javaModelGenerator targetPackage="org.lanqiao.entity"
targetProject=".\src">
<!-- trimStrings属性值:
true:对数据库的查询结果进行trim操作
false(默认):不进行trim操作
-->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sqlMapGenerator:生成sql映射文件
targetProject属性值:SQL映射文件的生成位置
targetPackage属性值:SQL映射文件所在包的路径
-->
<sqlMapGenerator targetPackage="org.lanqiao.mapper"
targetProject=".\src">
</sqlMapGenerator>
<!-- 生成动态代理的接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="org.lanqiao.mapper" targetProject=".\src">
</javaClientGenerator>
<!-- table:指定生成数据表
tableName:表名
domainObjectName:这个表对应的对象名
-->
<table tableName="Student" domainObjectName="Student"> </table>
<table tableName="studentCard"> </table>
<table tableName="Class"> </table>
</context>
</generatorConfiguration>
测试:
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);