环境准备:
SSM框架整合 + zookeeper + Dubbo
1、创建父工程maven工程 shf-parent
管理子模块项目,统一依赖版本号
2、common-util:公共类模块
3、 model:实体类模块
4、 web-admin:后台管理系统
SSM环境配置步骤
为三层架构的每一层配置环境
1、dao层配置文件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="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置PageHelper分页插件-->
<plugins>
<!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
2、配置与mybatis整合 spring-dao.xml 放在resource/spring文件夹下 方便区分
<?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:spring-mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="username" value="root" />
<property name="password" value="root" />
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_house?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true" />
</bean>
<!--spring和mybatis整合的工厂bean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.atguigu.entity"></property>
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations">
<array>
<value>classpath:mapper/*.xml</value>
</array>
</property>
</bean>
<!--批量扫描接口生成代理对象-->
<spring-mybatis:scan base-package="com.atguigu.dao"></spring-mybatis:scan>
</beans>
3、配置service层 spring-service.xml(主要对service包进行包扫描,以及开启事务管理器)
<?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:spring-mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="username" value="root" />
<property name="password" value="root" />
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_house?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true" />
</bean>
<!--spring和mybatis整合的工厂bean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.atguigu.entity"></property>
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations">
<array>
<value>classpath:mapper/*.xml</value>
</array>
</property>
</bean>
<!--批量扫描接口生成代理对象-->
<spring-mybatis:scan base-package="com.atguigu.dao"></spring-mybatis:scan>
</beans>
注意:配置文件会爆红:datasource数据源的注入和mapper映射文件的配置
1.datasource注入会爆红属于正常现象,DataSource对象没有注入。DataSource对象会随着打包编译时,完成注入。
2.mapper文件下的配置文件还没有写,所以会爆红。
4、完成controller层的配置 spring-mvc.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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--包扫描-->
<context:component-scan base-package="com.atguigu.controller" />
<!-- 没有匹配上的url全部按默认方式(就是直接访问)访问,避免拦截静态资源 -->
<mvc:default-servlet-handler/>
<!-- 开启mvc注解-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 配置Fastjson支持 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--视图解析器-->
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!--配置前缀-->
<property name="prefix" value="/WEB-INF/templates/"></property>
<!--配置后缀-->
<property name="suffix" value=".html"></property>
<!--配置编码格式-->
<property name="characterEncoding" value="UTF-8"></property>
<!--设置缓存为null-->
<property name="cacheable" value="false"></property>
<!--配置模板模式,
HTML5:表示严格模式
LEGACYHTML5:表示宽松模式-->
<property name="templateMode" value="LEGACYHTML5"></property>
</bean>
<!--配置spring的视图解析器-->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<!--设置编码格式-->
<property name="characterEncoding" value="UTF-8"></property>
<!--设置模板引擎-->
<property name="templateEngine" ref="templateEngine"/>
</bean>
<!--配置模板引擎-->
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
<!--引用视图解析器-->
<property name="templateResolver" ref="templateResolver"></property>
</bean>
</beans>
5、WEB/INF下的web.xml文件的配置
配置post乱码过滤器和spring容器的加载以及springmvc前端控制器。这样SSM环境配置就已经完成了。