当前位置:网站首页>SSM整合
SSM整合
2022-06-24 09:43:00 【梦及海深@无】
1、导包
<!--导入我们的Servlet的API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--导入我们jstl的标签库的包 -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<!--第一步应该导入我们的mybatis的包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--导入我们的mybatis运行的时候的一些依赖包日志相关的 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--导入的是日志相关的包 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--带入cglib代理的包 -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
<!--这个就是咋们的这个插件的包-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!--引入junit类 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--导入myBatis和Spring整合的中间包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<!--导入我们Spring的相关包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<!--介入aspectj的相关包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--MySQL的驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.23.1-GA</version>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.12</version>
</dependency>
2、编写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-->
<!--引入Spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置SpringMVC-->
<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>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
3、编写bean-ibatis.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置数据库的连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql:///lkx"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!--配置SqlSessionfactorybean这个对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--配置那个MapperScannerConfigurer-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="basePackage" value="com.qf.ibatis.mapper"></property>
</bean>
<!--配置aop的自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--配置包的扫描-->
<context:component-scan base-package="com.qf.ibatis"></context:component-scan>
</beans>
4、编写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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--使能MVC的注解-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置的是SpringMVC的扫描-->
<context:component-scan base-package="com.qf.ibatis.controller"></context:component-scan>
</beans>
5、编写Controller
package com.qf.ibatis.controller;
import com.qf.ibatis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/** * @Auther: lkx * @Date: 2021/3/10 15:55 * @Description: */
@Controller
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("add")
public String add(){
userService.add();
return "/index.jsp";
}
}
6、编写Service
package com.qf.ibatis.service.impl;
import com.qf.ibatis.mapper.UserMapper;
import com.qf.ibatis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/** * @Auther: lkx * @Date: 2021/3/10 15:56 * @Description: */
@Service
public class UserService implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public void add() {
userMapper.add();
}
}
7、编写mapper
public interface UserMapper {
/** * 添加用户 */
void add();
}
8、编写mapper.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">
<!--下面的这个userMapper是可以随便写的 这个的意思是命名空间 只要这个值在这个系统中是唯一的就可以了 你不可以写两个名字一样的nameSpace 一般的写法就是pojo对象Mapper,这样就见名之意了 -->
<mapper namespace="com.qf.ibatis.mapper.UserMapper">
<!--这里就是使用标签来进行方法的描述-->
<insert id="add">
insert into t_user(userName,password) values("lkx","123");
</insert>
</mapper>
边栏推荐
- 简单的价格表样式代码
- Array seamless scrolling demo
- How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?
- CICFlowMeter源码分析以及为满足需求而进行的修改
- SQL Server AVG function rounding
- 正规方程、、、
- 时尚的弹出模态登录注册窗口
- Jcim | AI based protein structure prediction in drug discovery: impacts and challenges
- Symbol. Iterator iterator
- YOLOv6:又快又准的目标检测框架开源啦
猜你喜欢
队列Queue
Jcim | AI based protein structure prediction in drug discovery: impacts and challenges
Groovy obtains Jenkins credentials through withcredentials
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
Getting user information for applet learning (getuserprofile and getUserInfo)
411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency
canvas无限扫描js特效代码
SQL Server AVG函数取整问题
整理接口性能优化技巧,干掉慢代码
微信小程序学习之 实现列表渲染和条件渲染.
随机推荐
Queue queue
学习整理在php中使用KindEditor富文本编辑器
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
Operator details
Cicflowmeter source code analysis and modification to meet requirements
How do novices choose the grade of investment and financial products?
静态链接库和动态链接库的区别
时尚的弹出模态登录注册窗口
Cookie encryption 4 RPC method determines cookie encryption
Internet of things? Come and see Arduino on the cloud
Record the range of data that MySQL update will lock
Floating point notation (summarized from cs61c and CMU CSAPP)
学习使用KindEditor富文本编辑器,点击上传图片遮罩太大或白屏解决方案
Producer / consumer model
How to manage massive network infrastructure?
How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
Safety and food security for teachers and students of the trapped Yingxi middle school
Why is JSX syntax so popular?
MySQL data advanced
PHP encapsulates a file upload class (supports single file and multiple file uploads)