SSM框架的介绍与搭建

一、简要介绍

1. Spring
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

2. SpringMVC
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

3. MyBatis
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

二、SSM的搭建

1. 创建maven项目。
2. 编辑pom.xml文件,添加相关jar包。
3. 新建db.properties。
在maven项目->src->resources目录下,新建db.properties文件并配置如下语句:

	mysql.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
	mysql.driverClassName=com.mysql.jdbc.Driver
	mysql.username=root
	mysql.password=root

4. 新建application-context.xml,将spring框架整合到web工程中。
在maven项目->src->resources目录下,新建一个application-context.xml文件,在此文件中写上如下语句:

    <beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-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/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">
	
	<!--读取数据库的配置文件-->
	<context:property-placeholderlocation="classpath:db.properties"/>
	
	<!--MySQL数据源-->
	<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
	<propertyname="url"value="${mysql.url}"/>
	<propertyname="driverClassName"value="${mysql.driverClassName}"/>
	<propertyname="username"value="${mysql.username}"/>
	<!--MySQL密码-->
	<propertyname="password"value="${mysql.password}"/>
	</bean>
	
	<!--session工厂-->
	<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
	<propertyname="dataSource"ref="dataSource"/>
	<propertyname="configLocation"value="classpath:mybatis.xml"/>
	</bean>
	
	<!--扫描器-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<propertyname="basePackage"value="com.tb.mapper"/>
	<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
	</bean>
</beans>	

5. 新建springmvc.xml
在maven项目->src->resources目录下,新建一个springmvc.xml文件,在此文件中写上如下语句:

	<beansxmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-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/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.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsd">
	<!--配置组建扫描器-->
	<context:component-scanbase-package="com.tb"/>
	<!--对静态资源放行-->
	<mvc:default-servlet-handler/>
	<!--配置注解驱动-->
	<mvc:annotation-driven/>
	<!--视图解析器,JSP-->
	<beanid="viewResolverJsp"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<!--配置逻辑视图的前缀-->
	<propertyname="prefix"value="/WEB-INF/view/"/>
	<!--配置逻辑视图的后缀-->
	<propertyname="suffix"value=".jsp"/>
	</bean>
</beans>

6. 新建mybatis.xml
在maven项目->src->resources目录下,新建一个mybatis.xml文件,在此文件中写上如下语句:

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <!--映射下划线风格到驼峰风格-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--打印SQL语句  -->
        <!-- <setting name="logImpl" value="STDOUT_LOGGING"/> -->
    </settings>

</configuration>

7. 新建web.xml
在maven项目->src->main下,新建一个webapp文件夹,在webapp下新建WEB-INF文件夹,在WEB-INF下新建web.xml文件,在web.xml文件下写上如下语句:

	<?xmlversion="1.0"encoding="UTF-8"?>
	<web-appxmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	
	<!--配置spring-->
	<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:application-context.xml</param-value>
	</context-param>
	
	<!--配置springmvc-->
	<servlet>
	<servlet-name>spring-mvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
	<param-name>contextConfigLocation</param-name>
	<!--配置文件的地址如果不配置contextConfigLocation,默认查找的配置文件名称classpath下的:servlet名称+"-serlvet.xml"即:springmvc-serlvet.xml-->
	<param-value>classpath:springmvc.xml</param-value>
	</init-param>
	</servlet>
	<servlet-mapping>
	<servlet-name>spring-mvc</servlet-name>
	<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!--将mybatis与spring整合-->
	<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	</web-app>

三、简单的web项目测试

1. 建包
在maven项目->src->main->java下,分别新建如下包:

com.dream.controller
com.dream.model
com.dream.service
com.dream.service

2. 新建view文件夹
在maven项目->src->main->webapp->WEB-INF下,新建view文件夹

3. 新建 index.jsp 文件

<%--
CreatedbyIntelliJIDEA.
User:meng
Date:2018/4/8
Time:15:48
TochangethistemplateuseFile|Settings|FileTemplates.
--%>
<%@pagecontentType="text/html;charset=UTF-8"language="java"%>
<html>
<head>
<title>home</title>
</head>
<body>
<h1>Hello,${name}</h1>
<h1>Thisismyteacher!</h1>
</body>
</html>

4. 新建IndexController.java类

packagecom.dream.controller;

importcom.dream.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.web.bind.annotation.RequestMapping;

/**
*@description:入口
*@author:snower
*@create:2018-04-08 16:01
**/
@Controller
publicclassIndexController{
	@Autowired
	UserServiceuserService;

	@RequestMapping("/")
	publicStringindex(Modelmodel){
		Strings=userService.getName();
	model.addAttribute("name",s);
	return"index";
	}

}

5. 新建UserService.java类

packagecom.dream.service;

importcom.dream.mapper.UserMapper;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;

/**
*@description:服务
*@author:snower
*@create:2018-04-08 16:06
**/
@Service
publicclassUserService{
	@Autowired
	UserMapperuserMapper;

	publicStringgetName(){
		Stringname=userMapper.getName();
		return name;
	}
}

6. 新建UserMapper.java接口

packagecom.dream.mapper;

/**
*@description:mapper
*@author:snower
*@create:2018-04-0816:16
**/

publicinterfaceUserMapper{
StringgetName();
}

7. 新建UserMapper.xml接口

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.dream.mapper.UserMapper">
<selectid="getName"resultType="java.lang.String">
selectname
fromuserWHEREid=1;
</select>
</mapper>

8. 配置Tomcat服务器,点击运行

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值