基于SSM搭建系统

news2024/9/24 21:19:08

原理

  1. SSM集成 = Spring+SpringMvc+Mybatis集成

  2. 框架集成核心,如果你的项目中,用到了Spring框架,那么其他框架主要就是和Spring集成;

  3. 和Spring集成的核心思路:

    • 把当前框架的核心类,交给Spring管理(IOC)

    • 如果框架有事务,那么事务也要统一交给Spring管理(AOP)

步骤

1、根据项目业务创建数据库和表格

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `account_info`
-- ----------------------------
DROP TABLE IF EXISTS `account_info`;
CREATE TABLE `account_info` (
  `account` varchar(11) COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户账号,主键',
  `acc_name` varchar(50) COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户姓名',
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '鐢ㄦ埛瀵嗙爜锛岄粯璁ゆ墜鏈哄彿鍚?浣?',
  `acc_phone` varchar(11) COLLATE utf8mb4_general_ci NOT NULL COMMENT '手机号11位,唯一',
  `is_enable` tinyint(1) NOT NULL COMMENT '是否启用(1:启用,0:未启用)',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`account`),
  UNIQUE KEY `uk_phone` (`acc_phone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ----------------------------
-- Records of account_info
-- ----------------------------
INSERT INTO `account_info` VALUES ('YZ0001', 'admin', '92159b1631dae48aa523875174e3ea60', '13811345670', '1', '2023-08-18 11:34:28', '2023-08-18 11:34:28');
INSERT INTO `account_info` VALUES ('YZ0002', 'admin', '986fa807bbe0c721702868bae6ef8a33', '13811345679', '1', '2023-08-18 11:34:38', '2023-08-18 11:34:38');
INSERT INTO `account_info` VALUES ('YZ0003', 'admin2', '7d839f278639a38b2ba83ad67ab836a2', '13811345677', '1', '2023-08-18 14:46:05', '2023-08-18 14:46:05');
INSERT INTO `account_info` VALUES ('YZ0004', 'admin2', '35ea60fd301a3895245aff0ca4947d9e', '13811345674', '1', '2023-08-18 15:03:12', '2023-08-18 15:03:12');

2、搭建一个maven项目

3、导入坐标依赖

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.8</version>
    </dependency>

    <!--    servlet依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <!--    json 相关-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.0</version>
    </dependency>
<!--    mysql驱动-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.33</version>
    </dependency>

<!--    连接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.2.4</version>
    </dependency>

    <!--        spring整合jdbc相关坐标-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
<!--    mybatis整合spring-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.20</version>
    </dependency>

4、编写实体类

package com.cqh.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;


@Data
public class AccountInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 用户账号,主键
     */
    private String account;

    /**
     * 用户姓名
     */
    private String accName;

    /**
     * 用户密码
     */
    private String password;

    /**
     * 手机号11位,唯一
     */
    private String accPhone;

    /**
     * 是否启用(1:启用,0:未启用)
     */
    private Boolean isEnable;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;
}

5、编写mapper接口

package com.cqh.mapper;

import com.cqh.entity.AccountInfo;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface AccountInfoMapper {

    @Select("select * from account_info")
    List<AccountInfo> selectAll();
}

 

6、编写service接口和实现类  

package com.cqh.service;

import com.cqh.entity.AccountInfo;

import java.util.List;

public interface AccountInfoService  {
    List<AccountInfo> getAll();
}

 

package com.cqh.service.impl;

import com.cqh.entity.AccountInfo;
import com.cqh.mapper.AccountInfoMapper;
import com.cqh.service.AccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountInfoServiceImpl  implements AccountInfoService {

    @Autowired
    private AccountInfoMapper accountInfoMapper;

    public List<AccountInfo> getAll() {
        return accountInfoMapper.selectAll();
    }
}

 

7、编写控制器

package com.cqh.controller;

import com.cqh.entity.AccountInfo;
import com.cqh.service.AccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/accountInfo")
public class AccountInfoController {

    @Autowired
    private AccountInfoService accountInfoService;

    @GetMapping("/getAll")
    public List<AccountInfo> getAll(){
        return accountInfoService.getAll();
    }
}

 

使用注解方式搭建SSM

1、创建Web项目入口配置类替换web.xml

package com.cqh.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
//加载spring配置类
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }
//加载springmvc配置类
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
//指定spring要管哪些地址
//设置SpringMVC请求地址拦截规则
    /*
    /所有地址
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //设置post请求中文乱码过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("utf-8");
        return new Filter[]{filter};
    }
}

 

2、创建SpringMVC配置类替换springmvc.xml

package com.cqh.config;


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

//表明这是配置类
@Configuration
//开启包扫描
@ComponentScan("com.cqh.controller")
//开启mvc注解驱动
@EnableWebMvc
public class SpringMvcConfig {
}

 

3、创建SpringConfig配置类替换applicationContext.xml  

package com.cqh.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;

//表明这是配置类
@Configuration
//开启包扫描
@ComponentScan("com.cqh.service")
//开启事务平台管理器
@EnableTransactionManagement
//引入数据库配置类
@Import({MybatisConfig.class,JdbcConfig.class})

public class SpringConfig {
}

4、创建JdbcConfig配置类

 

package com.cqh.config;

import com.alibaba.druid.pool.DataSourceClosedException;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
//从resources下引入jdbc.properties
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {

    @Value("${jdbc.driver}")
    //把配置文件中的jdbc.driver对应的字符串值 附值给 被修饰前的变量
    private String driver;
    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    //数据源

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    //平台事务管理器

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        //创建数据事务管理器
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        //设置事务源
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }

}

 5、创建MybatisConfig配置类

package com.cqh.config;

import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;


public class MybatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean factoryBean= new SqlSessionFactoryBean();
        //设置数据源

        factoryBean.setDataSource(dataSource);
        //给实体类设置别名
        factoryBean.setTypeAliasesPackage("com.cqh.entity");
        //开启驼峰命名
        Configuration configuration = new Configuration();

        //下划线转驼峰
        configuration.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(configuration);

        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        //扫描mapper所在的包
        mapperScannerConfigurer.setBasePackage( "com.cqh.mapper");
        return mapperScannerConfigurer;
    }

}

 

Xml方式 

1、配置jdbc.properties

2、applicationContext.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"
       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
">

    <context:component-scan base-package="com.cqh.service"></context:component-scan>

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

    <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value= "${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sqlSessionFactory" class=" org.mybatis.spring.SqlSessionFactoryBean ">
        <property name="dataSource" ref="ds" />
        <property name="typeAliasesPackage" value="com.cqh.entity"/>
        <property name="configuration">
            <bean class="org.apache.ibatis.session.Configuration">
                <property name="mapUnderscoreToCamelCase" value="true"/>
            </bean>
        </property>
    </bean>

    <bean class=" org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cqh.mapper"/>
    </bean>

</beans>

 

3、 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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.cqh.controller"></context:component-scan>
    <mvc:annotation-driven />
</beans>

 

4、web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--向核心控制器告知spring的配置文件在哪-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
  </context-param>
  <!--配置spring的监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <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-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <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>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <servlet-name>dispatcherServlet</servlet-name>
  </filter-mapping>
</web-app>

 

测试效果

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1272142.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++: string的模拟实现

C: string的模拟实现 一.前置说明1.模拟实现string容器的目的2.我们要实现的大致框架 二.默认成员函数1.构造函数2.拷贝构造函数1.传统写法2.现代写法 3.析构函数4.赋值运算符重载1.传统写法2.现代写法 三.遍历和访问1.operator[]运算符重载2.iterator迭代器 四.容量相关函数1.…

ant design vue3 处理 ant-card-head ant-tabs靠左边对齐之has选择器不生效

火狐浏览器是不支持has的。 解决方法&#xff1a;通过position来解决。

拥抱未来:大语言模型解锁平台工程的无限可能

01 了解大型语言模型 (LLM) 大型语言模型&#xff08;LLM&#xff09;是一种人工智能&#xff08;AI&#xff09;算法&#xff0c;它使用深度学习技术和海量数据集来理解、总结、生成和预测新内容。凭借合成大量信息的能力&#xff0c;LLM 可以提高以前需要人类专家的业务流程的…

2002-2021年全国各省产业结构合理化高级化指数数据(含原始数据+计算过程+计算结果)

2002-2021年全国各省产业结构合理化高级化指数数据&#xff08;含原始数据计算过程计算结果&#xff09; 1、时间&#xff1a;2002-2021年 2、指标&#xff1a;地区、时间、就业总人数&#xff08;万人&#xff09;、第一产业就业人数&#xff08;万人&#xff09;、第二产业…

C语言从入门到实战——常用字符函数和字符串函数的了解和模拟实现

常用字符函数和字符串函数的了解和模拟实现 前言1. 字符分类函数2. 字符转换函数3. strlen的使用和模拟实现4. strcpy的使用和模拟实现5. strcat的使用和模拟实现6. strcmp的使用和模拟实现7. strncpy函数的使用8. strncat函数的使用9. strncmp函数的使用10. strstr的使用和模拟…

中通快递查询,中通快递单号查询,分析筛选出多次揽收件

批量查询中通快递单号的物流信息&#xff0c;并将其中的多次揽收件分析筛选出来。 所需工具&#xff1a; 一个【快递批量查询高手】软件 中通快递单号若干 操作步骤&#xff1a; 步骤1&#xff1a;运行【快递批量查询高手】软件&#xff0c;第一次使用的朋友记得先注册&…

阿里云ACP认证考试快速通关攻略,请收好!

目前云计算人才紧缺&#xff0c;预计2025年我国云计算产业人才缺口预计将达到150万&#xff0c;因此现在云计算工程师的薪资是相当可观的。而阿里云从2016年起就一直在国内市场占据着领先地位 。 阿里云目前稳居国内云计算市场第一&#xff0c;比排后面5名同行市场占有率的总和…

换抵挡装置(按位运算符的运用)

给出两个长度分别为n1&#xff0c;n2&#xff08;n1 n2 <32)且每列高度只为1或2的长条&#xff08;保证高度为1的地方水平上一致&#xff09;。需要将它们放入一个高度为3的容器长度&#xff0c;问能够容纳它们的最短容器长度 用手画的 本来是n1&#xff0c;n2 < 100的…

串口更新app程序(参考他人资料)

一&#xff1a;参考资料 1.选定镁光的falsh Debug一下bootloader工程&#xff0c;串口只输出了一个SREC SPI Bootloader。单步调试发现&#xff0c;是XIsf_Initialize出错了&#xff0c;返回了1&#xff0c;程序直接退出了main函数。再继续分析&#xff0c;发现程序走的是Atm…

2023.11.29 -hmzx电商平台建设项目 -核销主题阶段总结

目录 1.准备源数据 2.准备数仓工具进行源数据同步到ods层,本项目使用Datax 3.使用Datax完成数据同步前建表时的方案选择 3.1同步方式区别: 3.2存储格式和压缩区别: 4.在hive中创建表,共31个表 5.数仓概念 和 数仓建模方案 5.1数仓的基本概念 5.2 数仓建模方案 关系建模…

java论坛数据以及搜索接口实现

一. 内容简介 java论坛数据以及搜索接口实现 二. 软件环境 2.1 java 1.8 2.2 mysql Ver 8.0.13 for Win64 on x86_64 (MySQL Community Server - GPL) 2.3 IDEA ULTIMATE 2019.3 2.4d代码地址 三.主要流程 3.1 创建数据库,创建数据表 3.2 开始编写接口&#xff0c;并测…

C/C++不定参数的使用

文章目录 C语言的不定参C的不定参 C语言的不定参 C语言的不定参数最常见的应用示例就是printf函数&#xff0c;如下&#xff0c;参数列表中的...表示不定参数列表 #include <stdio.h> int printf(const char *format, ...);试着模拟实现C语言的printf函数 void myprin…

新手村之SQL——分组与子查询

1.GROUP BY GROUP BY 函数就是 SQL 中用来实现分组的函数&#xff0c;其用于结合聚合函数&#xff0c;能根据给定数据列的每个成员对查询结果进行分组统计&#xff0c;最终得到一个分组汇总表。 mysql> SELECT country, COUNT(country) AS teacher_count-> FROM teacher…

Linux系统部署Tale个人博客并发布到公网访问

文章目录 前言1. Tale网站搭建1.1 检查本地环境1.2 部署Tale个人博客系统1.3 启动Tale服务1.4 访问博客地址 2. Linux安装Cpolar内网穿透3. 创建Tale博客公网地址4. 使用公网地址访问Tale 前言 今天给大家带来一款基于 Java 语言的轻量级博客开源项目——Tale&#xff0c;Tale…

敏捷开发实现测试自动化的6个步骤

许多敏捷软件开发中的自动化测试的工作都失败了&#xff0c;或者并没有发挥它们最大的潜力。本文研究分析了自动化测试也许不能满足测试人员和其他利益相关者期望的两个主要原因&#xff0c;然后列举了六个能够避免陷入这些陷阱的步骤。以下是在敏捷环境中成功实现测试自动化的…

Linux系统iptables扩展

目录 一. iptables规则保存 1. 导出规则保存 2. 自动重载规则 ①. 当前用户生效 ②. 全局生效 二. 自定义链 1. 新建自定义链 2. 重命名自定义链 3. 添加自定义链规则 4. 调用自定义链规则 5. 删除自定义链 三. NAT 1. SNAT 2. DNAT 3. 实验 ①. 实验要求 ②. …

设计模式-创建型模式之原型、建造者设计模式

文章目录 七、原型模式八、建造者模式 七、原型模式 原型模式&#xff08;Prototype Pattern&#xff09;是用于创建重复的对象&#xff0c;同时又能保证性能。它提供了一种创建对象的最佳方式。 这种模式是实现了一个原型接口&#xff0c;该接口用于创建当前对象的克隆。当直…

C题目12:请写一个函数,判断一个数是否为质数,并在main函数中调用

一.每日小语 人的一切痛苦&#xff0c;本质上都是对自己的无能的愤怒。——王小波 自己思考 判断一个函数是否为质数&#xff0c;这个我在之前练过&#xff0c;我想至少两次&#xff0c;而这一次则是问我如何在main函数中调用&#xff0c;这个概念我不理解&#xff0c;所以我…

软件测试面试最全八股文

请你说一说测试用例的边界 参考回答&#xff1a; 边界值分析法就是对输入或输出的边界值进行测试的一种黑盒测试方法。通常边界值分析法是作为对等价类划分法的补充&#xff0c;这种情况下&#xff0c;其测试用例来自等价类的边界。 常见的边界值 1)对16-bit 的整数而言 32…

在gitlab上使用server_hooks

文章目录 1. 前置条件2. Git Hook2.1 Git Hook 分为两部分&#xff1a;本地和远程2.1.1 本地 Git Hook&#xff0c;由提交和合并等操作触发&#xff1a;2.1.2 远程 Git Hook&#xff0c;运行在网络操作上&#xff0c;例如接收推送的提交&#xff1a; 3. 操作步骤3.1 对所有的仓…