赞
踩
这是一个相对比较详细的***SSM***整合案例,希望能给您提供帮助。
<?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>ssm</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ssm 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> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 日志--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.23</version> </dependency> <!-- 配置spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.6</version> </dependency> <!-- mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency> <!-- fast json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.3</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.11.3</version> </dependency> </dependencies> <!-- 静态资源过滤--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://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 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--加载properties文件--> <context:property-placeholder location="classpath:database.properties"/> <!-- 配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="${jdbc.initialSize}"/> <property name="maxActive" value="${jdbc.maxActive}"/> <property name="maxWait" value="${jdbc.maxWait}"/> </bean> <!--配置连接工厂--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:mapper/**/*.xml"/> </bean> <!-- 自动搜索mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.sun.liu.dao"/> </bean> <!--spring配置文件开始--> <!--配置组件扫描--> <context:component-scan base-package="com.sun.liu.service"/> <!--配置事务管理器,AOP面向切面编程, 切面=切入点+通知--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--事务的控制,也需要数据源的注入--> <property name="dataSource" ref="dataSource"/> </bean>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/users?characterEncoding=utf-8
jdbc.username=root
# 初始化连接数量
jdbc.initialSize=5
# 最大连接数
jdbc.maxActive=10
# 最大超时时间
jdbc.maxWait=3000
log4j.rootLogger=INFO,Console,File log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n log4j.appender.File = org.apache.log4j.RollingFileAppender log4j.appender.File.File = logs/ssm.log log4j.appender.File.MaxFileSize = 10MB log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
spring-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!--配置组件扫描--> <context:component-scan base-package="com.sun.liu.controller"/> <!--配置处理器,映射器--> <mvc:annotation-driven> </mvc:annotation-driven> <!--释放静态资源--> <mvc:default-servlet-handler/> <!--配置视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 显示日志信息 --> <setting name="logImpl" value="LOG4J"/> <!-- 驼峰--> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--reasonable:分页合理化参数,默认值为false。 当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。 默认false 时,直接根据参数进行查询。--> <property name="reasonable" value="true"/> </plugin> </plugins> </configuration>
package com.sun.liu.controller; import com.alibaba.fastjson.JSONObject; import com.sun.liu.enums.LoingEnums; import com.sun.liu.pojo.User; import com.sun.liu.service.LoginService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Map; import java.util.UUID; /** * @version 1.0 * @date 2022/11/8 11:40 */ @Controller public class LoginController { @Autowired private LoginService loginService; @GetMapping("/login") public String login(){ return "login"; } @GetMapping("/index") public String index(){ return "index"; } @RequestMapping("/tologin") @ResponseBody private JSONObject tologin(User user){ System.out.println("---------------------"+user.toString()); JSONObject jsonObject = new JSONObject(); User login = loginService.login(user); System.out.println(login.toString()); if(null!=login){ jsonObject.put("code", LoingEnums.LOGIN_SUCCESS.getCode()); jsonObject.put("msg",LoingEnums.LOGIN_SUCCESS.getMsg()); return jsonObject; } jsonObject.put("code", LoingEnums.LOGIN_ERROR.getCode()); jsonObject.put("msg",LoingEnums.LOGIN_ERROR.getMsg()); return jsonObject; } }
package com.sun.liu.service; import com.sun.liu.pojo.User; /** * @version 1.0 * @date 2022/11/8 11:44 */ public interface LoginService { User login(User user); } package com.sun.liu.service; import com.sun.liu.dao.LoginDao; import com.sun.liu.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @version 1.0 * @date 2022/11/8 11:44 */ @Service public class LoginServicesImps implements LoginService{ @Autowired private LoginDao loginDao; @Override public User login(User user) { return loginDao.login(user); } }
package com.sun.liu.dao;
import com.sun.liu.pojo.User;
/**
* @author liu qiang
* @version 1.0
* @date 2022/11/8 11:41
*/
public interface LoginDao {
//登录
User login(User user);
}
package com.sun.liu.pojo; /** * @version 1.0 * @date 2022/11/8 11:41 */ public class User { private Integer id; private String username; private String password; public User() { } public User(Integer id, String username, String password) { this.id = id; this.username = username; this.password = password; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
<?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.sun.liu.dao.LoginDao">
<resultMap id="Manages" type="com.sun.liu.pojo.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</resultMap>
<select id="login" resultType="com.sun.liu.pojo.User">
select * from user where username=#{username} and password=#{password};
</select>
</mapper>
<?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核心文件监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--重新制定配置文件的路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--springMvc的核心servlet--> <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.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> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<%-- Created by IntelliJ IDEA. User: SR-SF Date: 2022/11/8 Time: 12:04 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>SSM简易项目搭建</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/lib/layui-v2.5.5/css/layui.css" media="all"> </head> <style> html, body {width: 100%;height: 100%;overflow: hidden} body {background: url("${basePath}static/images/bg.jpg" ) } body:after {content:'';background-repeat:no-repeat;background-size:cover;-webkit-filter:blur(3px);-moz-filter:blur(3px);-o-filter:blur(3px);-ms-filter:blur(3px);filter:blur(3px);position:absolute;top:0;left:0;right:0;bottom:0;z-index:-1;} .layui-container {width: 100%;height: 100%;overflow: hidden} .admin-login-background {width:450px;height:360px;position:absolute;left:50%;top:30%;margin-left:-180px;margin-top:-100px;} .logo-title {text-align:center;letter-spacing:2px;padding:14px 0;} .logo-title h1 {color:#1E9FFF;font-size:25px;font-weight:bold;} .login-form {background-color:#fff;border:1px solid #fff;border-radius:3px;padding:14px 20px;box-shadow:0 0 8px #eeeeee;} .login-form .layui-form-item {position:relative;} .login-form .layui-form-item label {position:absolute;left:1px;top:1px;width:38px;line-height:36px;text-align:center;color:#d2d2d2;} .login-form .layui-form-item input {padding-left:36px;} .captcha-img img {height:34px;border:1px solid #e6e6e6;height:36px;width:100%;} </style> </head> <body> <div class="layui-container"> <div class="admin-login-background"> <div class="layui-form login-form"> <form class="layui-form"> <div class="layui-form-item logo-title"> <h1>SSM简易项目搭建</h1> </div> <div class="layui-form-item"> <label class="layui-icon layui-icon-username"></label> <input type="text" name="username" lay-verify="required" placeholder="用户名" autocomplete="off" class="layui-input" value="admin"> </div> <div class="layui-form-item"> <label class="layui-icon layui-icon-password"></label> <input type="password" name="password" lay-verify="required" placeholder="密码" autocomplete="off" class="layui-input" value="admin"> </div> <div class="layui-form-item"> <button class="layui-btn layui-btn layui-btn-normal layui-btn-fluid" lay-submit="" lay-filter="login">登录</button> </div> </form> </div> </div> </div> <script src="${pageContext.request.contextPath}/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script> <script src="${pageContext.request.contextPath}/static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script> <script src="${pageContext.request.contextPath}/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script> <script> layui.use(['form','jquery'], function () { var form = layui.form, $ = layui.jquery, layer = layui.layer; // 登录过期的时候,跳出ifram框架 if (top.location != self.location) top.location = self.location; // 进行登录操作 form.on('submit(login)', function (data) { $.ajax({ url:"tologin", type:"POST", dataType:'json', data:data.field, success:function(data){ if(data.code == "1000"){ location.href = "index"; }else{ layer.msg(data.msg,{time:2000}); } } }); return false; }); }); </script> </body> </html>
本来打算放阿里云的,但是压缩文化无法分享,现在放百度网盘的链接了,不是会员的可能下载有点慢。
链接:https://pan.baidu.com/s/1S-GgvOP2qXMmV1JVrRp1CA
提取码:8412
以上就是SSM项目整合的简易案例,我写的比较详细,刚开始整合的小伙伴可以跟着看一下,不合适的地方也可以指出来,谢谢大家。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。