在导入最基本的包,进行了最基础的配置之后,我就开始搭建SSM文件
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.csdn.net/download/baidu_32492845/10126554
5,开始写框架配置文件
1)在config包下面创建一个mybatis-config.xml文件,也就是DAO层的配置文件,文件代码如下
<?xml version="1.0" encoding="UTF-8" ?> 2)下面再在WebRoot下面的WEB-INF下面中创建一个applicationContext.xml,是用来配置spring的,代码如下 <?xml version="1.0" encoding="UTF-8"?><!-- 事务管理器 -->
<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>
3)再在WebRoot下面的WEB-INF下面创建一个配置文件mvc-config.xml,是配置controller层的,代码如下
<?xml version="1.0" encoding="UTF-8"?><!-- 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>
4)配置web.xml文件,代码如下 <?xml version="1.0" encoding="UTF-8"?>
<!-- 加载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>
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
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+"/";
%>
<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">
-->
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"%>
<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>
根据这样搭建完成之后,就可以配置文件,运行成功了。