ssm框架搭建整合测试
- 下载各种jar包
mybatis下载
https://github.com/mybatis/mybatis-3/releases
mysql驱动下载
http://mvnrepository.com/artifact/mysql/mysql-connector-java
spring下载(spring所用到的所有jar包,包括springmvc)
http://repo.spring.io/release/org/springframework/spring/
配合spring使用的工具类收录(开发包大全),最终版本3.0.2,后面就不更新了。
需要用到的jar:
- aopalliance jar包 AOP联盟的API包,里面包含了针对面向切面的接口。 通常Spring等其它具备动态织入功能的框架依赖此包
- aspectjweaver-1.6.11.jar aspectjweaver是spring的切入点表达式需要用的包
链接:https://pan.baidu.com/s/1rCt4z5JV9BW-MZlsOg1cRg 密码:m222
mybatis-spring jar包下载
http://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.3.2
第三方数据库连接池druid
http://mvnrepository.com/artifact/com.alibaba/druid
Json依赖包Jackson
junit
http://mvnrepository.com/artifact/junit/junit/4.9
各种文件配置
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
applicationContext.xml
- 配置 读取properties文件 jdbc.properties
- 通过注解,将Service的生命周期纳入Spring的管理
- 配置数据源
- 注解事务
- 开启注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <context:property-placeholder location="classpath:jdbc.properties"/> <!--通过注解,将Service的生命周期纳入Spring的管理-->
<context:annotation-config />
<context:component-scan base-package="com.ssm.service" /> <!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.ssm.pojo" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao"/>
</bean> <!-- 注解事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
springmvc.xml
- 配置Controller扫描
- 配置注解驱动
- 视图解释器
- 静态页面,如html,css,js,images可以访问
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--扫描controller ,service -->
<context:component-scan base-package="dx"/> <!--静态页面,如html,css,js,images可以访问-->
<mvc:default-servlet-handler /> <!--注解驱动,以使得访问路径与方法的匹配可以通过注解配置-->
<mvc:annotation-driven />
<!-- 视图解释器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> </be
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--扫描controller ,service -->
<context:component-scan base-package="com.ssm"/> <!--静态页面,如html,css,js,images可以访问-->
<mvc:default-servlet-handler /> <!--注解驱动,以使得访问路径与方法的匹配可以通过注解配置-->
<mvc:annotation-driven />
<!-- 视图解释器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>
web.xml
- spring的配置文件
- 处理POST提交乱码问题
- 配置SpringMVC前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="3.1"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 处理POST提交乱码问题 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping> <!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>SSM</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC配置文件 -->
<!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>SSM</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
- 至此框架就算搭建完成了,然后就是测试环节
新建一个student表
SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('', '张三', '');
INSERT INTO `student` VALUES ('', '李四', '');
INSERT INTO `student` VALUES ('', '王五', '');
INSERT INTO `student` VALUES ('', '赵六', '');
INSERT INTO `student` VALUES ('', '孙琪', '');
INSERT INTO `student` VALUES ('', '路飞', '');
创建student pojo对象
package com.ssm.pojo; import java.io.Serializable; /**
* Created by ${DX} on 2018/9/28.
*/
public class Student { private int id;
private String name;
private int age; public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
} @Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
} 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 int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
StudentDao
package com.ssm.dao; import com.ssm.pojo.Student;
import org.springframework.stereotype.Repository; import java.util.List; /**
* Created by ${DX} on 2018/9/28.
*/
@Repository
public interface StudentDao { List<Student> findAllStudent();
}
StudentDao.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="com.ssm.dao.StudentDao">
<select id="findAllStudent" resultType="Student">
SELECT * FROM student;
</select>
</mapper>
测试方法StudentDaoTest
package test; import com.ssm.dao.StudentDao;
import com.ssm.pojo.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; /**
* Created by ${DX} on 2018/9/28.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class StudentDaoTest { @Autowired
private StudentDao studentDao; @Test
public void test(){ List<Student> students=studentDao.findAllStudent();
System.out.println(students.size()); } }
测试通过
接下来是service层
StudentService
package com.ssm.service; import com.ssm.pojo.Student; import java.util.List; /**
* Created by ${DX} on 2018/9/29.
*/
public interface StudentService { List<Student> findAllStudent();
}
StudentServiceImpl
package com.ssm.service; import com.ssm.dao.StudentDao;
import com.ssm.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; /**
* Created by ${DX} on 2018/9/29.
*/
@Service
public class StudentServiceImpl implements StudentService { @Autowired
private StudentDao studentDao; @Override
public List<Student> findAllStudent() {
return studentDao.findAllStudent();
}
}
StudentSeviceTest
package test; import com.ssm.pojo.Student;
import com.ssm.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; /**
* Created by ${DX} on 2018/9/29.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class StudentSeviceTest { @Autowired
private StudentService studentService; @Test
public void test(){
List<Student> students=studentService.findAllStudent();
System.out.println(students.size());
} }
service层测试通过
最后就是controller层
StudentController
package com.ssm.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* Created by ${DX} on 2018/9/29.
*/ @Controller
@RequestMapping("/student")
public class StudentController { @RequestMapping("/index")
public String index(){
return "hello";
} }
hello.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/9/29
Time: 0:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
HELLO SSM!!!!
</body>
</html>
开启服务器后,页面访问/student/index 就会跳转到hello.jsp 页面
至此 ssm 框架搭建测试完毕!!!
ssm框架搭建整合测试的更多相关文章
- SSM 框架搭建
SSM框架搭建(Spring.SpringMVC.Mybatis) 一:基本概念 Spring : Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框 ...
- SSM框架搭建教程(从零开始,图文结合)
1.准备 IntelliJ IDEA Tomcat JDK Maven mysql spring.springmvc.mybatis 了解 现在假设如上条件你都具备,那么通过我这篇博客 你一定可以整合 ...
- SpringMVC笔记——SSM框架搭建简单实例
落叶枫桥 博客园 首页 新随笔 联系 订阅 管理 SpringMVC笔记——SSM框架搭建简单实例 简介 Spring+SpringMVC+MyBatis框架(SSM)是比较热门的中小型企业级项目开发 ...
- SSM框架——详细整合教程
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Jav ...
- SSM 框架快速整合实例--学生查询
一.快速准备 SSM 框架即 Spring 框架.SpringMVC 框架.MyBatis 框架,关于这几个框架的基础和入门程序,我前面已经写过几篇文章作为基础和入门介绍了.对于这 3 个框架还不熟悉 ...
- SSM框架快速整合实例——学生查询
一.快速准备 SSM 框架即 Spring 框架.SpringMVC 框架.MyBatis 框架,关于这几个框架的基础和入门程序,我前面已经写过几篇文章作为基础和入门介绍了.这里再简单的介绍一下: 1 ...
- 实习小结(二)--- SSM框架搭建
SSM项目框架搭建 前几天做了一个学生信息管理的项目,使用纯控制台输入,查询数据库,将信息在控制台中打印,功能完善得差不多之后,老师让将这个项目移植到Web中,使用Spring+SpringMVC+M ...
- SSM框架的整合与使用——实现简单的转账系统
一.整合思路 SSM框架即SpringMVC + Spring + MyBati框架集,是一种轻量级的Web开源框架.它们各自在JAVA三层架构中负责的模块如下图所示: 其中,SpringMVC与Sp ...
- SSM框架的整合思路&功能实现
这是我第一篇博客,关于SSM框架的整合思路以及简单功能实现. 首先,最近刚刚学习Spring+SpringMVC+Mybatis,在开发时遇到形形色色的问题,周遭人也为我提供了一些思路,我会一点点整理 ...
随机推荐
- python学习相关软件安装
爬虫初学者必装的软件!! 一.安装python step1:安装包下载 1.1.官网下载地址:https://www.python.org/ 根据自己电脑系统选择,我选择windowns 有不同版本可 ...
- LeetCode--139--单词拆分(python)
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词.你可以假设字典中没有重复的 ...
- Quartus_II官方教程-中文版之SignalTap II
非常实用 187-196 第十二章:调试 Quartus_II官方教程-中文版.pdf
- SpringCloud 教程 (七)服务注册(consul)
一.consul 简介 consul 具有以下性质: 服务发现:consul通过http 方式注册服务,并且服务与服务之间相互感应. 服务健康监测 key/value 存储 多数据中心 consul可 ...
- VS2015中添加QT5.9.0插件
https://blog.csdn.net/hhhuang1991/article/details/79768595 VS2015里使用QTDIR路径查找QT开发包目录 路径配置操作系统环境变量里添加 ...
- 再说vim 乱码问题
在vimrc设置中, 可以简写: file = f encoding = enc fileencodings = fencs fileencoding = fenc 主要有四个编码, 要了解它们的含义 ...
- GMSSL中生成SM2或RSA1024或RSA2048的证书相关命令
1.生成KEY:gmssl sm2 -genkey -out 01.root.pemgmssl genrsa -out 01.root_plain.key 2048gmssl genrsa -out ...
- node+express 发送get请求
var express = require('express') , app = express(); var querystring = require('querystring'); var ut ...
- 爬楼梯问题,yield学习总结
问题起源: 一个人爬楼梯,一步可以迈一级,二级,如果楼梯有N级,要求编写程序,求总共有多少种走法. 简单的一个递归思想,只要爬到了N-1层,或者爬到N-2层,则认定下一步只有一种走法.所以再去找寻N- ...
- 【ABAP系列】SAP GUI740 PATCH5出现弹窗BUG
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP GUI740 PATCH ...