• ssm项目布置流程


    项目目录

    1.编写db.properties

    1. jdbc.driver=com.mysql.cj.jdbc.Driver
    2. jdbc.url=jdbc:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    3. jdbc.username=root
    4. jdbc.password=root

    2.编写mybatis-spring.xml

    1.配置SqlSessionFactory  4.    5.

    1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    2. <property name="dataSource" ref="dataSource">property>
    3. <property name="configLocation" value="classpath:mybatis.xml">property>
    4. <property name="mapperLocations" value="classpath:mapper/*.xml">property>
    5. bean>

    2.读取db.properties文件

    1. <context:property-placeholder location="classpath:db.properties"/>

    3.连接数据库

    1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    2. <property name="driverClassName" value="${jdbc.driver}">property>
    3. <property name="url" value="${jdbc.url}">property>
    4. <property name="username" value="${jdbc.username}">property>
    5. <property name="password" value="${jdbc.password}">property>
    6. bean>

    6.扫描service包

    1. <context:component-scan base-package="com.chen.service">context:component-scan>

    7.配置数据源事务管理器

    1. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    2. <property name="dataSource" ref="dataSource">property>

    8.配置事务管理管理的方法

    9.配置Aop切入点

    3.编写mybatis.xml

    1. "1.0" encoding="UTF-8" ?>
    2. configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <settings>
    7. <setting name="logImpl" value="STDOUT_LOGGING"/>
    8. settings>
    9. <typeAliases>
    10. <package name="com.chen.entity"/>
    11. typeAliases>
    12. configuration>

     4.编写springmvc.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. https://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/context
    9. https://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.springframework.org/schema/mvc
    11. https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    13. <property name="prefix" value="/WEB-INF/jsp/">property>
    14. <property name="suffix" value=".jsp">property>
    15. bean>
    16. <context:component-scan base-package="com.chen.controller"/>
    17. <mvc:annotation-driven conversion-service="conversionService"/>
    18. <mvc:view-controller path="/index.html" view-name="login"/>
    19. <mvc:view-controller path="/login.html" view-name="login"/>
    20. <mvc:view-controller path="/" view-name="login"/>
    21. <mvc:view-controller path="/main.html" view-name="main"/>
    22. <mvc:resources mapping="/asserts/**" location="/asserts/">mvc:resources>
    23. <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    24. <property name="converters">
    25. <set>
    26. <bean class="com.chen.converter.DateConverter" />
    27. set>
    28. property>
    29. bean>
    30. beans>

    5.编写web.xml

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <context-param>
    7. <param-name>contextConfigLocationparam-name>
    8. <param-value>classpath:mybatis-spring.xmlparam-value>
    9. context-param>
    10. <listener>
    11. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    12. listener>
    13. <servlet>
    14. <servlet-name>dispatcherServletservlet-name>
    15. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    16. <init-param>
    17. <param-name>contextConfigLocationparam-name>
    18. <param-value>classpath:springmvc.xmlparam-value>
    19. init-param>
    20. <load-on-startup>1load-on-startup>
    21. servlet>
    22. <servlet-mapping>
    23. <servlet-name>dispatcherServletservlet-name>
    24. <url-pattern>/url-pattern>
    25. servlet-mapping>
    26. <filter>
    27. <filter-name>encodingfilter-name>
    28. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    29. <init-param>
    30. <param-name>encodingparam-name>
    31. <param-value>UTF-8param-value>
    32. init-param>
    33. filter>
    34. <filter-mapping>
    35. <filter-name>encodingfilter-name>
    36. <url-pattern>/*url-pattern>
    37. filter-mapping>
    38. web-app>


    6.编写实体类和mapper.xml

    注:一个实体类对应一张数据库表,也对应一个usermapper接口和usermapper.xml文件,

    一个mapper接口对应一个mapper实现类

    user

    1. package com.chen.entity;
    2. import lombok.Data;
    3. import javax.swing.*;
    4. @Data
    5. public class User {
    6. private Integer id;
    7. private String userName;
    8. private String pwd;
    9. }

    usermapper接口

    1. package com.chen.mapper;
    2. import com.chen.entity.User;
    3. import org.apache.ibatis.annotations.Param;
    4. public interface UserMapper {
    5. User queryByUserNameAndPwd(@Param("userName") String userName, @Param("pwd") String pwd);
    6. }

    UserServiceImpl.java

    1. package com.chen.service.Impl;
    2. import com.chen.entity.User;
    3. import com.chen.mapper.UserMapper;
    4. import com.chen.service.UserService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. @Service
    8. public class UserServiceImpl implements UserService {
    9. @Autowired
    10. private UserMapper userMapper;
    11. @Override
    12. public boolean login(String userName, String pwd) {
    13. User user = this.userMapper.queryByUserNameAndPwd(userName, pwd);
    14. return user != null;
    15. }
    16. }

    usermapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.chen.mapper.UserMapper">
    6. <select id="queryByUserNameAndPwd" resultType="user">
    7. SELECT * FROM t_user WHERE user_name=#{userName} and pwd=#{pwd}
    8. select>
    9. mapper>

    7.编写controller层

    controller层负责service层和页面的跳转

    UserController对应UserService接口

    UserController.java

    1. package com.chen.controller;
    2. import com.chen.entity.User;
    3. import com.chen.service.UserService;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.web.bind.annotation.PostMapping;
    7. @Controller
    8. public class UesrController {
    9. @Autowired
    10. private UserService userService;
    11. @PostMapping("/user/login")
    12. public String login(User user) {
    13. System.out.println(user);
    14. boolean result = this.userService.login(user.getUserName(), user.getPwd());
    15. if (result) {
    16. return "redirect:/main.html";
    17. } else {
    18. return "redirect:/index.html";
    19. }
    20. }
    21. }

    8.日期转换器,转换页面传回来的时间字符串

    1. package com.chen.converter;
    2. import org.springframework.core.convert.converter.Converter;
    3. import java.text.ParseException;
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6. public class DateConverter implements Converter {
    7. @Override
    8. public Date convert(String source) {
    9. try {
    10. // 把字符串转换为日期类型
    11. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    12. Date date = simpleDateFormat.parse(source);
    13. return date;
    14. } catch (ParseException e) {
    15. e.printStackTrace();
    16. }
    17. // 如果转换异常则返回空
    18. return null;
    19. }
    20. }

  • 相关阅读:
    jsp 九大内置对象和四大域对象
    【DevOps】OpenVPN 实现分流的几种方法和实战
    Android 12修改无源码 app 的字体大小和 dpi
    踩坑笔记: 基于 rust-analyzer 在 vscode 中进行 rust 开发配置问题
    linux统计日志文件中IP出现的次数,显示次数最多的前十,grep,cat,sort,uniq,head,cut,awk
    【C++】自旋锁
    AWS VPC
    【前端学习笔记】cookie和session的区别
    SpringBoot AnnotationUtils工具类的使用
    CTFHub | Cookie注入
  • 原文地址:https://blog.csdn.net/qq_51741292/article/details/127045705