SSM项目搭建全过程

2019/7/24 13:21:02 人评论 次浏览 分类:学习教程

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

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

1,创建数据库

  1. DROP TABLE IF EXISTS `userinformation`;
  2. CREATE TABLE `userinformation` (
  3. `id` int(100) NOT NULL AUTO_INCREMENT,
  4. `name` varchar(100) DEFAULT NULL,
  5. `password` varchar(100) DEFAULT NULL,
  6. `other` varchar(200) DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. ) 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层的配置文件,文件代码如下

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!--自己定义别名-->
  7. <typeAliases>
  8. <!--通过扫描的方式-->
  9. <package name="com.test.domain"/>
  10. </typeAliases>
  11. <!-- 加载 映射文件 -->
  12. <mappers>
  13. <package name="com/test/mapper" />
  14. </mappers>
  15. </configuration>
2)下面再在WebRoot下面的WEB-INF下面中创建一个applicationContext.xml,是用来配置spring的,代码如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  15. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  16. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  17. <property name="url" value="jdbc:mysql://localhost:3306/test" />
  18. <property name="username" value="root" />
  19. <property name="password" value="" />
  20. </bean>
  21. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  22. <property name="dataSource" ref="dataSource" />
  23. <property name="configLocation" value="classpath:com/test/config/mybatis-config.xml" />
  24. </bean>
  25. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  26. <property name="basePackage" value="com.test.mapper" />
  27. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  28. </bean>
  29. <!-- 事务管理器 -->
  30. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  31. <property name="dataSource" ref="dataSource" />
  32. </bean>
  33. <!-- 引入事务代理 -->
  34. <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
  35. <context:component-scan base-package="com.test"></context:component-scan>
  36. </beans>

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
  16. <!-- Spring容器扫描包 -->
  17. <context:component-scan base-package="com.test"></context:component-scan>
  18. <!-- 开启注解 -->
  19. <mvc:annotation-driven/>
  20. <!-- 允许使用容器默认的Servlet来处理静态资源的请求 -->
  21. <mvc:default-servlet-handler />
  22. <!-- 如果控制器组件方法返回值为:hello, 那么视图路径自动拼装为:/hello.jsp -->
  23. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  24. <property name="prefix" value="/" />
  25. <property name="suffix" value=".jsp"></property>
  26. </bean>
  27. </beans>
4)配置web.xml文件,代码如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <!-- 加载Spring容器配置 -->
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9. </listener>
  10. <!-- 设置Spring容器加载所有的配置文件的路径 -->
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>/WEB-INF/applicationContext.xml</param-value>
  14. </context-param>
  15. <!-- 配置SpringMVC核心控制器 -->
  16. <servlet>
  17. <servlet-name>springMVC</servlet-name>
  18. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  19. <init-param>
  20. <param-name>contextConfigLocation</param-name>
  21. <param-value>/WEB-INF/mvc-config.xml</param-value>
  22. </init-param>
  23. <!-- 启动加载一次 -->
  24. <load-on-startup>1</load-on-startup>
  25. </servlet>
  26. <!--为DispatcherServlet建立映射 -->
  27. <servlet-mapping>
  28. <servlet-name>springMVC</servlet-name>
  29. <!-- 此处可以可以配置成*.do -->
  30. <url-pattern>/</url-pattern>
  31. </servlet-mapping>
  32. <!-- 防止Spring内存溢出监听器 -->
  33. <listener>
  34. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  35. </listener>
  36. <!-- 解决工程编码过滤器 -->
  37. <filter>
  38. <filter-name>encodingFilter</filter-name>
  39. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  40. <init-param>
  41. <param-name>encoding</param-name>
  42. <param-value>UTF-8</param-value>
  43. </init-param>
  44. <init-param>
  45. <param-name>forceEncoding</param-name>
  46. <param-value>true</param-value>
  47. </init-param>
  48. </filter>
  49. <filter-mapping>
  50. <filter-name>encodingFilter</filter-name>
  51. <url-pattern>/*</url-pattern>
  52. </filter-mapping>
  53. <welcome-file-list>
  54. <welcome-file>index.jsp</welcome-file>
  55. </welcome-file-list>
  56. </web-app>

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

1)domain实体类的User.java

  1. package com.test.domain;
  2. public class User {
  3. private int id;
  4. private String name;
  5. private String password;
  6. private String other;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public String getPassword() {
  20. return password;
  21. }
  22. public void setPassword(String password) {
  23. this.password = password;
  24. }
  25. public String getOther() {
  26. return other;
  27. }
  28. public void setOther(String other) {
  29. this.other = other;
  30. }
  31. }

2)DAO层UserMapper.java

  1. package com.test.mapper;
  2. import java.util.List;
  3. import com.test.domain.User;
  4. public interface UserMapper {
  5. List<User> findAllUser();
  6. }
3)DAO层UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!-- namespace 是用来绑定DAO的,这样UserMapper.java是一个interface,
  6. 而利用这里的UserMapper.xml来作其实现这个interface -->
  7. <mapper namespace="com.test.mapper.UserMapper">
  8. <!--resultMap是指这里用到的实体类是哪个,id是用来作sql操作完成作返回用到的 -->
  9. <resultMap type="com.test.domain.User" id="UserMap">
  10. <!--property是实体类代码的properties,column是数据库表中的 properties -->
  11. <id property="id" column="id"/>
  12. <result property="name" column="name"/>
  13. <result property="password" column="password"/>
  14. <result property="other" column="other"/>
  15. </resultMap>
  16. <!-- id和DAO中UserMapper.java里面的方法名对应,
  17. resultMap就是返回的类型,也就是上面已经定义了的resultMap -->
  18. <select id="findAllUser" resultMap="UserMap">
  19. select * from userInformation
  20. </select>
  21. </mapper>

4)service层的UserService.java

  1. package com.test.service;
  2. import java.util.List;
  3. import javax.annotation.Resource;
  4. import org.springframework.stereotype.Service;
  5. import com.test.domain.User;
  6. import com.test.mapper.UserMapper;
  7. @Service
  8. public class UserService {
  9. @Resource
  10. private UserMapper userMapper;
  11. public List<User> getAllUser() {
  12. List<User> list = userMapper.findAllUser();
  13. return list;
  14. }
  15. }
5)controller层的UserController.java

  1. package com.test.controller;
  2. import java.util.List;
  3. import javax.annotation.Resource;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import com.test.domain.User;
  8. import com.test.service.UserService;
  9. @Controller
  10. @RequestMapping("/UserController")
  11. public class UserController {
  12. @Resource
  13. private UserService userService;
  14. @RequestMapping("/getAllUser")
  15. public String getAllUser(String message, Model model) {
  16. System.out.println("============message===============:" + message);
  17. List<User> list = userService.getAllUser();
  18. model.addAttribute("userList", list);
  19. return "list";
  20. }
  21. }
6)index.jsp  访问项目的首页

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <form action="UserController/getAllUser" method="get">
  22. <input type="text" name="message"/>
  23. <input type="submit" value="提交"/>
  24. </form>
  25. </body>
  26. </html>
7)lits.jsp  用来显示所有user的

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://" + request.getServerName() + ":"
  5. + request.getServerPort() + path + "/";
  6. %>
  7. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>list</title>
  13. <meta http-equiv="pragma" content="no-cache">
  14. <meta http-equiv="cache-control" content="no-cache">
  15. <meta http-equiv="expires" content="0">
  16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <meta http-equiv="description" content="This is my page">
  18. </head>
  19. <body>
  20. <table border="1">
  21. <tbody>
  22. <tr>
  23. <th>
  24. id
  25. </th>
  26. <th>
  27. name
  28. </th>
  29. <th>
  30. password
  31. </th>
  32. <th>
  33. other
  34. </th>
  35. </tr>
  36. <c:forEach items="${userList}" var="list">
  37. <tr>
  38. <td>
  39. ${list.id }
  40. </td>
  41. <td>
  42. ${list.name }
  43. </td>
  44. <td>
  45. ${list.password }
  46. </td>
  47. <td>
  48. ${list.other }
  49. </td>
  50. </tr>
  51. </c:forEach>
  52. </tbody>
  53. </table>
  54. </body>
  55. </html>
7,运行web项目,在浏览器输入:localhost:端口号默认8080/项目名字/index.jsp,我的是: localhost:8090/TTestSSM/index.jsp,记得数据库要启动起来

下面是项目首页index.jsp




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



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

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

地址:http://download.csdn.net/download/baidu_32492845/10126602























相关资讯

  • 那些我们不愿意承认的事

    很久没有见的老朋友,准确的说应该是很久没有见过的老师,一个比我大两岁的老师,我上初中的时候他从高中回来教我了一年。后来又回去上高中,我上高中的时候他上大学,现在我刚大学毕业他创办了公司。昨日一见依然如故,他还是热爱销售,而我却成了纯粹的技术人员。 看到他…

    2015/6/22 13:12:47

学习教程

共有访客发表了评论 网友评论

验证码: 看不清楚?
    -->