在进行整合之前,首先了解这个框架的作用
Mybaties:
丰富的标签库,可写动态sql,并统一的在.XML文件中编写,方便统一管理,解耦
SpringMVC:
标准的MVC思想(mode,view,controller),使代码的耦合性降低,方便实现ioc,方便代码的维护
Spring:
Spring的核心思想是IOC和AOP,ioc使代码耦合性低,便于维护。Aop方便事物和日志等管理。
aop:面向切面编程,横切关注点,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。主要功能是将程序中多个模块的事物日志等抽出来统一管理,使程序员专注开发,不需要管理其它的事情。
ioc:控制反转,将实例化对象的权利翻转了,原来new对象需要用户通过代码new,现在将权利交给spring容器来new。
项目整体结构:
1.首先在pom.xml中建立相关依赖
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>ssmdemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>capital Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <java-version>1.8</java-version> <junit-version>4.8.2</junit-version> <org.springframework-version>4.1.3.RELEASE</org.springframework-version> <!-- <jackson-mapper-asl-version>1.9.9</jackson-mapper-asl-version> <jackson-core-asl-version>1.9.9</jackson-core-asl-version> --> <mysql-connector-java-version>5.1.8</mysql-connector-java-version> <org.mybatis-version>3.2.7</org.mybatis-version> <org.mybatis-spring-version>1.2.2</org.mybatis-spring-version> <opensymphony-version>2.4.2</opensymphony-version> <freemarker-version>2.3.9</freemarker-version> <c3p0-version>0.9.1.2</c3p0-version> <commons-collections-version>1.0</commons-collections-version> <commons-fileupload-version>1.2.2</commons-fileupload-version> <org.apache.commons-version>3.1</org.apache.commons-version> <commons-codec-version>1.6</commons-codec-version> <dom4j-version>1.6.1</dom4j-version> <javax.servlet-version>1.2</javax.servlet-version> <aspectjweaver-version>1.6.6</aspectjweaver-version> <slf4j-log4j12-version>1.6.6</slf4j-log4j12-version> <log4j-version>1.2.16</log4j-version> <javax.servlet-jsp-version>2.0</javax.servlet-jsp-version> <cglib-version>2.2.2</cglib-version> <slf4j-api-version>1.6.6</slf4j-api-version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- with junit4.8.2 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit-version}</version> <type>jar</type> </dependency> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.4.6</version> </dependency> <!-- with spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument-tomcat</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.1.0</version> </dependency> <!--freemarker --> <dependency> <groupId>freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker-version}</version> </dependency> <!-- with mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${org.mybatis-version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${org.mybatis-spring-version}</version> </dependency> <!-- jdbc driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java-version}</version> </dependency> <!-- connect pool with c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>${c3p0-version}</version> </dependency> <!-- apache commons jar --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>${commons-collections-version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload-version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${org.apache.commons-version}</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${commons-codec-version}</version> </dependency> <!-- analyze xml use dom4j --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>${dom4j-version}</version> </dependency> <!-- about servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${javax.servlet-version}</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!-- memcached --> <dependency> <groupId>com.google.code.maven-play-plugin.spy</groupId> <artifactId>memcached</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>${cglib-version}</version> </dependency> <dependency> <groupId>net.fckeditor</groupId> <artifactId>java-core</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20131018</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18.1</version> </dependency> <dependency> <groupId>jsptags</groupId> <artifactId>pager-taglib</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- 资源文件拷贝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/capital</path> <port>8080</port> <!-- tomcat设置utf-8转码 --> <uriEncoding>utf-8</uriEncoding> </configuration> </plugin> </plugins> </build> </project>
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssmdemo</display-name> <!-- 加载spring配置文件 --> <!-- 配置spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <!-- spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springmvc servlet --> <servlet> <servlet-name>springmvcservlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc_servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvcservlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3.在resources下配置springmvc_servlet.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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- spring扫描 --> <context:component-scan base-package="org.lpf"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置springmvc视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean id="stringHttpMessage" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/plain;charset=UTF-8</value> </list> </property> </bean> </beans>
4.在resources下配置application-context.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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置spring扫描 --> <context:component-scan base-package="org.lpf"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--2 c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm_demo"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!-- mybaties配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:org/lpf/Mapper/*.xml"></property> <property name="typeAliasesPackage" value="org.lpf.entity.User"></property> </bean> <!-- mybatie 扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.lpf.dao"></property> </bean> <!-- spring事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
5.在java下新建包
6.在entity包下新建实体类User.java
package org.lpf.entity;
/**
* @author user:11963
* @version date:2017年12月12日 下午8:56:29
*
*/
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
7.在mapper包下新建UserMapper.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="org.lpf.dao.UserDao"> <!-- 登录 --> <select id="dologin" parameterType="org.lpf.entity.User" resultType="org.lpf.entity.User"> select * from user where name=#{name} and password=#{password} </select> </mapper>
8.在dao包下新建UserDao.java
package org.lpf.dao;
/**
* @author user:11963
* @version date:2017年12月12日 下午8:58:08
*
*/
import org.lpf.entity.User;
public interface UserDao {
public User dologin(User user);
}
9.在service下新建UserService.java
package org.lpf.service;
import org.lpf.entity.User;
/**
* @author user:11963
* @version date:2017年12月12日 下午9:09:26
*
*/
public interface UserService {
public User dologin(User user);
}
10.在impl包下新建UserServiceImpl.java
package org.lpf.service.impl;
import org.lpf.dao.UserDao;
import org.lpf.entity.User;
import org.lpf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author user:11963
* @version date:2017年12月12日 下午9:10:52
*
*/
@Service
@Transactional
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public User dologin(User user) {
// TODO Auto-generated method stub
return this.userDao.dologin(user);
}
}
11.在controller在新建UserController.java
package org.lpf.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.lpf.entity.User;
import org.lpf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author user:11963
* @version date:2017年12月12日 下午9:21:02
*
*/
@Controller
@RequestMapping("/user/")
public class UserController {
@Autowired(required=true)
private UserService service;
public UserController() {
// TODO Auto-generated constructor stub
}
@RequestMapping("login")
public String login(User user,HttpServletRequest request,HttpSession session){
System.out.print("!!!!!");
User users=new User();
users=service.dologin(user);
return "index";
}
}
12.编写jsp:
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <head> <meta charset="UTF-8"> <title>欢迎登录</title> <script src='http://www.5iweb.com.cn/statics/js/jquery.1.7.1.min.js'></script> </body> <style type="text/css"> #code { font-family: Arial; font-style: italic; font-weight: bold; border: 0; letter-spacing: 2px; color: blue; } </style> <script type="text/javascript"> //设置一个全局的变量,便于保存验证码 var code; function createCode() { //首先默认code为空字符串 code = ''; //设置长度,这里看需求,我这里设置了4 var codeLength = 4; var codeV = document.getElementById('code'); //设置随机字符 var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //循环codeLength 我设置的4就是循环4次 for (var i = 0; i < codeLength; i++) { //设置随机数范围,这设置为0 ~ 36 var index = Math.floor(Math.random() * 36); //字符串拼接 将每次随机的字符 进行拼接 code += random[index]; } //将拼接好的字符串赋值给展示的Value codeV.value = code; } //下面就是判断是否== 的代码,无需解释 function validate() { var oValue = document.getElementById('input').value.toUpperCase(); if (oValue == 0) { alert('请确认是否输入正确!'); window.location.reload(); //window.history.go(-1); //window.history.go(0); //window.navigate(location) event.preventDefault(); return true; } else if (oValue != code) { alert('验证码不正确,请重新输入!'); window.location.reload(); /* window.history.go(-1); window.history.go(0); window.navigate(location) */ event.preventDefault(); return true; } else { alert('登陆成功,感谢您的使用!'); $("#form1").submit(); } } //设置此处的原因是每次进入界面展示一个随机的验证码,不设置则为空 window.onload = function() { createCode(); } </script> <script typet="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script> <base href="<%=basePath%>"> <title>用户登录</title> </head> <h1> <i class="icon-leaf green"></i> <span class="red">SSM(SpringMVC+Spring+Mybaties)整合</span> <span class="white">Demo</span> </h1> <h4 class="header blue lighter bigger"> <i class="icon-coffee green"></i> 请输入您的账号和密码 </h4> <form action="user/login.do" method="post" onsubmit="return check()" name="form1" id="form1"> <fieldset> <label class="block clearfix"> <span class="block input-icon input-icon-right"> <input id="userId" name="name" type="text" class="form-control" placeholder="请输入账号" /> <i class="icon-user"></i><br> </span> </label> <label class="block clearfix"> <span class="block input-icon input-icon-right"> <input id="userPw" name="password" type="password" class="form-control" placeholder="请输入密码" /> <i class="icon-lock"></i> </span> </label> <div class="clearfix"> <input type="text" id="input" value="" /> <input type="button" id="code" onclick="createCode()" /> <br> <input type="submit" class="width-35 pull-right btn btn-sm btn-primary" class="icon-key" value="登陆" onclick="validate()" /> </div> </fieldset> </form> </body> </html>
index.jsp
<html> <body> <h2>Hello World!</h2> </body> </html>
框架搭建完成,下面会有下载的地址,拿去用的同学记得改一下c3p0链接池的数据库登录信息
链接: https://pan.baidu.com/s/1slA2pjv 密码: ths2 (包含项目和sql脚本)