ssm框架整合測試

上一篇介紹了怎麼SSM框架怎麼進行整合:點擊打開鏈接

 

這一篇主要創建一個項目(只寫登錄功能),測試一下我們的整合流程;

整合SSM:

1.創建測試數據庫:db_test_ssm(編碼utf8)

 

/*
Navicat MySQL Data Transfer

Source Server         : 本地
Source Server Version : 50536
Source Host           : localhost:3306
Source Database       : db_test_ssm

Target Server Type    : MYSQL
Target Server Version : 50536
File Encoding         : 65001

Date: 2017-07-30 16:07:13
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `uid` int(10) NOT NULL,
  `uname` varchar(10) DEFAULT NULL,
  `upassword` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------

 

 

 

2.在eclipse中創建一個web項目

項目結構如下:

3.導入jar包

在項目的WEB-INF/lib下導入jar包,之後,並且右擊-->添加到構建路徑

我的jar包可以在這兒下載:點擊打開鏈接

 

 

4.創建配置文件(db.properties)

不知道是爲什麼,用db.properties配置的話,我的數據庫總是連接不上。所以,我還是在配置文件中

在config目錄下,創建db.properties

直接從我上一個博客中複製,並修改部分參數就可以了。

 

    driver=com.mysql.jdbc.Driver    
    url=jdbc:mysql://localhost:3306/db_test_ssm  
    username=root  
    password=1234  

 

 

5.創建log4j的配置文件,log4j.properties

如果對日誌沒有什麼需求,這塊可以不配置。

 

在config目錄下,創建log4j.properties

直接從我上一個博客中複製,不需要任何修改

 

 

6.創建spring與mybatis整合的配置文件(spring-mybatis.xml)

 

在config目錄下,創建spring-mybatis.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:util="http://www.springframework.org/schema/util"    
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
          
         <!-- 開啓自動掃描:就是將所有的對象交給spring管理 -->    
        <context:component-scan base-package="cn.com"></context:component-scan>    
          
          
        <!-- 配置dataSource -->  
         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
            <property name="url" value="jdbc:mysql://localhost:3306/db_test_ssm"></property>  
            <property name="username" value="root"></property>  
            <property name="password" value="1234"></property>    
        </bean>  
          
          
        <!-- 配置SqlSessionFactory    spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
        <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">  
            <property name="dataSource" ref="dataSource"></property>  
             <!-- 自動掃描mapping.xml文件 -->    
            <property name="mapperLocations" value="classpath:cn/com/sql/*.xml"></property>  
        </bean>  
          
          
        <!-- 配置MapperScannerConfigurer -->  
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
            <property name="basePackage" value="cn.com.dao"></property>  
            <property name="sqlSessionFactoryBeanName" value="ssf"></property>    
        </bean>  
      
      
        <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->    
        <bean id="transactionManager"    
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
            <property name="dataSource" ref="dataSource" />    
        </bean>    
    </beans>  

 

<?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:util="http://www.springframework.org/schema/util"    
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
          
         <!-- 開啓自動掃描:就是將所有的對象交給spring管理 -->    
        <context:component-scan base-package="cn.com"></context:component-scan>    
          
          
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="locations" value="classpath:db.properties"/>  
        </bean>   
          
          
        <!-- 配置dataSource -->  
         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
            <property name="driverClassName" value="${driver}"></property>  
            <property name="url" value="${url}"></property>  
            <property name="username" value="${username}"></property>  
            <property name="password" value="${password}"></property>    
        </bean>  
          
          
        <!-- 配置SqlSessionFactory    spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
            <property name="url" value="jdbc:mysql://localhost:3306/db_test_online"></property>  
            <property name="username" value="root"></property>  
            <property name="password" value="1234"></property>    
        </bean>  

          
          
        <!-- 配置MapperScannerConfigurer -->  
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
            <property name="basePackage" value="cn.com.dao"></property>  
            <property name="sqlSessionFactoryBeanName" value="ssf"></property>    
        </bean>  
      
      
        <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->    
        <bean id="transactionManager"    
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
            <property name="dataSource" ref="dataSource" />    
        </bean>    
    </beans>  

 

 

 

 

 

 

7.整合springMVC(創建spring-mvc.xml)

 

 

在config目錄下,創建spring-mvc.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:util="http://www.springframework.org/schema/util"    
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
              
         <!-- 自動掃描該包,使SpringMVC認爲包下用了@controller註解的類是控制器 -->    
        <context:component-scan base-package="cn.com.controller" />    
          
        <!-- 啓動SpringMVC的註解功能 -->    
       <mvc:annotation-driven></mvc:annotation-driven>  
          
        <!-- 如果需要其他的配置,可以在自行引入 -->  
    </beans>    

 

8.配置web.xml

 

如果eclipse提示報錯,可以參考:點擊打開鏈接

如果不報錯,那就更好了

如果之前項目的配置都和我的一樣,那麼這裏的web.xml的配置直接複製,不需要進行任何修改,如果有不同之處,請自行更改。

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
http://www.springmodules.org/schema/cache/springmodules-cache.xsd
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"  
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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>test_ssm</display-name>
    <!-- Spring和mybatis的配置文件 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring-mybatis.xml</param-value>  
    </context-param>  
    <!-- 編碼過濾器 -->  
    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        
        <async-supported>true</async-supported>  
        <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>  
    <!-- Spring監聽器 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!-- 防止Spring內存溢出監聽器 -->  
    <listener>  
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
    </listener>  
  
    <!-- Spring MVC 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>  
        <async-supported>true</async-supported>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVC</servlet-name>  
        <!-- 此處可以可以配置成*.do,對應struts的後綴習慣 -->  
        <url-pattern>*.do</url-pattern>  
    </servlet-mapping>  
    
    <welcome-file-list>  
        <welcome-file>/index.jsp</welcome-file>  
    </welcome-file-list>  
  

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

 

 

 

 

9.檢查是否有缺漏

 

框架整合完之後,完整的項目結構圖如下:

 

編寫項目代碼:

 

1.在數據庫中插入一條測試數據

2.編寫前臺登錄頁面(index.jsp)

我這裏主要使用,使用ajax發送請求

稍微解釋一下:輸入用戶名和密碼,點擊登錄,ajax就會向後臺發送請求。

 

 

<%@ 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">
<!-- 使用ajax,需要引入jquery -->
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>

<title>測試ssm框架</title>
</head>
<!--   script代碼   -->
<script type="text/javascript">
$(function(){
	var uname = $('#uname').val();
	var upassword = $("#upassword").val();
	$('#login').click(function(){
		$.ajax({
			url:'user/login.do',
			data:{uanme:uname,upassword:upassword},
			type:'post',
			async:false,
			success:function(data){
				//如果登錄成功:彈出:登陸成功,否則:彈出失敗
				alert(data);
			}
		});
	});	
});
</script>

<body>
<!-- 前臺頁面 -->
	用戶名:<input type="text" id="uname" name="uname"></input><br>
	密碼:<input type="password" id="upassword" name="upassword"></input><br>
	<button id="login" >login</button>
</body>
</html>

 

 

 

3.編寫後臺代碼

各個類如下:

先說明一下:從上往下說:

0.user實體類

1.前臺發送請求,找到LoginController

2.LoginController調用LoginServiceImpl

3.LoginServiceImpl調用ILoginDao

4.ILoginDao實現類使用Spring的自動代理實現

5.ILoginDao中的方法,必須對應loginMapper.xml中的id

6.loginMapper.xml中,寫sql語句

7.測試

具體代碼在下面:

3.0.User實體類

 

package cn.com.domain;

public class User {
	
	private int uid;
	private String uname;
	private String upassword;
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUpassword() {
		return upassword;
	}
	public void setUpassword(String upassword) {
		this.upassword = upassword;
	}
	
	
	
	
}

 

 

 

 

 

3.1.LoginController 的代碼

當我們使用@Resource的註解的時候,無法使用,也沒有可以導入的包,怎麼辦?

那是因爲@Resource的註解不是spring 的,而是tomcat的,具體設置參考:點擊打開鏈接

 

package cn.com.controller;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.service.impl.LoginServiceImpl;

@Controller
@RequestMapping("/user")
public class LoginController {
	
	@Resource
	private LoginServiceImpl loginService;
	
	@RequestMapping("/login.do")
	@ResponseBody
	public String execute(String uname,String upassword){
		String result = loginService.login(uname,upassword);
		//因爲前臺亂碼,所以我在這裏輸出一下
		System.out.println(result);
		return result;
	}
	
}

 

 

 

 

 

 

3.2.ILoginService的代碼

 

package cn.com.service;

public interface ILoginService {
	
	public String login(String uname,String upassword);
	
}

 

 

 

 

 

 

3.3.LoginServiceImpl的代碼

 

package cn.com.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.com.dao.ILoginDao;
import cn.com.domain.User;
import cn.com.service.ILoginService;

@Service
public class LoginServiceImpl implements ILoginService{
	
	@Resource
	private ILoginDao loginDao;
	

	public String login(String uname,String upassword) {
		
		User user=   loginDao.login(uname);
		if(upassword.equals(user.getUpassword())){
			return "登錄成功";
		}
		return "登錄失敗";
	}

	
}

 

 

 

 

 

3.4.ILoginDao的代碼

 

package cn.com.dao;


import cn.com.domain.User;

public interface ILoginDao {

	public User login(String uname);

}

 

 

 

 

 

3.5.loginMapper的代碼

 

<?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.com.dao.ILoginDao">

<select id="login" resultType="cn.com.domain.User">
	SELECT * from `user` WHERE uname =#{uname}
</select>

</mapper>

 

 

 

 

 

3.結果圖

 

 

 

 

關於前臺亂碼,可以參考我的另一篇文章:點擊打開鏈接

本項目的源碼包:點擊打開鏈接

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章