2023-02-10 一、配置SSM環境 1、添加日誌文件 在“shf-parent/web-admin/src/main/resources”下創建“logback.xml” <?xml version="1.0" encoding="UTF-8"?> <configuration debug=" ...
2023-02-10
一、配置SSM環境
1、添加日誌文件
在“shf-parent/web-admin/src/main/resources”下創建“logback.xml”
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!--定義日誌文件的存儲地址 logs為當前項目的logs目錄 還可以設置為../logs --> <property name="LOG_HOME" value="logs" /> <!--控制台日誌, 控制台輸出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字元寬度,%msg:日誌消息,%n是換行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender> <!-- 日誌輸出級別 --> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> </configuration>
2、在“shf-parent/web-admin/src/main/resources”下創建“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> </configuration>
3、在“shf-parent/web-admin/src/main/resources”下創建spring文件夾,創建“db.properties”
(1)db.properties
jdbc.username=資料庫用戶名
jdbc.password=資料庫密碼
jdbc.url=jdbc:mysql://localhost:3306/資料庫名稱?serverTimezone=Asia/Shanghai
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
(2)在spring文件夾下
①創建“spring-dao.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:mybatis-spring="http://mybatis.org/schema/mybatis-spring" 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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <!-- 引入外部文件--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- 配置數據源--> <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource" destroy-method="close"> <!-- 配置連接資料庫的相關屬性--> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClassName}"></property> </bean> <!-- 整合mybatis--> <!-- 1、配置sqlsessionFactoryBean--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean"> <!-- 配置數據源屬性--> <property name="dataSource" ref="dataSource"></property> <!-- 配置mybatis的全局配置文件的路徑--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 配置給實體類包下所有的類起別名--> <property name="typeAliasesPackage" value="com.hh.entity"></property> <!-- 配置mapper映射文件的路徑--> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 2、掃描mapper工具--> <mybatis-spring:scan base-package="com.hh.dao"></mybatis-spring:scan> </beans>
②創建“spring-service.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置掃描的包--> <context:component-scan base-package="com.hh.service"></context:component-scan> <!-- 配置事務管理器--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <!-- 配置數據源屬性--> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 開啟事務註解支持--> <tx:annotation-driven></tx:annotation-driven> </beans>
③創建“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.hh.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>
(3)在“shf-parent/web-admin/src/main/resources”下創建mapper文件夾
(4)在“shf-parent/web-admin/src/main/java”下創建包“com.hh.controller”、“com.hh.dao”、“com.hh.service”
4、配置“shf-parent/web-admin/src/main/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"> <display-name>web</display-name> <!-- 配置POST請求請求亂碼問題的過濾器--> <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>forceRequestEncoding</param-name> <param-value>true</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> <!-- 配置前端控制器DispatcherServlet--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置當前WEB應用的初始化參數--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/spring-*.xml</param-value> </context-param> <!-- 配置監聽器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
二、測試SSM環境
1、添加表
2、添加實體
3、添加RoleDao
在“shf-parent/web-admin/src/main/java/com.hh.dao”下創建RoleDao介面
public interface RoleDao { List<Role> findAll(); }
4、添加RoleDao.xml映射文件
<?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.hh.dao.RoleDao"> <!-- 用於select查詢公用抽取的列 --> <sql id="columns"> select id,role_name,role_code,description,create_time,update_time,is_deleted </sql> <!-- 查詢所有--> <select id="findAll" resultType="role"> <include refid="columns"></include> from acl_role where is_deleted = 0 </select> </mapper>
5、添加service
①在“shf-parent/web-admin/src/main/java/com.hh.service”中創建“RoleService"
public interface RoleService { List<Role> findAll(); }
②在“shf-parent/web-admin/src/main/java/com.hh.service”中創建“impl.RoleServiceImpl"
@Service public class RoleServiceImpl implements RoleService { @Autowired private RoleDao roleDao; @Override public List<Role> findAll() { return roleDao.findAll(); } }
6、添加controller
在“shf-parent/web-admin/src/main/java/com.hh.controller”中創建“RoleController"
@Controller @RequestMapping("/role") public class RoleController { @Autowired private RoleService roleService; @RequestMapping public String index(Map map){ //調用RoleService中獲取所有的角色的方法 List<Role> roleList = roleService.findAll(); //將所有的角色放到request域中 map.put("list",roleList); //渲染數據的頁面 return "role/index"; } }
7、添加頁面
在“shf-parent/web-admin/src/main/webapp/WEN-INF”下創建“templates/role/index.html"
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table> <tr th:each="item,it : ${list}"> <td class="text-center" th:text="${it.count}">11</td> <td th:text="${item.roleName}">22</td> <td th:text="${item.roleCode}">33</td> <td th:text="${item.description}">33</td> <td th:text="${#dates.format(item.createTime,'yyyy-MM-dd HH:mm:ss')}" >33</td> </tr> </table> </body> </html>