首页 > 其他分享 >学习笔记——尚好房项目(配置ssm环境、测试ssm环境)

学习笔记——尚好房项目(配置ssm环境、测试ssm环境)

时间:2023-02-10 18:34:12浏览次数:43  
标签:web parent src admin 环境 ssm 尚好 shf main

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>

 

标签:web,parent,src,admin,环境,ssm,尚好,shf,main
From: https://www.cnblogs.com/isDaHua/p/17109009.html

相关文章

  • 一次生产环境CPU占用高的排查
    1.项目背景甲方是保密级别非常高的政府部门。所以我们全程拿不到任何测试数据,只能是自己模拟数据进行测试。项目部署的时候,公司派了一人到甲方现场,在甲方客户全程监督下......
  • mongo数据库docker环境删除数据释放空间
    整理mongo环境存入数据1、先删除部分数据db.xxx.remove({ createBy: ""})2、查询数量db.xxx.find({}).count()3、进入容器dockerexec-itid bash4、进入mongo......
  • 学习笔记——尚好房项目(项目介绍、环境搭建、配置依赖关系)
    2023-02-10一、项目介绍1、介绍 尚好房是一个二手房管理服务平台,开放优质资源和线上能力,聚合线上线下二手房产资源,打造一个全方位二手房服务生态市场,为消费者提供优质......
  • 快速了解什么是可信执行环境TEE技术
    1.背景TEE全名为可信执行环境(TrustedExecutionEnvironment)是计算平台上由软硬件方法构建的一个安全区域,可保证在安全区域内加载的代码和数据在机密性和完整性方面得到保护......
  • vivo小游戏开发环境搭建
    基本信息官网,要chrome才能打开http://minigame.vivo.com.cn/documents/#/lesson/base/environmentvivoapi广告文档http://minigame.vivo.com.cn/documents/#/api/......
  • Kata环境搭建1 —— kata+containerd
    Kata+containerdenvironmentubuntu22.04(刚开始用20.04,编译的时候musl出现一些问题)BuildandinstallKataContainersinstallneccesarytools需要注意,先gi......
  • java 环境配置
    linuxopenjdkdownload_url:https://www.openlogic.com/openjdk-downloads源码包安装java11tar-zxvfjdk-11.0.14_linux-x64_bin.tar.gz-C/usr/local/cat>>/......
  • 第二十二天python3 classmethod、staticmethod、property装饰器学习笔记
    classmethod1、在类定义中,使用@classmethod装饰器修饰的方法;2、必须至少有一个参数,且第一个参数留给了cls,cls指代调用者即类对象自身;3、cls这个标识符可以是任意合法名......
  • CFS三层靶机-内网环境渗透
    <1>靶场介绍及环境配置三个主机的网络环境拓扑图,攻击机的网段在192.168.236.0/24,三台靶机的IP地址分别如图:上面的Target1、2、3分别对应CentOS7、Ubuntu、Windows7三......
  • Windows 环境下安装Snort+MySQL+ACID
       在Windows系统中安装软件,通常是一通Next...Finish,相比Linux要容易,所以很多同学在准备IDS实验环境时自然会想到采用Windows系统,本文对阅读者的网络基础知识以及对实......