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

相关文章

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Win11安装PostgreSQL数据库的两种方式详细步骤

《Win11安装PostgreSQL数据库的两种方式详细步骤》PostgreSQL是备受业界青睐的关系型数据库,尤其是在地理空间和移动领域,:本文主要介绍Win11安装PostgreSQL数据库的... 目录一、exe文件安装 (推荐)下载安装包1. 选择操作系统2. 跳转到EDB(PostgreSQL 的

Python3.6连接MySQL的详细步骤

《Python3.6连接MySQL的详细步骤》在现代Web开发和数据处理中,Python与数据库的交互是必不可少的一部分,MySQL作为最流行的开源关系型数据库管理系统之一,与Python的结合可以实... 目录环境准备安装python 3.6安装mysql安装pymysql库连接到MySQL建立连接执行S

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

一文详解如何从零构建Spring Boot Starter并实现整合

《一文详解如何从零构建SpringBootStarter并实现整合》SpringBoot是一个开源的Java基础框架,用于创建独立、生产级的基于Spring框架的应用程序,:本文主要介绍如何从... 目录一、Spring Boot Starter的核心价值二、Starter项目创建全流程2.1 项目初始化(

Spring Boot3虚拟线程的使用步骤详解

《SpringBoot3虚拟线程的使用步骤详解》虚拟线程是Java19中引入的一个新特性,旨在通过简化线程管理来提升应用程序的并发性能,:本文主要介绍SpringBoot3虚拟线程的使用步骤,... 目录问题根源分析解决方案验证验证实验实验1:未启用keep-alive实验2:启用keep-alive扩展建

Python下载Pandas包的步骤

《Python下载Pandas包的步骤》:本文主要介绍Python下载Pandas包的步骤,在python中安装pandas库,我采取的方法是用PIP的方法在Python目标位置进行安装,本文给大... 目录安装步骤1、首先找到我们安装python的目录2、使用命令行到Python安装目录下3、我们回到Py

Spring Boot 整合 MyBatis 连接数据库及常见问题

《SpringBoot整合MyBatis连接数据库及常见问题》MyBatis是一个优秀的持久层框架,支持定制化SQL、存储过程以及高级映射,下面详细介绍如何在SpringBoot项目中整合My... 目录一、基本配置1. 添加依赖2. 配置数据库连接二、项目结构三、核心组件实现(示例)1. 实体类2. Ma

SpringBoot整合jasypt实现重要数据加密

《SpringBoot整合jasypt实现重要数据加密》Jasypt是一个专注于简化Java加密操作的开源工具,:本文主要介绍详细介绍了如何使用jasypt实现重要数据加密,感兴趣的小伙伴可... 目录jasypt简介 jasypt的优点SpringBoot使用jasypt创建mapper接口配置文件加密