利用SSM搭建web项目

下面给大家介绍一下,ssm的搭建和使用,7步搞定框架搭建

SSM所需要的jar包下载地址:http://download.youkuaiyun.com/download/baidu_32492845/10126554

1,创建数据库

DROP TABLE IF EXISTS `userinformation`;
CREATE TABLE `userinformation` (
  `id` int(100) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `password` varchar(100) DEFAULT NULL,
  `other` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2,创建Javaweb项目,如下图所示,先建立几个必要的包

config-用来装配置文件;controller-用来装controller文件;domain-用来装实体类;mapper-用来装DAO文件,service-服务层文件;


3,建立创建controller类,domain实体类,DAO的mapper接口和xml文件,service类,只是创建,里面的代码下面会告诉大家怎么写,如下图所示


4,将ssm框架需要用到的jar包copy到WebRoot下面的WEB-INF下面的lib中,如图

SSM框架需要的jar包下载地址:http://download.youkuaiyun.com/download/baidu_32492845/10126554


5,开始写框架配置文件

1)在config包下面创建一个mybatis-config.xml文件,也就是DAO层的配置文件,文件代码如下

<?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>
    <!--自己定义别名--> 
    <typeAliases>
    <!--通过扫描的方式-->  
    <package name="com.test.domain"/>
    </typeAliases> 
	<!-- 加载 映射文件 -->
	<mappers>
		<package name="com/test/mapper" />
	</mappers>
</configuration>
2)下面再在WebRoot下面的WEB-INF下面中创建一个applicationContext.xml,是用来配置spring的,代码如下

<?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: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.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/test" />
		<property name="username" value="root" />
		<property name="password" value="" />
</bean>
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:com/test/config/mybatis-config.xml" />
	</bean>

   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	  <property name="basePackage" value="com.test.mapper" /> 
	  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 
   </bean>
   

 	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 引入事务代理 -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
	<context:component-scan base-package="com.test"></context:component-scan>


</beans>

3)再在WebRoot下面的WEB-INF下面创建一个配置文件mvc-config.xml,是配置controller层的,代码如下

<?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-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 ">
    
	<!-- Spring容器扫描包 -->
	<context:component-scan base-package="com.test"></context:component-scan>
	
	
	<!-- 开启注解 -->
	<mvc:annotation-driven/>
	
	<!-- 允许使用容器默认的Servlet来处理静态资源的请求 -->
	<mvc:default-servlet-handler />
	
	<!-- 如果控制器组件方法返回值为:hello, 那么视图路径自动拼装为:/hello.jsp -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>
4)配置web.xml文件,代码如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="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">

	<!-- 加载Spring容器配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 设置Spring容器加载所有的配置文件的路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

	<!-- 配置SpringMVC核心控制器 -->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

		<init-param>
			<param-name>contextConfigLocation</param-name>
		    <param-value>/WEB-INF/mvc-config.xml</param-value>
		</init-param>
		<!-- 启动加载一次 -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!--为DispatcherServlet建立映射 -->
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<!-- 此处可以可以配置成*.do -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 防止Spring内存溢出监听器 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>

	<!-- 解决工程编码过滤器 -->
	<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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

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

6,配置文件写好了,下面开始写代码

1)domain实体类的User.java

package com.test.domain;

public class User {
	private int id;
	private String name;
	private String password;
	private String other;

	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 String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getOther() {
		return other;
	}

	public void setOther(String other) {
		this.other = other;
	}

}

2)DAO层UserMapper.java

package com.test.mapper;

import java.util.List;

import com.test.domain.User;

public interface UserMapper {

	List<User> findAllUser();

}
3)DAO层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">
<!-- namespace 是用来绑定DAO的,这样UserMapper.java是一个interface,
     而利用这里的UserMapper.xml来作其实现这个interface -->
<mapper namespace="com.test.mapper.UserMapper">
    <!--resultMap是指这里用到的实体类是哪个,id是用来作sql操作完成作返回用到的  -->
    <resultMap type="com.test.domain.User" id="UserMap">
    <!--property是实体类代码的properties,column是数据库表中的 properties -->
    	<id property="id" column="id"/>
    	<result property="name" column="name"/>
    	<result property="password" column="password"/>
    	<result property="other" column="other"/>
    </resultMap>
    <!-- id和DAO中UserMapper.java里面的方法名对应,
    resultMap就是返回的类型,也就是上面已经定义了的resultMap -->
    <select id="findAllUser"  resultMap="UserMap">
    	select * from userInformation
    </select>
 
</mapper>

4)service层的UserService.java

package com.test.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.test.domain.User;
import com.test.mapper.UserMapper;

@Service
public class UserService {

	@Resource
	private UserMapper userMapper;

	public List<User> getAllUser() {
		List<User> list = userMapper.findAllUser();
		return list;
	}
}
5)controller层的UserController.java

package com.test.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.test.domain.User;
import com.test.service.UserService;

@Controller
@RequestMapping("/UserController")
public class UserController {

	@Resource
	private UserService userService;

	@RequestMapping("/getAllUser")
	public String getAllUser(String message, Model model) {
		System.out.println("============message===============:" + message);
		List<User> list = userService.getAllUser();
		model.addAttribute("userList", list);
		return "list";
	}

}
6)index.jsp  访问项目的首页

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <form action="UserController/getAllUser" method="get">
    	<input type="text" name="message"/>
    	<input type="submit" value="提交"/>
    </form>
  </body>
</html>
7)lits.jsp  用来显示所有user的

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":"
			+ request.getServerPort() + path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>list</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">


	</head>

	<body>
		<table border="1">
			<tbody>
				<tr>
					<th>
						id
					</th>
					<th>
						name
					</th>
					<th>
						password
					</th>
					<th>
						other
					</th>
				</tr>

				<c:forEach items="${userList}" var="list">
					<tr>
						<td>
							${list.id }
						</td>
						<td>
							${list.name }
						</td>
						<td>
							${list.password }
						</td>
						<td>
							${list.other }
						</td>
					</tr>
				</c:forEach>

			</tbody>
		</table>
	</body>
</html>
7,运行web项目,在浏览器输入:localhost:端口号默认8080/项目名字/index.jsp,我的是: localhost:8090/TTestSSM/index.jsp,记得数据库要启动起来

下面是项目首页index.jsp




点击提交,出现下面list.jsp界面



好了,整个SSM项目到这里就搭建完成了。

最后可以下载完整版ssm的demo

地址:http://download.youkuaiyun.com/download/baidu_32492845/10126602























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值