下面给大家介绍一下,ssm的搭建和使用,7步搞定框架搭建
SSM所需要的jar包下载地址:http://download.csdn.net/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;
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.csdn.net/download/baidu_32492845/10126554
5,开始写框架配置文件
1)在config包下面创建一个mybatis-config.xml文件,也就是DAO层的配置文件,文件代码如下
2)下面再在WebRoot下面的WEB-INF下面中创建一个applicationContext.xml,是用来配置spring的,代码如下
4)配置web.xml文件,代码如下
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/applicationContext.xml
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/mvc-config.xml
1
springMVC
/
org.springframework.web.util.IntrospectorCleanupListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
index.jsp
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 findAllUser();
}
3)DAO层UserMapper.xml
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 getAllUser() {
List 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 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+"/";
%>
My JSP 'index.jsp' starting page
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"%>
list
id
name
password
other
${list.id }
${list.name }
${list.password }
${list.other }
7,运行web项目,在浏览器输入:localhost:端口号默认8080/项目名字/index.jsp,我的是: localhost:8090/TTestSSM/index.jsp,记得数据库要启动起来
下面是项目首页index.jsp
好了,整个SSM项目到这里就搭建完成了。
最后可以下载完整版ssm的demo
地址:http://download.csdn.net/download/baidu_32492845/10126602