摘要
在学习ssm框架搭配的过程中,碰到一些问题,于是想记录下来…
Maven项目的创建
1.勾选Create a simple project,点击next.
2.输入Group Id,Artifact Id 以及选择packaging.点击finish.
3.单击项目右键,选择properties for ssm之后,单击project facets,将java改成1.8(看自己电脑jdk版本是多少就选择多少),取消勾选dynamic web module,点击 apply.接下来将dynamic web module 的version改为3.1,并且勾选就会出现Futher configuration availiable选项…
4.点击Futher configuration availiable选项,勾选Generate web.xml ,点击OK…然后在点击Apply and Close.
5.至此,我们的Maven项目就算是真这个的创建好了.
SSM框架的搭建
1.在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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dhcc</groupId>
<artifactId>ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 指定maven编译方式为jdk1.8版本 -->
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<dependencies>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<!-- Spring Web -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<!-- Spring SpringMVC -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.3.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.3.RELEASE</version>
</dependency>
<!-- Spring Aspects -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<!-- MyBatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- MyBatis Spring -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- C3P0 -->
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- MySQL驱动包 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!-- JSTL -->
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</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>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
</project>
2.在Web.xml中启动Spring容器以及加载SpringMVC拦截器,拦截所有请求
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1"
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_3_1.xsd">
<!-- 1.启动Spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2.加载springmvc前端控制器,拦截所有请求 -->
<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping
</web-app>
3.加载springMVC配置文件,SpringMVC只负责前端页面跳转的逻辑处理.
其余所有和业务逻辑有关的配置都配置在Spring配置文件中.
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 1.启动注解扫描功能 -->
<context:component-scan base-package="com.dhcc"
use-default-filters="false">
<!-- 只扫描控制器 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 2.配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 两个标准配置 -->
<!-- 3.处理静态资源 -->
<mvc:default-servlet-handler/>
<!-- 4.支持springmvc高级功能... JSR303校验... -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
4.加载Spring配置文件,这里处理配置和业务逻辑有关的,例如数据源,事务管理等等…注意Spring在配置注解扫描器的时候时候默认的过滤器,即use-default-filter = true,否则就不能扫描到@Service等注解…
<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="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-4.2.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">
<!-- 1.配置注解扫描器 -->
<!-- !!!=====遇到的问题,Spring配置文件中的注解扫描器使用默认的过滤器,即use-default-filter=true====!!! -->
<context:component-scan base-package="com.dhcc">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- Spring的配置文件,主要配置和业务逻辑有关的 -->
<!-- ====================数据源、事务管理、×××===================== -->
<!-- 加载配置文件 -->
<context:property-placeholder
location="classpath:dbconfig.properties" />
<!-- 2.配置数据源 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3.配置事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.开启基于注解的事务 -->
<tx:annotation-driven
transaction-manager="transactionManager" />
<!-- 5.配置SqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- configLocation指定全局配置位置 -->
<property name="configLocation"
value="classpath:mybatis-config.xml"></property>
<!-- mapperLocations指定映射文件的位置 -->
<property name="mapperLocations"
value="classpath:mapper/user/UserInfoDao.xml"></property>
</bean>
<!-- 6.扫描所有的mapper接口,让这些mapper能够自动注入 -->
<!-- <mybatis-spring:scan base-package="com.dhcc.dao"/> -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dhcc.dao"></property>
</bean>
</beans>
5.编写映射文件以及接口,映射文件中的namespace对应接口的全类名,select中的id对应接口的方法名,这样映射文件就会和接口进行动态绑定,将所有的sql都放在映射文件中,在接口的作用在于规范sql的入参以及返回类型.
<?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.dhcc.dao.UserInfoDao">
<!-- public UserInfo getUser(@Param("userName") String userName, @Param("password")
String password); -->
<select id="getUser" resultType="com.dhcc.pojo.UserInfo">
select * from user_info
where userName = #{userName} and password = #{password}
</select>
</mapper>
package com.dhcc.dao;
import org.apache.ibatis.annotations.Param;
import com.dhcc.pojo.UserInfo;
/**
* @author: 241江斌
* @since: 2020年7月9日 下午6:48:24
* @version: 1.0.0
*/
public interface UserInfoDao {
public UserInfo getUser(@Param("userName") String userName, @Param("password") String password);
}
6.编写Service层和Controller层,
package com.dhcc.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dhcc.dao.UserInfoDao;
/**
* @author: 241江斌
* @since: 2020年7月9日 下午7:20:38
* @version: 1.0.0
*/
import com.dhcc.pojo.UserInfo;
@Service("userInfoService")
public class UserInfoServiceImp implements UserInfoService {
@Autowired
private UserInfoDao userInfoDao;
@Override
public UserInfo login(String userName, String password) {
return userInfoDao.getUser(userName, password);
}
}
package com.dhcc.controller;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dhcc.pojo.UserInfo;
import com.dhcc.service.UserInfoService;
/**
* @author: 241江斌
* @since: 2020年7月9日 下午7:26:26
* @version: 1.0.0
*/
@RequestMapping("/userinfo")
@Controller
public class UserInfoController {
@Autowired
private UserInfoService userInfoService;
@RequestMapping("/login")
public String login(UserInfo ui) {
UserInfo tempUi = userInfoService.login(ui.getUserName(), ui.getPassword());
if (tempUi != null && tempUi.getUserName() != null) {
return "index";
} else {
return "redirect:/login.jsp";
}
}
}
7.接下来写个页面进行测试,看系统是否能跑通…
<form action="userinfo/login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="登录" /></td>
<td></td>
</tr>
</table>
</form>
8.至此,可以看见测试成功了,到这里项目也就结束了.
9.项目很简单,全部文件也就这么多…