百木园-与人分享,
就是让自己快乐。

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

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>

 


来源:https://www.cnblogs.com/isDaHua/p/17109009.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » 学习笔记——尚好房项目(配置ssm环境、测试ssm环境)

相关推荐

  • 暂无文章