基于spring的ssm整合
目录
基于spring的ssm整合
Spring 框架
SpringMVC 框架
MyBatis 框架
1.创建项目
2.导入依赖
3.导入sql
4.创建jdbc.propries文件
1)mysql8以下
2)mysql8以上的
5.创建mybatis-config.xml配置文件
6.创建spring-Config.xml文件
7.创建项目所需包和类
1)创建mapper包,并且创建UserMapper接口文件
UserMapper
2)在resources文件下创建mapper,并且创建UserMapper.xml文件
3)创建service包,创建MyService接口,并且创建impl包实现MyService
MyService
编辑
MyServiceImpl
4)创建controller包,并且创建MyController
5)创建enty包,并且创建User实体类
User实体类
8.为项目添加web.xml文件
1)找到项目的pom.xml文件,修改为war包
2)添加web.xml文件
1-按ctrl+Shift+alt+s 弹出界面
2-操作如下
9.配置web.xml文件
10.为项目配置tomcat
11.启动测试
附录
1.sql
usersql
下载地址
2.springConfig配置文件
3.mybatis-config配置文件
4.web.xml配置文件
5.jdbc.properies文件
基于spring的ssm整合
SSM框架指的是Spring、SpringMVC和MyBatis三个Java框架的整合。这三个框架可以一起使用来构建一个功能完整的企业级应用。
Spring 框架
- Spring是一个开源的轻量级Java EE框架,主要用来解决企业应用开发中的复杂性问题。Spring的核心特性可以将应用的各种组件进行解耦,它提供了依赖注入(Dependency Injection, DI)和面向切面编程(AOP)等功能。Spring还提供了一系列模块如Spring MVC用于Web层的支持。
SpringMVC 框架
- SpringMVC是Spring框架的一个模块,它是一个基于MVC设计模式的轻量级Web框架。SpringMVC负责处理HTTP请求和响应,并且可以与Spring框架无缝集成。它定义了请求-响应模型,简化了Web应用的开发流程。
MyBatis 框架
- MyBatis是一个优秀的持久层框架,它内部封装了JDBC API的操作,简化了数据访问层(DAL)的编程。它支持SQL查询、更新、事务管理等操作,同时也支持存储过程。
1.创建项目
2.导入依赖
<dependencies> <!--web依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.1.12</version> </dependency> <!--spring-context--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.1.12</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.16</version> </dependency> <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>6.1.0</version> </dependency> <!--servlet-api--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>6.1.0</version> </dependency> <!--druid连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.7</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!--mybatis-Spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>3.0.4</version> </dependency> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.2</version> </dependency> </dependencies>
3.导入sql
4.创建jdbc.propries文件
1)mysql8以下
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
2)mysql8以上的
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
5.创建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="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> </settings> <!--配置分页插件--> <plugins> <plugin interceptor="www.github.pagehelper.PageInterceptor"></plugin> </plugins> </configuration>
6.创建spring-Config.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="org.xiji"/> <!--开启springmvc注解驱动--> <mvc:annotation-driven/> <!--导入外部配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置druid数据源--> <bean id="datasource" 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> <!--配置mybatis--> <bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--配置xml文件的位置--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!--配置mapper扫描位置--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.xiji.mapper"/> <property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"/> </bean> </beans>
7.创建项目所需包和类
1)创建mapper包,并且创建UserMapper接口文件
UserMapper
package org.xiji.mapper; import org.apache.ibatis.annotations.Mapper; import org.xiji.enty.User; import java.util.List; @Mapper public interface UserMapper { public List<User> test() ; }
2)在resources文件下创建mapper,并且创建UserMapper.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="org.xiji.mapper.UserMapper">
<select id="test" resultType="org.xiji.enty.User">
select * from user
</select>
</mapper>
注:红色位置为你的mapper接口文件位置
3)创建service包,创建MyService接口,并且创建impl包实现MyService
MyService
package org.xiji.serive; import org.xiji.enty.User; import java.util.List; public interface MyService { List<User> test(); }
MyServiceImpl
package org.xiji.serive.impl; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.xiji.enty.User; import org.xiji.mapper.UserMapper; import org.xiji.serive.MyService; import java.util.List; @Service public class MyServiceImpl implements MyService { @Autowired private UserMapper userMapper; @Override public List<User> test() { //返回数据 //设置分页插件 Page<User> user = PageHelper.startPage(1, 2); userMapper.test(); List<User> result = user.getResult(); return result; } }
4)创建controller包,并且创建MyController
package org.xiji.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.xiji.enty.User; import org.xiji.mapper.UserMapper; import org.xiji.serive.MyService; import java.util.List; @Controller public class MyController { @Autowired MyService myService; @RequestMapping(value = "/",produces = {}) @ResponseBody public List<User> test() { List<User> test = myService.test(); return test; } }
5)创建enty包,并且创建User实体类
User实体类
package org.xiji.enty; public class User { private int id; private String username; private String password; private String userInfo; public User() { } public User(int id, String username, String password, String userInfo) { this.id = id; this.username = username; this.password = password; this.userInfo = userInfo; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", userInfo='" + userInfo + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserInfo() { return userInfo; } public void setUserInfo(String userInfo) { this.userInfo = userInfo; } }
8.为项目添加web.xml文件
1)找到项目的pom.xml文件,修改为war包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.xiji</groupId> <artifactId>spring-ssm</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!--web依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.1.12</version> </dependency> <!--spring-context--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.1.12</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.16</version> </dependency> <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>6.1.0</version> </dependency> <!--servlet-api--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency><dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>6.1.0</version> </dependency><!--druid连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.7</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!--mybatis-Spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>3.0.4</version> </dependency><!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.2</version> </dependency></dependencies> </project>
2)添加web.xml文件
1-按ctrl+Shift+alt+s 弹出界面
2-操作如下
9.配置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"> <servlet> <servlet-name>MyPro</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置spring配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springConfig.xml</param-value> </init-param> <!--初始化提前--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyPro</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
10.为项目配置tomcat
add ==>tomcat ===>local
configure===>fix===>任选一个
application contenx 修改为 /
11.启动测试
访问成功 http://localhost:8080/
附录
1.sql
usersql
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int NOT NULL AUTO_INCREMENT COMMENT '用户id', `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户名字', `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户密码', `userInfo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户信息', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, 'xiji', '123456', 'helloword'); INSERT INTO `user` VALUES (2, 'xiaojiu', '123456', 'helloword'); INSERT INTO `user` VALUES (3, 'as', '123456', 'helloword'); INSERT INTO `user` VALUES (4, 'zs', '123456', 'helloword'); SET FOREIGN_KEY_CHECKS = 1;
下载地址
user.sql官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘
2.springConfig配置文件
<?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="org.xiji"/> <!--开启springmvc注解驱动--> <mvc:annotation-driven/> <!--导入外部配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置druid数据源--> <bean id="datasource" 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> <!--配置mybatis--> <bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--配置xml文件的位置--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!--配置mapper扫描位置--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.xiji.mapper"/> <property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"/> </bean> </beans>
3.mybatis-config配置文件
<?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="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> </settings> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins> </configuration>
4.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">
<servlet>
<servlet-name>MyPro</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置spring配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springConfig.xml</param-value>
</init-param>
<!--初始化提前-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyPro</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
5.jdbc.properies文件
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root