SSM整合笔记
1,创建maven项目
创建maven项目过程省略
ps:如果创建完maven项目之后项目报错,可能是没有配置Tomcat
2,在pom.xml里面导入相应的jar的依赖
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.junge</groupId>
<artifactId>SSM1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SSM1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- 导入springmvc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency> <!-- spring-jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- mybatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.3</version>
</dependency> <!-- 连接池commons-dbcp -->
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency> <!-- 数据库驱动 mysql -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency> <!-- mybatis-spring -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency> <!-- jstl -->
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- js -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.4</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency> <!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<!-- 引入PageHelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency> </dependencies>
<build>
<finalName>SSM1</finalName>
</build>
</project>
3,构建基本目录
A:包目录
根据mvc分层思想,创建包目录
- action action表示控制层,用于接收请求,并返回数据或转发页面
- service service表示业务逻辑层,用于处理事务
- action接收请求后,会调用service层的函数做事务处理,代码逻辑的实现就写在service层
- dao dao表示持久层,用来写sql
- service层需要用到数据库数据时,会调用dao层的sql接口
- view view包下写实体类,就是有 set/get 函数的类。封装dao层取出来的数据为一个对象
B:后台页面目录
在src\main\webapp\WEB-INF下创建一个目录
(该目录的名字随意取,该目录下存放页面文件
WEB-INF目录是不可见的,所以其下的文件是安全的
一般来讲存放的是需要登录后才可以访问的后台管理页面)
C:静态资源目录
在src\main\webapp下创建一个目录
该目录的名字随意取,该目录下存放前端样式文件
D:默认首页
在src\main\webapp下创建一个 index.jsp 文件
4,文件配置
A:数据库
在src\main\resources目录下创建数据库连接配置文件 db.properties(new一个file)
填写配置内容
oracle、mysql、sql server,3者的写法均不同变量名可以随意取,但是要注意,不能和系统内置的变量名起冲突等号右边的内容,不要加双引号或单引号
Mysql的配置
jdbc.driver= com.mysql.jdbc.Driver;//数据库驱动
jdbc.url =jdbc:mysql://localhost:3306/blog?characterEncoding=utf-8 //数据库地址(一般写ip地址)
jdbc.username = root //数据库用户名 jdbc.password =root //数据库密码
jdbc.driver= com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql://localhost:3306/blog?characterEncoding=utf-8
jdbc.username = root
jdbc.password =root
B:spring-mybatis
在src\main\resources目录下创建spring-mybatis整合配置文件 spring-mybatis.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <!-- 引入数据库配置文件 -->
<!-- <util:properties id="config"
></util:properties> -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据连接参数及连接池 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- spring 集成mybaits 所以不用配置mybatis -->
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 映射文件的位置 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 分页插件pageHelper -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean> <!-- 该bean负责调用sqlsession的getMaper函数 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.junge.dao*"/>
<!-- -->
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property> -->
</bean>
<!-- 开启事务注解驱动 -->
<tx:annotation-driven/>
<!-- 事务管理 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>
C:spring-mvc
在src\main\resources目录下创建spring-mvc整合配置文件 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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <!-- 启用注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 处理静态资源 -->
<mvc:default-servlet-handler />
<!-- 开启组件扫描 -->
<context:component-scan
base-package="com.junge.*" />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 拦截器 -->
<mvc:interceptors>
<!-- 多个拦截器顺序执行
登录认证拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.junge.interceptor.Interceptor" />
</mvc:interceptor>
</mvc:interceptors> <!--全局异常处理 只要你实现了HandelExceptionResolver接口 , 这个类就是一个全局异常处理类 -->
<bean class="com.junge.exception.JExceptionResolver"></bean>
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置默认编码 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 上传文件总的大小最大为5M -->
<property name="maxUploadSize" value="5242440"></property>
</bean>
</beans>
D:web.xml
找到src\main\webapp\WEB-INF目录下的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>
<!-- 字符过滤,防止post请求乱码 -->
<filter>
<filter-name>SpringCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SpringCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- spring mvc 请求响应 -->
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value><!-- 这么写会自动找到之前写的 spring-mybaits文件和spring-mvc文件 -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<!-- 表示后台控制层接受。action和json结尾的请求 本项目中利用action请求转发页面 json请求转发json数据 -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 配置静态资源文件路径 -->
<servlet-mapping>
<servlet-name>default</servlet-name><!-- 表示static目录下的文件都是静态文件 -->
<url-pattern>/static/*</url-pattern><!-- 不配置的话,框架会认为这些文件的路径都是请求,导致无法访问 -->
</servlet-mapping> <!-- 配置错误页面 -->
<error-page>
<error-code>403</error-code>
<location>/WEB-INF/page/portal/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/page/portal/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/page/portal/500.jsp</location>
</error-page>
</web-app>
E:mybatis-config.xml
用来分页的配置文件(pagehelper)
<?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>
<!-- 引入分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 分页参数合理化 -->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
</configuration>
5,访问测试
A:准备数据库数据
创建一个user表(oracle数据库里面user是关键字)
表数据
B:实体类(view)
在之前创建好的view包中创建User实体类
package com.ssm.view; public class User {
private int user_id;
private String user_name;
private String user_password;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
} }
C:dao层(接口)
在之前创建好的dao包中创建UserDao接口 并写登录的方法
package com.ssm.dao; import org.apache.ibatis.annotations.Param; import com.ssm.view.User; public interface UserDao {
/**
* 登录操作
* @param user_name 用户名
* @param user_password 密码
* @return
*/
public User login(@Param("user_name")String user_name,@Param("user_password")String user_password);
}
在src/main/resources/路径下创建mapper文件夹 用来放mybatis dao的配置文件
创建xml文件 名称为UserDaomapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.ssm.dao.UserDao"><!-- 对应的dao包的类 -->
<!-- 建立sql查询结果字段与实体属性的映射关系 -->
<resultMap id="UserMap" type="com.ssm.view.User"><!-- 对应的实体类的路径 -->
<result column="user_id" property="user_id" /> <result column="user_name" property="user_name" />
<result column="user_password" property="user_password" />
</resultMap> <select id="login" resultMap="UserMap">
SELECT
*
FROM
user
WHERE
user_name=#{user_name} and user_password=#{user_password}
</select> </mapper>
D:service层
在之前创建好的service包中创建UserService类 写登录的方法
package com.ssm.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.ssm.dao.UserDao;
import com.ssm.view.User; @Service("userService")
public class UserService {
@Autowired
private UserDao userDao;
/**
* 登录操作
* @param user_name
* @param user_password
* @return
*/
public User login(String user_name,String user_password) {
return userDao.login(user_name, user_password);
}
}
E:准备页面(登录)
之前创建的index页面
<%@ page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="#" method="post">
账号:<input type="text" id="user_name" name="user_name"><br>
密码:<input type="password" id="user_password" name="user_password"><br>
<input type="submit" value="login">
</form>
</body>
</html>
在创建登录成功后的页面
<%@ page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
hello
</body>
</html>
F:控制层(action/controller)
在之前创建好的action包里创建一个UserAction类
G:访问(省略)