ssm框架整合测试
来源:互联网 发布:炉石传说有mac版吗 编辑:程序博客网 时间:2024/05/18 03:25
上一篇介绍了怎么SSM框架怎么进行整合:点击打开链接
这一篇主要创建一个项目(只写登录功能),测试一下我们的整合流程;
整合SSM:
1.创建测试数据库:db_test_ssm(编码utf8)
/*Navicat MySQL Data TransferSource Server : 本地Source Server Version : 50536Source Host : localhost:3306Source Database : db_test_ssmTarget Server Type : MYSQLTarget Server Version : 50536File Encoding : 65001Date: 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-instancehttp://www.springmodules.org/schema/cache/springmodules-cache.xsdhttp://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 {@Resourceprivate LoginServiceImpl loginService;@RequestMapping("/login.do")@ResponseBodypublic 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;@Servicepublic class LoginServiceImpl implements ILoginService{@Resourceprivate 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.结果图
关于前台乱码,可以参考我的另一篇文章:点击打开链接
本项目的源码包:点击打开链接
- ssm框架整合测试
- SSM框架整合Junit测试
- SSM框架整合之 junit测试
- SSM框架快速整合与测试
- 史上最全SSM框架整合(四)-----测试整合后的SSM框架
- SSM:简单SSM框架整合
- SSM框架整合
- ssm框架整合
- SSM框架整合例子
- SSM框架整合
- SSM框架整合
- ssm框架整合
- SSM框架整合
- SSM 框架整合
- ssm框架总体整合
- SSM框架整合
- SSM 框架整合案例
- SSM框架整合
- 微信企业号主动发送
- hihoOffer收割练习20题目2
- jQuery LightBox插件原理的简单实现
- HBase集群启动脚本流程分析
- 生物信息脚本练习(3)gb文件转换
- ssm框架整合测试
- HDU5934Bomb(强连通缩点)
- UIAutomator之---UiObject
- 《网络经济与企业管理》第一遍思维导图
- 设计模式——单例
- 二叉树的路径和
- 背包问题 (Knapsack.cpp)
- C++实现身份证校验和归属地查询
- How Many Tables (简单并查集)