ssm框架的整合(非常详细的讲解)(仅供参考)
ssm的意思是Spring+SpringMVC+MyBatis的整合,我是用的工具为idea,在Maven基础上整合的
一.引入相关jar包
下面展示一些 jar包代码片
<!--此jar包会自动引入spring的核心jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--AOP相关jia包-->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<!--mybatis包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!--springmvc包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!--若要想使用jsp页面作为前端,可引入jstl包简化开发-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--若要使用jstl和el表达式必须引入该包-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--文件上传相关包-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<!--数据库与spring连接的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--JSON包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!--引入日志文件-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!--mybatis与spring的整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
数据库表
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT ,
`username` varchar(255) NULL ,
`password` varchar(255) NULL ,
PRIMARY KEY (`id`)
);
创建框架结构
切记:mapper映射文件一定要一层一层创建,并且要和mapper层目录保持一致
创建映射目录如下
创建结构如下
配置文件xml
spring配置文件
spring配置文件中融合了mybatis部分内容
若是spring配置文件没有融合的情况下,spring配置文件无需扫描controller层包,mybatis配置文件中需要扫controller层包,避免重复扫包,以免后续报错
<?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/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 https://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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描注解所在的包 -->
<context:component-scan base-package="com.bc.service,com.bc.pojo,com.bc.mapper"></context:component-scan>
<!--开启注解-->
<context:annotation-config></context:annotation-config>
<!--加载properties文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--加载datasource数据源-->
<bean id="basicDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.drive}"></property>
<property name="url" value="${jdbc.url}"></property>
</bean>
<!--创建sqlsession工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="basicDataSource"></property>
<!--指定mybatis配置文件位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!--指定spring自动扫描的mapper包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bc.mapper"></property>
</bean>
<!--创建事务Bean-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="basicDataSource"></property>
</bean>
<!--开启事务注解-->
<tx:annotation-driven></tx:annotation-driven>
<!--配置事务切面-->
<tx:advice transaction-manager="transactionManager" id="transactionInterceptor"></tx:advice>
<!--开启aop注解-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
db.properties
mysql版本较高的需要配置useSSL=true
jdbc.drive=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bc?useSSL=true
jdbc.username=root
jdbc.password=123456
mybatis配置文件
1:驼峰式:下划线到驼峰式命名法的自动匹配
2:给类起别名开启后,在mapper映射文件中返回类型无需写全路径,只写类名即可
3:延迟加载,开启后当进行一对多或着多对一等,不调用不加载
代码片
<?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="mapUnderscoreToCamelCase" value="true"/>
<!--配置日志-->
<setting name="logImpl" value="log4j"/>
<!--开启多对象关联查询的延迟加载功能-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--设置是否开启二级缓存 默认为true-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--给类起别名-->
<typeAliases>
<package name="com.bc.pojo"/>
</typeAliases>
<plugins>
<!--分页-->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
log4j.properties
# 全局日志配置
log4j.rootLogger=DEBUG, stdout
# MyBatis 日志配置
log4j.logger.com.bc.mapper=DEBUG
# 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
springmvc配置文件
若不是前后端分离,需要进行处理静态资源,不然将会被拦截
系统默认是许可jsp页面加载的
<?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="com.bc.controller"></context:component-scan>
<!--添加mvc注解驱动-->
<mvc:annotation-driven>
<!--处理返回json乱码-->
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
<value>text/html;charset=utf-8</value>
<value>text/plain;charset=utf-8</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--处理静态资源-->
<mvc:resources mapping="/static/**" location="/static/"></mvc:resources>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml配置文件
jdk8虽然处理了get请求的乱码问题,但post还仍未解决
下面展示一些 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-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContect.xml</param-value>
</context-param>
<!--配置监听器 必要配的,不然将会报错-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端适配器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化时加载mvc配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--处理post请求乱码-->
<filter>
<filter-name>encoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
pojo层中User类
pojo类要实现序列化接口,get,set,无参有参构造我就不拷贝了
public class User implements Serializable {
private Integer id;
private String username;
private String password;
}
mapper层中UserMapper接口
public interface UserMapper {
List<User> listAll();
}
service层
//接口
public interface UserService {
List<User> listALL();
}
//实现类
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> listALL() {
return userMapper.listAll();
}
}
controller层
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("listAll")
@ResponseBody
public String listAll(){
List<User> list= userService.listALL();
return JSON.toJSONString(list);
}
}
UserMapper接口的映射文件
namespace="com.bc.mapper.UserMapper"
命名空间要和你的接口路径一致
<?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="com.bc.mapper.UserMapper">
<!--List<User> listALL();-->
<select id="listAll" resultType="User">
select * from user ;
</select>
</mapper>
接着配置tomcat,然后跑一下,看能不能跑成功
跑项目时记得检查下看jar包是否添加上了
添加后如下
接着检查路径是否相同
接着跑一下项目试试
成功后页面会自动跳转
紧接着我们进行测试
在这里我们看到有数据返回
然后tomcat上会有日志打印
end