Spring+Spring MVC+myBatis整合(使用maven)
ssm整合报各种异常,看了众多帖子,都是零零散散,浪费时间,所以自己写了一篇,内含详细目录以及所有代码,无尿点爽文!
一.搭建环境 1.IDEA 2019 2.Maven 3.Spring+Spring MVC+myBatis 4.MySQL,表名为login(id,name,password)三个字段 5.目录结构图(使用的都是login部分)
二.导入Maven依赖
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>org.example</groupId>
<artifactId>ssmDay1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssmDay1 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!-- spring 相关版本 -->
<spring.version>5.3.2</spring.version>
<!-- json 相关版本 -->
<json.version>2.9.0</json.version>
<!-- mybatis 相关版本 -->
<mybatis.version>3.5.5</mybatis.version>
<!-- mybatis 整合 spring 相关版本 -->
<mybatis-spring.version>2.0.6</mybatis-spring.version>
<!-- druid 相关版本 -->
<druid.version>1.2.4</druid.version>
<!-- c3p0 相关版本 -->
<c3p0.version>0.9.5.2</c3p0.version>
<!-- log4j 相关版本 -->
<log4j.version>2.14.0</log4j.version>
<!-- slf4j 相关版本 -->
<slf4j.version>1.7.30</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 导入 servlet 依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 导入 jsp 依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- 导入 jstl 依赖 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- mysql连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 导入 spring 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 导入 springMVC 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring 事务控制依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring 对于 jdbc或mybatis的事务控制 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring 单元测试依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 处理 json 数据相关依赖,springMVC默认
处理 json 依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${json.version}</version>
</dependency>
<!-- 文件下载插件,解析 MIME 类型描述的正文 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- mybatis 依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis 整合 spring 依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!-- druid 数据库连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- c3p0 数据库连接池依赖 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<!-- log4j 日志 -->
<dependency>
<groupId>org.apache.logging.log4j
</groupId
>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>***.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>***.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<finalName>ssmDay1</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
三.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:conf/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端控制器-->
<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:conf/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 乱码解析-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>
四.spring配置文件
1.applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--组件扫描 service和mapper-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<context:component-scan base-package="com.keep.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--连接数据库的必备信息-->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:conf/mybatis.xml"></property>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.keep.ssm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
五.Spring-mvc.xml配置文件
1.dispatcherServlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描-->
<context:component-scan base-package="com.keep.ssm.controller"/>
<!-- 开启注解-->
<mvc:annotation-driven/>
<!-- 视图解析-->
<bean id="resourceViewResolve" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--文件multipart解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 字符集编码-->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 上传文件总大小-->
<property name="maxUploadSize" value="10240"></property>
<!-- 上传单个文件大小-->
<property name="maxUploadSizePerFile" value="10240"></property>
</bean>
<!-- 静态资源访问权限-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
六.mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-
Config 3.0
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="conf/jdbc.properties"></properties>
<!--<typeAliases>-->
<!-- <typeAlias type="com.peng.ssm.domain.login" alias="login"/>-->
<!-- <!–扫包–>-->
<!--<!– <package name="com.peng.ssm.domain"/>–>-->
<!-- <typeAlias type="com.peng.ssm.domain.employee" alias="employee"/>-->
<!--</typeAliases>-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.keep.ssm.dao"/>
</mappers>
</configuration>
实体类login.class
1.自动生成get/set方法,提供toString。内含如下字段: private Integer id; private String name; private String password;
七.login.jsp
<form class="form" action="${pageContext.request.contextPath}/user/login" method="post">
<label>用户名:</label>
<input type="text" name="name"/><br/>
<label>密 码:</label>
<input type="text" name="password"/><br/>
<button type="submit">登录</button><br/>
<a href="/register.jsp">点击前往注册</a>
</form>
*七.接口实现类的编写
1.sevice接口以及实现类
public interface loginService {
login logins(login login);
}
@Service("loginService")
public class loginServiceImpl implements loginService {
@Autowired
private loginDao loginDao;
@Override
public login logins(login login) {
return loginDao.logins(login);
}
}
2.dao层接口
public interface loginDao {
login logins(login login);
}
八.映射文件
1.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">
<mapper namespace="com.keep.ssm.dao.loginDao">
<select id="logins" parameterType="com.keep.ssm.domain.login" resultType="com.keep.ssm.domain.login">
select * from login where name=#{name} and password=#{password}
</select>
</mapper>
九.Controller层代码
1.loginController.class
@Controller
@RequestMapping("/user")
public class loginController {
@Resource
private loginService loginService;
@Resource
private employeeService employeeService;
@RequestMapping(value = "/login", produces = "text/html;charset=UTF-8")
public ModelAndView loginUser(login login, Model model) {
login user = loginService.logins(login);
ModelAndView mv = new ModelAndView();
if (user != null) {
mv.addObject("userList",user);
mv.setViewName("LoginSuccess");
return mv;
}
mv.setViewName("error");
return mv;
}
总结:整合的过程中会出现各种各样的异常,梳理如下心得(一般都是配置文件的问题): 1.配置文件路径要仔细。 2.注解不要遗漏。 3.数据库字段对应问题。 好啦,希望本篇能够帮助到您,编写不易,您的点赞是我编写的动力!
|