在进行整合之前,首先了解这个框架的作用
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.0modelVersion> <groupId>comgroupId> <artifactId>ssmdemoartifactId> <packaging>warpackaging> <version>0.0.1-SNAPSHOTversion> <name>capital Maven Webappname> <url>http://maven.apache.orgurl> <properties> <java-version>1.8java-version> <junit-version>4.8.2junit-version> <org.springframework-version>4.1.3.RELEASEorg.springframework-version> <mysql-connector-java-version>5.1.8mysql-connector-java-version> <org.mybatis-version>3.2.7org.mybatis-version> <org.mybatis-spring-version>1.2.2org.mybatis-spring-version> <opensymphony-version>2.4.2opensymphony-version> <freemarker-version>2.3.9freemarker-version> <c3p0-version>0.9.1.2c3p0-version> <commons-collections-version>1.0commons-collections-version> <commons-fileupload-version>1.2.2commons-fileupload-version> <org.apache.commons-version>3.1org.apache.commons-version> <commons-codec-version>1.6commons-codec-version> <dom4j-version>1.6.1dom4j-version> <javax.servlet-version>1.2javax.servlet-version> <aspectjweaver-version>1.6.6aspectjweaver-version> <slf4j-log4j12-version>1.6.6slf4j-log4j12-version> <log4j-version>1.2.16log4j-version> <javax.servlet-jsp-version>2.0javax.servlet-jsp-version> <cglib-version>2.2.2cglib-version> <slf4j-api-version>1.6.6slf4j-api-version> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>${junit-version}version> <type>jartype> dependency> <dependency> <groupId>com.sun.mailgroupId> <artifactId>javax.mailartifactId> <version>1.4.6version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aspectsartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-context-supportartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-txartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-instrumentartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-instrument-tomcatartifactId> <version>${org.springframework-version}version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-coreartifactId> <version>2.1.0version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.1.0version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-annotationsartifactId> <version>2.1.0version> dependency> <dependency> <groupId>freemarkergroupId> <artifactId>freemarkerartifactId> <version>${freemarker-version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>${org.mybatis-version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>${org.mybatis-spring-version}version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>${mysql-connector-java-version}version> dependency> <dependency> <groupId>c3p0groupId> <artifactId>c3p0artifactId> <version>${c3p0-version}version> dependency> <dependency> <groupId>commons-collectionsgroupId> <artifactId>commons-collectionsartifactId> <version>${commons-collections-version}version> dependency> <dependency> <groupId>commons-fileuploadgroupId> <artifactId>commons-fileuploadartifactId> <version>${commons-fileupload-version}version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-lang3artifactId> <version>${org.apache.commons-version}version> dependency> <dependency> <groupId>commons-codecgroupId> <artifactId>commons-codecartifactId> <version>${commons-codec-version}version> dependency> <dependency> <groupId>dom4jgroupId> <artifactId>dom4jartifactId> <version>${dom4j-version}version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>jstlartifactId> <version>${javax.servlet-version}version> dependency> <dependency> <groupId>taglibsgroupId> <artifactId>standardartifactId> <version>1.1.2version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>3.0.1version> <scope>providedscope> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-log4j12artifactId> <version>1.6.6version> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>1.2.14version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-apiartifactId> <version>1.6.6version> dependency> <dependency> <groupId>javax.activationgroupId> <artifactId>activationartifactId> <version>1.1.1version> dependency> <dependency> <groupId>com.google.code.maven-play-plugin.spygroupId> <artifactId>memcachedartifactId> <version>2.4.2version> dependency> <dependency> <groupId>cglibgroupId> <artifactId>cglibartifactId> <version>${cglib-version}version> dependency> <dependency> <groupId>net.fckeditorgroupId> <artifactId>java-coreartifactId> <version>2.6version> dependency> <dependency> <groupId>org.jsongroupId> <artifactId>jsonartifactId> <version>20131018version> dependency> <dependency> <groupId>com.sun.jerseygroupId> <artifactId>jersey-clientartifactId> <version>1.18.1version> dependency> <dependency> <groupId>jsptagsgroupId> <artifactId>pager-taglibartifactId> <version>2.0version> dependency> <dependency> <groupId>net.sourceforge.jexcelapigroupId> <artifactId>jxlartifactId> <version>2.6.10version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>servlet-apiartifactId> <version>2.4version> <scope>providedscope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-resources-pluginartifactId> <version>3.0.2version> <configuration> <encoding>UTF-8encoding> configuration> plugin> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> <version>3.6.1version> <configuration> <source>1.8source> <target>1.8target> <encoding>UTF-8encoding> configuration> plugin> <plugin> <groupId>org.apache.tomcat.mavengroupId> <artifactId>tomcat7-maven-pluginartifactId> <version>2.2version> <configuration> <path>/capitalpath> <port>8080port> <uriEncoding>utf-8uriEncoding> 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>ssmdemodisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:application-context.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springmvcservletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc_servlet.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservletservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>login.jspwelcome-file>
welcome-file-list>
<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>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-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">
<context:component-scan base-package="org.lpf">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<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-8value>
<value>text/plain;charset=UTF-8value>
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">
<context:component-scan base-package="org.lpf">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<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>
<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>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.lpf.dao">property>
bean>
<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">Demospan> 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脚本)