SSM简单整合步骤(含简单实例)

最近学习了一下ssm框架,写了一个超级简单的ssm的整合项目,并记录如下:

1、创建数据库表

在这里插入图片描述

2、新建maven web项目

在这里插入图片描述

项目目录结构:

在这里插入图片描述

3、加入依赖

pom.xml

1.	<?xml version="1.0" encoding="UTF-8"?>  
2.	  
3.	<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
4.	  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
5.	  <modelVersion>4.0.0</modelVersion>  
6.	  
7.	  <groupId>com.ssm</groupId>  
8.	  <artifactId>SSM-Test</artifactId>  
9.	  <version>1.0-SNAPSHOT</version>  
10.	  <packaging>war</packaging>  
11.	  
12.	  
13.	  
14.	  <properties>  
15.	    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
16.	    <maven.compiler.source>1.8</maven.compiler.source>  
17.	    <maven.compiler.target>1.8</maven.compiler.target>  
18.	  </properties>  
19.	  
20.	  <dependencies>  
21.	    <dependency>  
22.	      <groupId>junit</groupId>  
23.	      <artifactId>junit</artifactId>  
24.	      <version>4.11</version>  
25.	      <scope>test</scope>  
26.	    </dependency>  
27.	  
28.	    <!--servlet依赖-->  
29.	    <dependency>  
30.	      <groupId>javax.servlet</groupId>  
31.	      <artifactId>javax.servlet-api</artifactId>  
32.	      <version>3.1.0</version>  
33.	    </dependency>  
34.	  
35.	    <!--jsp依赖-->  
36.	    <dependency>  
37.	      <groupId>javax.servlet</groupId>  
38.	      <artifactId>jsp-api</artifactId>  
39.	      <version>2.0</version>  
40.	      <scope>provided</scope>  
41.	    </dependency>  
42.	  
43.	    <!--spring-mvc依赖-->  
44.	    <!--包含SpringMVC框架相关的所有类。  
45.	    包含国际化、标签、Theme、视图展现的FreeMarker、JasperReports、 Tiles、Velocity、XSLT相关类。  
46.	    当然,如果你的应用使用了独立的MVC框架,则无需这个JAR文件里的任何类。-->  
47.	    <dependency>  
48.	      <groupId>org.springframework</groupId>  
49.	      <artifactId>spring-webmvc</artifactId>  
50.	      <version>5.2.7.RELEASE</version>  
51.	    </dependency>  
52.	  
53.	    <!--事务相关依赖-->  
54.	    <!--为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。-->  
55.	    <dependency>  
56.	      <groupId>org.springframework</groupId>  
57.	      <artifactId>spring-tx</artifactId>  
58.	      <version>5.2.7.RELEASE</version>  
59.	    </dependency>  
60.	    <!--jdbc依赖-->  
61.	    <dependency>  
62.	      <groupId>org.springframework</groupId>  
63.	      <artifactId>spring-jdbc</artifactId>  
64.	      <version>5.2.7.RELEASE</version>  
65.	    </dependency>  
66.	  
67.	    <!--Jackson依赖-->  
68.	    <dependency>  
69.	      <groupId>com.fasterxml.jackson.core</groupId>  
70.	      <artifactId>jackson-core</artifactId>  
71.	      <version>2.11.0</version>  
72.	    </dependency>  
73.	    <dependency>  
74.	      <groupId>com.fasterxml.jackson.core</groupId>  
75.	      <artifactId>jackson-databind</artifactId>  
76.	      <version>2.11.0</version>  
77.	    </dependency>  
78.	  
79.	    <!--mybatis整合spring依赖-->  
80.	    <dependency>  
81.	      <groupId>org.mybatis</groupId>  
82.	      <artifactId>mybatis-spring</artifactId>  
83.	      <version>2.0.5</version>  
84.	    </dependency>  
85.	  
86.	    <!--mybatis依赖-->  
87.	    <dependency>  
88.	      <groupId>org.mybatis</groupId>  
89.	      <artifactId>mybatis</artifactId>  
90.	      <version>3.5.0</version>  
91.	    </dependency>  
92.	  
93.	    <!--mysql驱动依赖-->  
94.	    <dependency>  
95.	      <groupId>mysql</groupId>  
96.	      <artifactId>mysql-connector-java</artifactId>  
97.	      <version>8.0.20</version>  
98.	    </dependency>  
99.	  
100.	    <!--阿里巴巴连接池依赖-->  
101.	    <dependency>  
102.	      <groupId>com.alibaba</groupId>  
103.	      <artifactId>druid</artifactId>  
104.	      <version>1.1.10</version>  
105.	    </dependency>  
106.	  </dependencies>  
107.	  
108.	  <build>  
109.	    <resources>  
110.	      <!-- 不编译resources下的配置文件 -->  
111.	      <resource>  
112.	        <directory>src/main/java</directory>  
113.	        <includes>  
114.	          <include>**/*.xml</include> 
115.	          <include>**/*.properties</include>  
116.	        </includes>  
117.	        <filtering>false</filtering>  
118.	      </resource>  
119.	  
120.	      <resource>  
121.	        <directory>src/main/resources</directory>  
122.	        <includes>  
123.	          <include>**/*.xml</include> 
124.	          <include>**/*.properties</include>  
125.	        </includes>  
126.	        <filtering>false</filtering>  
127.	      </resource>  
128.	    </resources>  
129.	  
130.	    <plugins>  
131.	      <plugin>  
132.	        <artifactId>maven-compiler-plugin</artifactId>  
133.	        <version>3.1</version>  
134.	        <configuration>  
135.	          <source>1.8</source>  
136.	          <target>1.8</target>  
137.	        </configuration>  
138.	      </plugin>  
139.	    </plugins>  
140.	  </build>  
141.	</project>

4、web.xml

1)、注册dispatcherServlet

     目的:a、创建springMVC容器对象(才能创建controller类对象)
	            b、创建servlet(才能接受用户请求)
1.	<servlet>  
2.	        <servlet-name>dispatcherServlet</servlet-name>  
3.	        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
4.	        <init-param>  
5.	            <param-name>contextConfigLocation</param-name>  
6.	            <param-value>classpath:dispatcherServlet.xml</param-value>  
7.	        </init-param>  
8.	        <load-on-startup>1</load-on-startup>  
9.	    </servlet>  
10.	    <servlet-mapping>  
11.	        <servlet-name>dispatcherServlet</servlet-name>  
12.	        <url-pattern>*.do</url-pattern>  
13.	    </servlet-mapping> 

2)、注册spring的监听器:ContextLoaderListener

     目的:创建spring的容器对象(才能创建service、dao等对象)
1.	<context-param>  
2.	        <param-name>contextConfigLocation</param-name>  
3.	        <param-value>classpath:applicationContext.xml</param-value>  
4.	    </context-param>  
5.	    <listener>  
6.	        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
7.	    </listener> 

3)、注册字符集过滤器

     目的:解决post请求乱码问题
1.	<filter>  
2.	        <filter-name>characterEncodingFilter</filter-name>  
3.	        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
4.	        <init-param>  
5.	            <param-name>encoding</param-name>  
6.	            <param-value>utf-8</param-value>  
7.	        </init-param><init-param>  
8.	            <param-name>forceRequestEncoding</param-name>  
9.	            <param-value>true</param-value>  
10.	        </init-param><init-param>  
11.	            <param-name>forceResponseEncoding</param-name>  
12.	            <param-value>true</param-value>  
13.	        </init-param>  
14.	    </filter>  
15.	    <filter-mapping>  
16.	        <filter-name>characterEncodingFilter</filter-name>  
17.	        <url-pattern>/*</url-pattern>  
18.	    </filter-mapping>  

web.xml整体代码如下:

1.	<?xml version="1.0" encoding="UTF-8"?>  
2.	<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
3.	         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
4.	         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"  
5.	         version="4.0">  
6.	  
7.	    <!--注册DispatchServlet-->  
8.	    <servlet>  
9.	        <servlet-name>dispatcherServlet</servlet-name>  
10.	        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
11.	        <init-param>  
12.	            <param-name>contextConfigLocation</param-name>  
13.	            <param-value>classpath:dispatcherServlet.xml</param-value>  
14.	        </init-param>  
15.	        <load-on-startup>1</load-on-startup>  
16.	    </servlet>  
17.	    <servlet-mapping>  
18.	        <servlet-name>dispatcherServlet</servlet-name>  
19.	        <url-pattern>*.do</url-pattern>  
20.	    </servlet-mapping>  
21.	  
22.	    <!--注册spring监听器-->  
23.	    <context-param>  
24.	        <param-name>contextConfigLocation</param-name>  
25.	        <param-value>classpath:applicationContext.xml</param-value>  
26.	    </context-param>  
27.	    <listener>  
28.	        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
29.	    </listener>  
30.	  
31.	    <!--注册字符集过滤器-->  
32.	    <filter>  
33.	        <filter-name>characterEncodingFilter</filter-name>  
34.	        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
35.	        <init-param>  
36.	            <param-name>encoding</param-name>  
37.	            <param-value>utf-8</param-value>  
38.	        </init-param><init-param>  
39.	            <param-name>forceRequestEncoding</param-name>  
40.	            <param-value>true</param-value>  
41.	        </init-param><init-param>  
42.	            <param-name>forceResponseEncoding</param-name>  
43.	            <param-value>true</param-value>  
44.	        </init-param>  
45.	    </filter>  
46.	    <filter-mapping>  
47.	        <filter-name>characterEncodingFilter</filter-name>  
48.	        <url-pattern>/*</url-pattern>  
49.	    </filter-mapping>  
50.	</web-app>  

5、代码实现

1)、实体类

Student.java

1.	package com.ssm.domain;  
2.	  
3.	public class Student {  
4.	    private int id;  
5.	    private String name;  
6.	    private int age;  
7.	  
8.	    public int getId() {  
9.	        return id;  
10.	    }  
11.	  
12.	    public void setId(int id) {  
13.	        this.id = id;  
14.	    }  
15.	  
16.	    public String getName() {  
17.	        return name;  
18.	    }  
19.	  
20.	    public void setName(String name) {  
21.	        this.name = name;  
22.	    }  
23.	  
24.	    public int getAge() {  
25.	        return age;  
26.	    }  
27.	  
28.	    public void setAge(int age) {  
29.	        this.age = age;  
30.	    }  
31.	  
32.	    @Override  
33.	    public String toString() {  
34.	        return "Student{" +  
35.	                "id=" + id +  
36.	                ", name='" + name + '\'' +  
37.	                ", age=" + age +  
38.	                '}';  
39.	    }  
40.	}  

2)、dao接口

StudentDao.java

1.	package com.ssm.dao;  
2.	  
3.	import com.ssm.domain.Student;  
4.	  
5.	import java.util.List;  
6.	  
7.	public interface StudentDao {  
8.	    //插入学生信息操作  
9.	    public int insertStudent(Student student);  
10.	      
11.	    //查询所有学生信息操作  
12.	    public List<Student> selectStudents();  
13.	      
14.	    //按id查询学生信息  
15.	    public Student selectStudentById(int id);  
16.	      
17.	}  

3)、mapper文件

StudentDao.xml

1.	<?xml version="1.0" encoding="UTF-8" ?>  
2.	<!DOCTYPE mapper  
3.	        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
4.	        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
5.	<mapper namespace="com.ssm.dao.StudentDao">  
6.	    <insert id="insertStudent" parameterType="com.ssm.domain.Student">  
7.	        insert into students(name, age) values(#{name}, #{age})  
8.	    </insert>  
9.	      
10.	    <select id="selectStudents" resultType="com.ssm.domain.Student">  
11.	        select id, name, age from students  
12.	    </select>  
13.	      
14.	    <select id="selectStudentById" parameterType="int" resultType="com.ssm.domain.Student">  
15.	        select id, name, age from where id = #{id}  
16.	    </select>  
17.	</mapper>  

4)、service和实现类

StudentService.java

1.	package com.ssm.service;  
2.	  
3.	import com.ssm.domain.Student;  
4.	import org.springframework.stereotype.Service;  
5.	  
6.	import java.util.List;  
7.	  
8.	@Service  
9.	public interface StudentService {  
10.	    //增加学生信息  
11.	    public int addStudent(Student student);  
12.	  
13.	    //查询所有学生信息  
14.	    public List<Student> queryStudetns();  
15.	  
16.	    //按id查询学生信息  
17.	    public Student queryStudentById(int id);  
18.	  
19.	}  

StudentServiceImpl.java

1.	package com.ssm.service.impl;  
2.	  
3.	import com.ssm.dao.StudentDao;  
4.	import com.ssm.domain.Student;  
5.	import com.ssm.service.StudentService;  
6.	import org.springframework.beans.factory.annotation.Autowired;  
7.	import org.springframework.stereotype.Service;  
8.	  
9.	import java.util.List;  
10.	  
11.	@Service  
12.	public class StudentServiceImpl implements StudentService {  
13.	    @Autowired  
14.	    private StudentDao dao;  
15.	      
16.	    @Override  
17.	    public int addStudent(Student student) {  
18.	        int num = dao.insertStudent(student);  
19.	        return num;  
20.	    }  
21.	  
22.	    @Override  
23.	    public List<Student> queryStudetns() {  
24.	        List<Student> students = dao.selectStudents();  
25.	        return students;  
26.	    }  
27.	  
28.	    @Override  
29.	    public Student queryStudentById(int id) {  
30.	        Student student = dao.selectStudentById(id);  
31.	        return student;  
32.	    }  
33.	}  

5)、Controller

StudentController.java

1.	package com.ssm.controller;  
2.	  
3.	import com.ssm.domain.Student;  
4.	import com.ssm.service.StudentService;  
5.	import org.springframework.beans.factory.annotation.Autowired;  
6.	import org.springframework.stereotype.Controller;  
7.	import org.springframework.web.bind.annotation.RequestMapping;  
8.	import org.springframework.web.servlet.ModelAndView;  
9.	  
10.	@Controller  
11.	public class StudentController {  
12.	  
13.	    @Autowired  
14.	    private StudentService studentService;  
15.	  
16.	    @RequestMapping("/insert.do")  
17.	    public ModelAndView doInsert(String name, Integer age){  
18.	        Student student = new Student();  
19.	        student.setName(name);  
20.	        student.setAge(age);  
21.	        int num = studentService.addStudent(student);  
22.	  
23.	        ModelAndView mv = new ModelAndView();  
24.	        mv.addObject("name", name);  
25.	        mv.addObject("age", age);  
26.	        mv.addObject("num", num);  
27.	        mv.setViewName("insert");  
28.	        return mv;  
29.	    }  
30.	}  

6)、SpringMVC配置文件

dispatcherServlet.xml

1.	<?xml version="1.0" encoding="UTF-8"?>  
2.	<beans xmlns="http://www.springframework.org/schema/beans"  
3.	       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
4.	       xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.alibaba.com/schema/stat"  
5.	       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.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd">  
6.	  
7.	    <context:component-scan base-package="com.ssm.controller"/>  
8.	  
9.	    <!--视图解析器-->  
10.	    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
11.	        <property name="prefix" value="/WEB-INF/jsp/"/>  
12.	        <property name="suffix" value=".jsp"/>  
13.	    </bean>  
14.	  
15.	    <!--注解驱动-->  
16.	    <mvc:annotation-driven/>  
17.	  
18.	</beans>  

7)、Spring配置文件

applicationContext.xml

1.	<?xml version="1.0" encoding="UTF-8"?>  
2.	<beans xmlns="http://www.springframework.org/schema/beans"  
3.	       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
4.	       xmlns:context="http://www.springframework.org/schema/context"  
5.	       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">  
6.	  
7.	    <!--声明DataSource-->  
8.	    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"  
9.	          destroy-method="close">  
10.	        <property name="url" value="jdbc:mysql://localhost:3306/bcdata?serverTimezone=UTC"/>  
11.	        <property name="username" value="root"/>  
12.	        <property name="password" value="333"/>  
13.	    </bean>  
14.	  
15.	    <!--声明sqlSessionFactory-->  
16.	    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
17.	        <property name="configLocation" value="classpath:mybatis.xml"/>  
18.	        <property name="dataSource" ref="myDataSource"/>  
19.	    </bean>  
20.	  
21.	    <!--声明mybatis的扫描器,创建dao对象-->  
22.	    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
23.	        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>  
24.	        <property name="basePackage" value="com.ssm.dao"/>  
25.	    </bean>  
26.	  
27.	    <!--声明@service所在的包名-->  
28.	    <context:component-scan base-package="com.ssm.service"/>  
29.	</beans>  

8)、Mybatis配置文件

Mybatis.xml

1.	<?xml version="1.0" encoding="UTF-8" ?>  
2.	<!DOCTYPE configuration  
3.	        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
4.	        "http://mybatis.org/dtd/mybatis-3-config.dtd">  
5.	<configuration>  
6.	    <settings>  
7.	        <setting name="logImpl" value="STDOUT_LOGGING"/>  
8.	    </settings>  
9.	  
10.	    <!--设置别名-->  
11.	    <typeAliases>  
12.	        <!--实体类所在的包名-->  
13.	        <package name="com.ssm.domain"/>  
14.	    </typeAliases>  
15.	  
16.	    <!--sql mapper(sql映射文件)的位置-->  
17.	    <mappers>  
18.	        <!--name:是包名,这个包中的所有mapper。xml一次都能加载-->  
19.	        <package name="com.ssm.dao"/>  
20.	    </mappers>  
21.	  
22.	</configuration> 

6、写jsp页面

Index页面
index.jsp

1.	<%@ page contentType="text/html;charset=UTF-8" language="java" %>  
2.	<html>  
3.	<head>  
4.	    <title>Title</title>  
5.	</head>  
6.	<body>  
7.	  
8.	    <form action="insert.do">  
9.	        <table>  
10.	            <tr>  
11.	                <td>请输入需要插入的学生信息</td>  
12.	            </tr>  
13.	            <tr>  
14.	                <td>姓名</td>  
15.	                <td><input id="name" name="name"></td>  
16.	            </tr>  
17.	            <tr>  
18.	                <td>年龄</td>  
19.	                <td><input id="age" name="age"></td>  
20.	            </tr>  
21.	            <tr>  
22.	                <td><input type="submit" value="增加"></td>  
23.	            </tr>  
24.	        </table>  
25.	    </form>  
26.	</body>  
27.	</html>  

结果页面
Insert.jsp

1.	<%@ page contentType="text/html;charset=UTF-8" language="java" %>  
2.	<html>  
3.	<head>  
4.	    <title>Title</title>  
5.	</head>  
6.	<body>  
7.	    姓名为${name},年龄为${age}的学生信息插入成功  
8.	</body>  
9.	</html>  

运行:

初始页面

在这里插入图片描述

运行结果页面:

在这里插入图片描述

数据库运行结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值