【WEB应用】SSM框架搭建V2.0
1.
提前下载好eclipse,mysql,tomcat9
下载apache-maven-3.6.3,放到C盘。
修改配置文件:C:\apache-maven-3.6.3\conf\settings.xml
apache-maven-3.6.3下新建目录:repository
2.配置eclipse
maven配置:
jdk配置、server配置、jsp配置:
3.创建maven project
4、配置maven project
选中项目,右击鼠标,选中Properties
这步如果提示WebContent已存在之类的报错,可以删除WebContent文件夹,再次尝试。
5.pom.xml、web.xml、springmvc.xml、spring.xml、mybatis-config.xml、jdbc.properties文件的创建.
【如果包名不一样,别忘记修改成自己的项目包名】
内容分别如下:
pom.xml(maven自动根据dependency配置,下载依赖包。)
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.fawiec.etp.ssmprojectgroupId> <artifactId>ssmprojectartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>warpackaging> <dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>5.1.16.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aspectsartifactId> <version>5.1.16.RELEASEversion> dependency> <dependency> <groupId>aopalliancegroupId> <artifactId>aopallianceartifactId> <version>1.0version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>5.1.16.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>5.1.16.RELEASEversion> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.5.4version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>2.0.4version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.49version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>1.1.22version> dependency> <dependency> <groupId>jstlgroupId> <artifactId>jstlartifactId> <version>1.2version> dependency> <dependency> <groupId>org.mybatis.cachesgroupId> <artifactId>mybatis-ehcacheartifactId> <version>1.2.1version> dependency> <dependency> <groupId>org.hibernate.validatorgroupId> <artifactId>hibernate-validatorartifactId> <version>6.0.20.Finalversion> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.10.4version> dependency> <dependency> <groupId>com.github.pagehelpergroupId> <artifactId>pagehelperartifactId> <version>5.1.11version> dependency> <dependency> <groupId>commons-fileuploadgroupId> <artifactId>commons-fileuploadartifactId> <version>1.4version> dependency> <dependency> <groupId>ognlgroupId> <artifactId>ognlartifactId> <version>3.2.13version> dependency> <dependency> <groupId>org.mybatis.generatorgroupId> <artifactId>mybatis-generator-coreartifactId> <version>1.4.0version> dependency> dependencies> project>
web.xml
<?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"> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:spring.xmlparam-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> <servlet> <servlet-name>DispatcherServletservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:springmvc.xmlparam-value> init-param> servlet> <servlet-mapping> <servlet-name>DispatcherServletservlet-name> <url-pattern>/url-pattern> servlet-mapping> <filter> <filter-name>CharacterEncodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF-8param-value> init-param> <init-param> <param-name>forceRequestEncodingparam-name> <param-value>trueparam-value> init-param> <init-param> <param-name>forceResponseEncodingparam-name> <param-value>trueparam-value> init-param> filter> <filter-mapping> <filter-name>CharacterEncodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> <filter> <filter-name>HiddenHttpMethodFilterfilter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class> filter> <filter-mapping> <filter-name>HiddenHttpMethodFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> web-app>
springmvc.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.fawiec.etp.ssmproject.controller"/> <context:annotation-config /> <mvc:default-servlet-handler/> <mvc:annotation-driven validator="validation"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:message"/> bean> <bean id="validation" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <property name="validationMessageSource" ref="messageSource"/> bean> beans>
spring.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx https://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.fawiec.etp.ssmproject"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> context:component-scan> <context:annotation-config/> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.dburl}"/> <property name="username" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="${jdbc.pool.initialSize}"/> <property name="minIdle" value="${jdbc.pool.minIdle}"/> <property name="maxActive" value="${jdbc.pool.maxActive}"/> <property name="maxWait" value="${jdbc.pool.maxWait}"/> bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="druidDataSource"/> bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="druidDataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.fawiec.etp.ssmproject.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> bean> beans>
mybatis-config.xml
<?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="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> <setting name="cacheEnabled" value="true"/> settings> <mappers> <package name="com.fawiec.etp.ssmproject.mapper"/> mappers> configuration>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dburl=jdbc:mysql://localhost:3306/ceshi
jdbc.user=zhangsan
jdbc.password=123456
jdbc.pool.initialSize=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20
jdbc.pool.maxWait=60000
6.写代码
LoginUser.java:
package com.fawiec.etp.ssmproject.domain; public class LoginUser { private String userName;//用户名称 private String loginPwd;//用户登录密码 private Integer userType;//表示用户的类型,如 1:普通用户,2:管理员用户等 public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getLoginPwd() { return loginPwd; } public void setLoginPwd(String loginPwd) { this.loginPwd = loginPwd; } public Integer getUserType() { return userType; } public void setUserType(Integer userType) { this.userType = userType; } @Override public String toString() { return "LoginUser [userName=" + userName + ", loginPwd=" + loginPwd + ", userType=" + userType + "]"; } }
LoginUserMapper.java
package com.fawiec.etp.ssmproject.mapper; import org.apache.ibatis.annotations.Param; import com.fawiec.etp.ssmproject.domain.LoginUser; public interface LoginUserMapper { public LoginUser findUserByNameAndPwd(@Param("userName")String userName,@Param("loginPwd")String loginPwd); }
LoginUserMapper.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.fawiec.etp.ssmproject.mapper.LoginUserMapper"> <select id="findUserByNameAndPwd" resultType="com.fawiec.etp.ssmproject.domain.LoginUser"> select * from tb_loginUser where userName=#{userName} and loginPwd=#{loginPwd} select> mapper>
LoginUserService.java
package com.fawiec.etp.ssmproject.service; import com.fawiec.etp.ssmproject.domain.LoginUser; public interface LoginUserService { public LoginUser findUserByUserNameAndPassword(String userName,String loginPwd); }
LoginUserServiceImpl.java
package com.fawiec.etp.ssmproject.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.fawiec.etp.ssmproject.domain.LoginUser; import com.fawiec.etp.ssmproject.mapper.LoginUserMapper; import com.fawiec.etp.ssmproject.service.LoginUserService; @Service @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = true) public class LoginUserServiceImpl implements LoginUserService{ @Autowired private LoginUserMapper loginUserMapper; public LoginUser findUserByUserNameAndPassword(String userName,String loginPwd) { return loginUserMapper.findUserByNameAndPwd(userName, loginPwd); } }
LoginUserController.java
package com.fawiec.etp.ssmproject.controller; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.fawiec.etp.ssmproject.domain.LoginUser; import com.fawiec.etp.ssmproject.service.LoginUserService; @Controller @RequestMapping(value="/user") public class LoginUserController { @Autowired private LoginUserService userService; @RequestMapping(value="/login",method = RequestMethod.POST) public String login(LoginUser user ,Mapmap) { System.out.println(user); LoginUser loginUser=userService.findUserByUserNameAndPassword(user.getUserName(), user.getLoginPwd()); if(loginUser!=null) { map.put("info", "["+loginUser.getUserName()+"]登录成功!"); }else { map.put("info", "用户名称或密码错误,登录失败!"); } return "login/info"; } }
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title heretitle> head> <body> <h1>用户登录页面h1> <form action="${pageContext.request.contextPath }/user/login" method="post"> 用户名称:<input type="text" name="userName"/> 登录密码:<Input type="password" name="loginPwd"/> <input type="submit" value="用户登录"/> form> body> html>
info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title heretitle> head> <body> <h1>${info }h1> body> html>
7.运行到server上,测试验证
Servers下增加Tomcat9 Server
项目添加BuildPath,增加Server Runtime,选中新建的Tomcat9 Server完成。
项目右键,Run As,然后Run On Server,启动后
输入账号密码,登陆成功: