1,SSM的简介

SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。

其中spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

SpringMVC分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。

2,SSM的搭建

笔者的数据库是Oracle,开发工具为Eclipse,项目的结构如下:

首先配置Spring+MyBatis部分,关于MyBatis的配置可以参见MyBatis之如何配置

sqlmap-config.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE configuration             PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"            "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<!-- 加载SQL定义文件 -->
<mappers>
<mapper resource="cn/shop/mapper/UserMapper.xml" />
</mappers>
</configuration>

sqlmap-config.xml

UserMapper.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="cn.shop.dao.UserMapper">
<!--用户登录-->
<select id="login" parameterType="Map" resultType="cn.shop.bean.User">
<!--sq_s_user 是专门为user表创建的序列-->
select * from s_user where name=#{name} and password=#{password}
</select>
</mapper>

UserMapper.xml

UserMapper.java 文件

package cn.shop.dao;

import java.util.Map;

import org.springframework.stereotype.Component;

import cn.shop.bean.User;

public interface UserMapper {

    /**
* 用户登录
* @param user 需要登录的用户信息,其中必须包含两个key,name和password。
* @return 用户登录成功后的信息
*/
User login(Map map);
}

UserMapper.java

User.java 文件

package cn.shop.bean;

import java.util.Date;

public class User {
private Integer id;
private String name;
private String password;
private Date register_time;
public User() {
super();
}
public User(Integer id, String name, String password, Date register_time) {
super();
this.id = id;
this.name = name;
this.password = password;
this.register_time = register_time;
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
public Date getRegister_time() {
return register_time;
}
public void setRegister_time(Date register_time) {
this.register_time = register_time;
} }

User.java

dispatcherServlet.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!--
开启注解扫描
-->
<context:component-scan base-package="cn.shop"></context:component-scan>
<!--
开启mvc注解扫描
-->
<mvc:annotation-driven/> <!--定义视图 通过internalResourceView来表示 使用的是Servlet/jsp技术-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
<!--jsp存放的目录-->
<property name="prefix">
<value>/</value>
</property>
<!--jsp文件的后缀-->
<property name="suffix">
<value>.jsp</value>
</property>
</bean> <!-- 获取properties配置文件 -->
<bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db-config.properties</value>
</list>
</property>
</bean> <!-- 获取数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
<property name="driverClassName">
<value>${db.dirverClass}</value>
</property>
</bean> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:sqlmap-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="ssf"></property>
<property name="basePackage" value="cn.shop.dao"></property>
</bean> </beans>

dispatcherServlet.xml

db-config.properties 文件

db.url=jdbc:oracle:thin:@localhost:1521:xe
db.username=system
db.password=517839
db.dirverClass=oracle.jdbc.OracleDriver

db-config.properties

配置到这里就完成了Spring和MyBatis的结合,接下来就是和SpringMVC的整合了。关于SpringMVC的配置可以参考基于注解实现SpringMVC+MySQL

web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>shop</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> <!-- 这里是一个总控制器 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcherServlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 解决POST提交乱码问题 -->
<filter>
<filter-name>EncodingName</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>EncodingName</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping> </web-app>

web.xml

UserService.java 文件

package cn.shop.service;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.shop.bean.User;
import cn.shop.dao.UserMapper; @Service
public class UserService { @Resource
private UserMapper userMapper; public User userlogin(Map<String,String> map){
return userMapper.login(map);
}
}

UserService.java

编写完UserService.java文件后,我们可以进行一些测试,否则以后出错不便于调试:

新建一个cn.user.test.UserTest.java文件:

public class UserTest {
@Test
public void testName1() throws Exception {
ApplicationContext ac=new ClassPathXmlApplicationContext("dispatcherServlet.xml"); Map<String,String> map=new HashMap<String,String>();
map.put("name","jame");
map.put("password",""); UserService userService= ac.getBean("userService",UserService.class);
User user=userService.userlogin(map);//测试jame,123456
System.out.println(user); }
}

UserController.java 文件

package cn.shop.controller;

import java.util.HashMap;
import java.util.Map; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import cn.shop.bean.User;
import cn.shop.service.UserService; @Controller
public class UserController { @Resource
private UserService userService; @RequestMapping("/login.do")
public ModelAndView userLogin(String uname,String upass){
Map<String,String> map=new HashMap<String,String>();
map.put("name", uname);
map.put("password", upass);
User user = userService.userlogin(map);
ModelAndView mav=new ModelAndView();
if(user!=null){
mav.getModel().put("loginresult", "登录成功");
}else{
mav.getModel().put("loginresult", "登录失败");
}
mav.setViewName("userLoginResult");
return mav;
}
}

UserController.java

@Resource注解和@Autowired注解都可以取出IOC中容器的对象,关于两者的区别可以查看Spring注解@Resource和@Autowired区别对比.

login.jsp 文件

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<form action="login.do" method="POST">
用户名:<input type="text" name="uname"/><br/>
密码:<input type="password" name="upass"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>

login.jsp

userLoginResult.jsp文件

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录结果页面</title>
</head>
<body>
${loginresult}
</body>
</html>

userLoginResult.jsp

到这里就完成了Spring+SpringMVC+MyBatis的配置了。

【Spring】Spring+SpringMVC+MyBatis框架的搭建的更多相关文章

  1. Spring+SpringMvc+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  2. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc

    在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试的基础上 继续进行springmvc的配置 一:配置完善web.xml文件

  3. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试

    这一部分的主要目的是 配置spring-service.xml  也就是配置spring  并测试service层 是否配置成功 用IntelliJ IDEA 开发Spring+SpringMVC+M ...

  4. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)

    用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...

  5. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)

    引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一   的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...

  6. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一:建立MAVEN Web项目

    一:创建maven web项目er

  7. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee.com/niceyoo/jeenotes-ssm 2. 概述 在写代码之前我们先了解一下 ...

  8. Spring+SpringMvc+Mybatis框架集成搭建教程四(项目部署及测试)

    在IDEA中将项目部署到本地Tomcat下进行运行并验证整合结果 (1).点击如下图所示的下拉按钮,弹出Edit Configurations...后点击该项. (2).跳出如下界面后,点击红框内的& ...

  9. Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)

    依赖导入以及框架整合 (1).打开项目的pom.xml文件,声明依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

随机推荐

  1. glValidateProgram只用于调试

    glValidateProgram应该只用于调试,用于release版本中会影响性能.以下是详细描述:   Before doing so, however, we might want to che ...

  2. artTemplate 简洁语法版

    引用简洁语法的引擎版本,例如: <script src="dist/template.js"></script> 下载 表达式 {{ 与 }} 符号包裹起来 ...

  3. [Canvas]英雄可以射箭了

    点此下载源码并用浏览器观看. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv ...

  4. LintCode: Combination Sum II

    C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, v ...

  5. PHP Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,

    $html = file_get_contents("http://www.somesite.com/"); $dom = new DOMDocument(); $dom-> ...

  6. 【DB】部分MySQL操作记录

    工作中涉及到部分统计工作,恰好把之前的有些SQL再熟悉回顾一下. 一.涉及到时间统计部分: 求时间差: ), (SELECT CURDATE())) AS '试用时间'; ), (SELECT CUR ...

  7. Java通过ScriptEngine 执行js脚本案例

    public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodEx ...

  8. SpringMVC+Spring+mybatis项目从零开始--分布式项目结构搭建

    转载出处: SpringMVC+Spring+mybatis+Redis项目从零开始--分布式项目结构搭建 /** 本文为博主原创文章,如转载请附链接. **/ SSM框架web项目从零开始--分布式 ...

  9. mule学习笔记

    mule学习笔记 1.安装&配置 版本:mule 2.2.1 操作: 1) 下载.解压 2)配置环境变量:MULE_HOME.PATH 3)如果网络环境使用代理,找到%MULE_HOME%/c ...

  10. 图解Ajax工作原理

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6126542.html Ajax指Asynchronous JavaScript and XML(异步的 Jav ...