ssm框架的搭建
一.所谓的SSM 就是Spring+SpringMVC+Mybatis三者的结合的框架。
二.至少需要的jar包有:
三.配置
1.首先在web.xml做一些配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssm</display-name>
<!--过滤器,对编码转换说明,减少代码量 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ContextLoaderListener的作用是在web.xml声明spring的配置默认在WEB_INF下,也可以像下面一样自定义路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- DispachterServlet的配置 -->
<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:springmvc.xml</param-value><!--springmvc的配置文件 -->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.在spring.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-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
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 ">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--对包进行扫描读取-->
<context:component-scan base-package="com.ssm.dao"/>
<context:component-scan base-package="com.ssm.service"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 第二步:创建Mybatis的sqlSessionFactory。生产sqlSession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- mybatis配置文件路径 -->
</bean>
<!-- 配置mybatis接口代理开发
* 接口类名和映射文件必须同名
* 接口类和映射文件必须在同一个目录 下
* 映射文件namespace名字必须是接口的全类路径名
* 接口的方法名必须和映射Statement的id一致
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao"></property><!-- 数据库操作的dao层的路径 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
3.在springmvc.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: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-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
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">
<!-- 配置扫描的包 -->
<context:component-scan base-package="com.ssm.controller"></context:component-scan>
<!-- 注解启动 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.其他文件的内容
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
jdbc.properties
jdbc.url = jdbc\:mysql\://localhost:3306/user (数据库名)
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username= root(用户名)
jdbc.password= lanouhn(密码)
四.项目实现
做了一个简单的小例子测试ssm框架的运行,在数据库里面建了一个user数据库 user的表
项目的文件存储为
1.在com.ssm.controller中写入一个testcontroller.java,里面写入测试方法
package com.ssm.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ssm.service.showIDservice;
@Controller
public class testcontroller {
@Autowired
private showIDservice showidservice;
@RequestMapping("/test")
public String test(){
Map<String ,Object> map=showidservice.showID("1");//调用showidservice的showID方法
String username=(String)map.get("userName");
System.out.println(username);
return "hello";
}
}
2.在com.ssm.service 中写入showIDservice.java
package com.ssm.service;
import java.util.Map;
public interface showIDservice {
public Map<String,Object> showID(String userid) ;
}
3.在com.ssm.serviceimpl写入showIDserviceimpl,在showIDserviceimpl中实现showID方法,继续调用usermapper的showID方法
package com.ssm.service.impl;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.dao.UserMapper;
import com.ssm.service.showIDservice;
@Service
public class showIDserviceimpl implements showIDservice {
@Autowired
public UserMapper usermapper;
@Override
public Map<String,Object> showID(String userid) {
return usermapper.showID(userid);
}
}
4.在com.ssm.dao中写入UserMapper.java
package com.ssm.dao;
import java.util.Map;
public interface UserMapper {
public Map<String,Object> showID(String userid) ;
}
5.然后写入UserMapper.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.UserMapper" >
<select id="showID" resultType="java.util.Map">
select userId,userName
from user1
where userId = #{userid}
</select>
</mapper>
6.hello.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>Insert title here</title>
</head>
<body>
hello ssm!!!!
</body>
</html>
五.项目运行结果
最后在tomcat搭载运行
控制台中出现
搭建成功!