ssm_ums整合步骤

2023-11-07 19:59
文章标签 ssm 步骤 整合 ums

本文主要是介绍ssm_ums整合步骤,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ssm_ums整合步骤


1.创建Maven webapp项目

2.导入jar包和插件

3.拷贝UMS页面

4.创建java和resources文件夹

5.初始化数据库和表

CREATE TABLE t_user (uid INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,login_name VARCHAR(50) DEFAULT NULL,login_pwd VARCHAR(32) DEFAULT NULL,age INT(11) DEFAULT NULL,birthday DATE DEFAULT NULL,sex VARCHAR(1) DEFAULT NULL,education VARCHAR(50),telephone VARCHAR(11),interest VARCHAR(100),remark VARCHAR(200)
)insert  into `t_user`(`uid`,`login_name`,`login_pwd`,`age`,`birthday`,`sex`,`education`,`telephone`,`interest`,`remark`) values (1,'jack','1234',18,'1996-11-11','男',NULL,NULL,NULL,NULL),(2,'rose','1234',21,'1993-11-11','女',NULL,NULL,NULL,NULL),(3,'tom','1234',23,'1996-12-24','男',NULL,NULL,NULL,NULL);

6.创建db.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/表名
jdbc.username=用户名
jdbc.password=密码

7.项目目录

在这里插入图片描述

8.创建javaBean

@Table(name="t_user")
public class User {
@Id
private Integer uid;
private String loginName;
private String loginPwd;
private Integer age;
private String sex;
private String birthday;
private String education;
private String telephone;
private String interest;		//爱好:对应数据库(内容:A,B,C)
private String[] interestArr;	//爱好:对应页面表单
private String remark;爱好需要特殊处理
public String getInterest() {if(interestArr != null){interest = Arrays.toString(interestArr);  //[A,B,C]interest = interest.substring(1, interest.length() - 1);	//A,B,C}return interest;
}
public void setInterest(String interest) {this.interest = interest;if(interest != null){interestArr = interest.split(", ");}
}

9.编写mapper接口

public interface UserMapper extends Mapper<User> {
}

10.编写service接口

public interface UserService {public List<User> findAll();}

11.编写service实现类

@Service(value = "userServiceImpl")
@Transactional
public class UserServiceImpl implements UserService {@Resourceprivate UserMapper userMapper;@Overridepublic List<User> findAll() {List<User> users = userMapper.selectAll();return users;}
}

12.编写controller实现类

@Controller
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/findAllUsers.action")public String findAll(Model model){List<User> userList = userService.findAll();model.addAttribute("userList", userList);return "/user/list.jsp";}
}

13.编写jsp页面

14.编写spring配置类

@Configuration
//要扫描的包
@ComponentScan(basePackages = {"com.czxy"})
//开启事务支持
@EnableTransactionManagement
//读取properties配置文件
@PropertySource(value = "classpath:db.properties")
public class SpringConfig {//4.2.4 读取properties的固定代码@Beanpublic static PropertySourcesPlaceholderConfigurer create(){return new PropertySourcesPlaceholderConfigurer();}//读取数据库中的相关配置@Value("${jdbc.driver}")private String driverClass;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;//设置德鲁伊连接池@Beanpublic DataSource dataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setPassword(password);dataSource.setUsername(username);dataSource.setUrl(url);dataSource.setDriverClassName(driverClass);return dataSource;}//开启事务管理器@Bean@Resourcepublic DataSourceTransactionManager txManager(DataSource dataSource){return  new DataSourceTransactionManager(dataSource);}
}

15.编写mybatis配置类

@Configuration
public class MybatisConfig {//获取sessionFactory对象,@Bean@Resourcepublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {//1.设置SQL// 通过工厂bean创建对象,对吼需要调用 getObject获取具体对象SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();// 设置数据源factoryBean.setDataSource(dataSource);// 设置别名 要扫描的包factoryBean.setTypeAliasesPackage("com.czxy.domain");// 全局驼峰映射org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();config.setMapUnderscoreToCamelCase(true);factoryBean.setConfiguration(config);//2.插件配置 分页PageHelper pageHelper = new PageHelper();Properties pro = new Properties();pro.setProperty("dialect", "mysql");pro.setProperty("rowBoundsWithCount", "true");pageHelper.setProperties(pro);factoryBean.setPlugins(new Interceptor[]{pageHelper});//3. 通过工厂bean 获取 SQLSessionFactoryreturn factoryBean.getObject();}/***  设置要扫描的 mapper包 将创建好的XXXMapper对象存放到IOC容器中* @return*/@Beanpublic MapperScannerConfigurer mapperScanner(){MapperScannerConfigurer configurer = new MapperScannerConfigurer();configurer.setBasePackage("com.czxy.dao");return configurer;}
}

16.编写springMVC配置类

//声明为配置类
@Configuration
//设置扫描 controller
@ComponentScan("com.czxy.controller")
public class SpringMvcConfig {
}

17.编写web启动配置类配置类

public class WebInitializer implements WebApplicationInitializer {@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {//1.初始化 Spring 容器AnnotationConfigWebApplicationContext applicationContext  = new AnnotationConfigWebApplicationContext();applicationContext.register(SpringMvcConfig.class);applicationContext.register(SpringConfig.class);applicationContext.register(MybatisConfig.class);applicationContext.setServletContext(servletContext);//2.设置核心控制器ServletRegistration.Dynamic ds = servletContext.addServlet("springmvc", new DispatcherServlet(applicationContext));ds.addMapping("*.action");ds.setLoadOnStartup(2);//3 post乱码配置    FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("CharacterEncodingFilter", new CharacterEncodingFilter("UTF-8"));encodingFilter.addMappingForUrlPatterns(null, true, "/*");}
}

18.测试

这篇关于ssm_ums整合步骤的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/365956

相关文章

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

Java使用正则提取字符串中的内容的详细步骤

《Java使用正则提取字符串中的内容的详细步骤》:本文主要介绍Java中使用正则表达式提取字符串内容的方法,通过Pattern和Matcher类实现,涵盖编译正则、查找匹配、分组捕获、数字与邮箱提... 目录1. 基础流程2. 关键方法说明3. 常见场景示例场景1:提取所有数字场景2:提取邮箱地址4. 高级

Spring Boot 整合 SSE(Server-Sent Events)实战案例(全网最全)

《SpringBoot整合SSE(Server-SentEvents)实战案例(全网最全)》本文通过实战案例讲解SpringBoot整合SSE技术,涵盖实现原理、代码配置、异常处理及前端交互,... 目录Spring Boot 整合 SSE(Server-Sent Events)1、简述SSE与其他技术的对

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

springboot整合mqtt的步骤示例详解

《springboot整合mqtt的步骤示例详解》MQTT(MessageQueuingTelemetryTransport)是一种轻量级的消息传输协议,适用于物联网设备之间的通信,本文介绍Sprin... 目录1、引入依赖包2、yml配置3、创建配置4、自定义注解6、使用示例使用场景:mqtt可用于消息发

Java实现TXT文件导入功能的详细步骤

《Java实现TXT文件导入功能的详细步骤》在实际开发中,很多应用场景需要将用户上传的TXT文件进行解析,并将文件中的数据导入到数据库或其他存储系统中,本文将演示如何用Java实现一个基本的TXT文件... 目录前言1. 项目需求分析2. 示例文件格式3. 实现步骤3.1. 准备数据库(假设使用 mysql