文章目录
SSM整合
一、Idea新建maven项目
项目结构
二、Maven导入依赖
1.导入依赖
打开pom.xml文件导入
<?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>com.lxl</groupId>
<artifactId>ssm2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.3</version>
</dependency>
</dependencies>
</project>
三、新建spring配置文件
1.连接数据库配置文件
新建db.properties文件放在resources目录下
url=jdbc:mysql://localhost:3306/spring?useSSL=false
user=root
password=123456
2.spring配置文件
新建applicationContext.xml
放在Resources目录下
<?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:tx="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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--载入连接数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!--得到sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--此包下的类自动添加别名,别名是类名-->
<property name="typeAliasesPackage" value="ssm2.pojo"/>
<!--此包下的所有映射文件自动添加到mybatis-->
<property name="mapperLocations">
<list>
<value>classpath:mappers/**</value>
</list>
</property>
</bean>
<!--事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务管理注解驱动-->
<tx:annotation-driven/>
<!--配置dao接口扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="ssm2.dao"/>
</bean>
<!--获得service对象并注入dao对象-->
<bean id="userService2" class="ssm2.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<!--获得control对象并注入service-->
<bean class="ssm2.control.UserControl">
<property name="userService" ref="userService2"/>
</bean>
</beans>
四、新建springMVC配置文件
新建springMVC.xml文件在resources目录下
<?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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描处理器包-->
<context:component-scan base-package="ssm2.control"/>
<!--注解驱动-->
<mvc:annotation-driven/>
<!--放行静态资源-->
<mvc:resources mapping="/static/**" location="/static/"/>
</beans>
五、配置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">
<!--添加spring配置文件到context域,spring运行加载-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--添加监听器,把xmlWebApplicationContext对象放到ServletContext域-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--添加springMVC DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--springMVC需要的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!--tomcat启动后马上创建DispatcherServlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--配置“/",静态资源不能访问,需要在springMVC文件进行配置-->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
六、测试
1.User类
在pojo包下创建User类,实现serializable接口(事务属性readOnly=false需要实现接口)
2.UserDao接口
3.UserMapper.xml
需要放在spring配置文件指定的resources/mapper/目录下才能自动添加
<?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="ssm2.dao.UserDao">
<select id="queryById" resultType="User">
select id,username,password from spring.user where id= #{id}
</select>
<insert id="insert" parameterType="User">
insert into spring.user values (#{id},#{username},#{password})
</insert>
</mapper>
4.UserService接口
在spring配置文件中添加bean(已添加,可在配置文件查看)
5.UserService实现类
UserServiceImpl 需要添加到spring配置文件(可在配置文件查看)
添加属性UserDao,在配置文件注入
6.处理器类创建
package ssm2.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ssm2.pojo.User;
import ssm2.service.UserService;
import javax.annotation.Resource;
@Controller
@RequestMapping("/user/")
public class UserControl {
@Resource
public UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@RequestMapping("all")
public ModelAndView showAllUsers(){
ModelAndView mv=new ModelAndView();
mv.addObject("a","ok");
mv.setViewName("/index.jsp");
return mv;
}
@RequestMapping("test")
public ModelAndView test(){
ModelAndView mv=new ModelAndView();
System.out.println("test control run");
mv.addObject("user","ok");
mv.setViewName("/index.jsp");
return mv;
}
@RequestMapping("id")
public ModelAndView id(int id){
ModelAndView mv=new ModelAndView();
User user=userService.id(id);
mv.addObject("user",user.toString());
mv.setViewName("/index.jsp");
return mv;
}
@RequestMapping("b")
@ResponseBody
public User b(int id){
return userService.id(id);
}
@RequestMapping("add")
public ModelAndView add(User user){
System.out.println(user);
userService.addUser(user);
ModelAndView mv=new ModelAndView();
mv.addObject("user",user.toString());
mv.setViewName("/index.jsp");
return mv;
}
}
7.index.jsp 编写
<%@ taglib prefix="input" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery/jquery-3.5.1.js"></script>
</head>
<body>
<a href="${pageContext.request.contextPath}/user/test">test</a>
<br/>
${user}
<br/>
<a id="a" href="${pageContext.request.contextPath}/user/id?id=1">user</a>
<br/>
<span id="s"></span>
<button id="b">user</button>
<br/>
<span id="d"></span>
<br/>
<form action="${pageContext.request.contextPath}/user/add" method="post">
id:<input type="text" name="id" value="10"/>
username:<input type="text" name="username" value="kobe"/>
password:<input type="password" name="password" value="5555"/>
<input type="submit" >
</form>
</body>
<script>
$("#b").click(function (){
$.getJSON("/ssm2/user/b",{"id":2,"d":5},function (d){
console.log(JSON.stringify(d));
$("#d").html(JSON.stringify(d));
})
});
$("#a").click(function (){
$("#s").html("${user}")
})
</script>
</html>
总结
1.新建maven项目;
管理依赖
2.配置spring配置文件,添加mybatis配置;
注册bean,添加连接池,添加事务,管理mybatis相关文件
3.配置springMVC配置文件;
管理处理器,处理静态资源
4.配置web.xml文件
添加dispatcherServlet,添加xmlWebApplicationContext