又快到毕业设计的时候了,有的学弟说想用ssm做毕业设计,在网上找到资料看不懂,基础差。我就帮他写了一个demo,顺便也整理一下。
SSM框架,顾名思义,就是Spring+SpringMVC+mybatis。
通过Spring来将各层进行整合,
通过spring来管理持久层(mybatis),
通过spring来管理handler。
总之,spring是将各层进行整合。
废话不说了,来搭建吧。
共十个步骤,有点啰嗦,但是我觉得挺仔细的。不足之处,指正。
建立user类,自建get和set方法,并构造方法:
1 package com.model; 2 3 public class User { 4 private int id; 5 private String username; 6 private String age; 7 }
建一个userMapper.xml和UserMapper.java 接口文件 。
先做一个select * from d_user;
因为数据库是这个样子滴,如下图:
配置代码如下:
1 package com.mapper; 2 3 import java.util.List; 4 5 import com.model.User; 6 7 public interface UserMapper { 8 List<User> findAllUser(); 9 }
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.mapper.UserMapper"> <!-- 解决表名与字段不匹配 --> <resultMap type="User" id="userResultMap"> <id property="id" column="user_id"/> <result property="username" column="user_name"/> <result property="age" column="user_age"/> </resultMap> <select id="findAllUser" resultMap="userResultMap" resultType="User"> select * from d_user </select> </mapper>
mybatis,算是完成一半了,后面的会继续,不会停的。
需要考虑到service了。这就有了第五步了。
首先,要看到我们之前建的两个包,一个是com.service和com.service.impl。
在com.service中,要建立一个UserService类。代码如下:
package com.service; import java.util.List; import com.model.User; public interface UserService { List<User> findAllUser(); }
另一个,需要用到这个接口,implements它吧。
UserServiceImpl.java代码如下:
package com.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.mapper.UserMapper; import com.model.User; import com.service.UserService; @Service @Transactional public class UserServiceImpl implements UserService{ @Resource public UserMapper userMapper; @Override public List<User> findAllUser() { // TODO Auto-generated method stub List<User> findAllUser = userMapper.findAllUser(); return findAllUser; } }
@后面的注解一定要加的,不然,会出错的。
第六步:开始写controller吧
controller,从单词上就能看到,控制。
写一个UserController类吧。
1 package com.controller; 2 3 import java.util.List; 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 11 import com.model.User; 12 import com.service.UserService; 13 14 @Controller 15 @RequestMapping("/user") 16 public class UserController { 17 18 @Autowired 19 private UserService userService; 20 21 @RequestMapping("/findAllUser") 22 public String findAllUser(HttpServletRequest request){ 23 List<User> listUser = userService.findAllUser(); 24 request.setAttribute("listUser", listUser); 25 return "/allUser"; 26 } 27 }
去看他们之间的类,去了解他们之间的关系。我觉得了解很重要。
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> <typeAliases> <typeAlias alias="User" type="com.model.User"/> </typeAliases> <mappers> <mapper resource="com/mapper/userMapper.xml" /> </mappers> </configuration>
其实,这些代码是在配置mybatis的时候提前敲好的,
<mapper resource="com/mapper/userMapper.xml" />
这行代码是在写完userMapper.xml去写上的。
然后就是去写spring配置了:spring-common.xml和spring-mvc.xml依次如下。其实代码都是我百度的,然后自己修改一下,谢谢分享。
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:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.0.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 12 13 <!-- 1. 数据源 : DriverManagerDataSource --> 14 <bean id="dataSource" 15 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 16 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 17 <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> 18 <property name="username" value="root" /> 19 <property name="password" value="root" /> 20 </bean> 21 22 <!-- 23 2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源 24 25 MyBatis定义数据源,同意加载配置 26 --> 27 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 28 <property name="dataSource" ref="dataSource"></property> 29 <property name="configLocation" value="classpath:config/mybatis-config.xml" /> 30 </bean> 31 32 <!-- 33 3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory 34 35 basePackage:指定sql映射文件/接口所在的包(自动扫描) 36 --> 37 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 38 <property name="basePackage" value="com.mapper"></property> 39 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 40 </bean> 41 42 <!-- 43 4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源 44 --> 45 <bean id="txManager" 46 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 47 <property name="dataSource" ref="dataSource"></property> 48 </bean> 49 50 <!-- 5. 使用声明式事务 51 transaction-manager:引用上面定义的事务管理器 52 --> 53 <tx:annotation-driven transaction-manager="txManager" /> 54 55 </beans> View Code
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:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.0.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 11 12 <!-- 注解扫描包 --> 13 <context:component-scan base-package="com" /> 14 15 <!-- 开启注解 --> 16 <mvc:annotation-driven /> 17 18 <!-- 19 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd 20 --> 21 <mvc:resources mapping="/img/**" location="/img/" /> 22 <mvc:resources mapping="/js/**" location="/js/" /> 23 <mvc:resources mapping="/css/**" location="/css/" /> 24 <mvc:resources mapping="/html/**" location="/html/" /> 25 26 27 28 <!-- 定义跳转的文件的前后缀 ,视图模式配置--> 29 <bean id="viewResolver" 30 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 31 <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> 32 <property name="prefix" value="/WEB-INF/jsp/" /> 33 <property name="suffix" value=".jsp" /> 34 </bean> 35 </beans> View Code
第八步,就是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 7 <!-- 加载Spring容器配置 --> 8 <listener> 9 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 10 </listener> 11 12 <!-- 设置Spring容器加载所有的配置文件的路径 --> 13 <context-param> 14 <param-name>contextConfigLocation</param-name> 15 <param-value>classpath*:config/spring-*.xml</param-value> 16 </context-param> 17 18 <!-- 配置SpringMVC核心控制器 --> 19 <servlet> 20 <servlet-name>springMVC</servlet-name> 21 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 22 23 <init-param> 24 <param-name>contextConfigLocation</param-name> 25 <param-value>classpath*:config/spring-mvc.xml</param-value> 26 </init-param> 27 <!-- 启动加载一次 --> 28 <load-on-startup>1</load-on-startup> 29 </servlet> 30 31 <!--为DispatcherServlet建立映射 --> 32 <servlet-mapping> 33 <servlet-name>springMVC</servlet-name> 34 <!-- 此处可以可以配置成*.do --> 35 <url-pattern>/</url-pattern> 36 </servlet-mapping> 37 38 <!-- 防止Spring内存溢出监听器 --> 39 <listener> 40 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 41 </listener> 42 43 <!-- 解决工程编码过滤器 --> 44 <filter> 45 <filter-name>encodingFilter</filter-name> 46 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 47 <init-param> 48 <param-name>encoding</param-name> 49 <param-value>UTF-8</param-value> 50 </init-param> 51 <init-param> 52 <param-name>forceEncoding</param-name> 53 <param-value>true</param-value> 54 </init-param> 55 </filter> 56 57 <filter-mapping> 58 <filter-name>encodingFilter</filter-name> 59 <url-pattern>/*</url-pattern> 60 </filter-mapping> 61 62 <welcome-file-list> 63 <welcome-file>index.jsp</welcome-file> 64 </welcome-file-list> 65 </web-app> View Code
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 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>ssm</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <table border="1"> 27 <tbody> 28 <tr> 29 <th>姓名</th> 30 <th>年龄</th> 31 </tr> 32 <c:if test="${!empty listUser }"> 33 <c:forEach items="${listUser}" var="list"> 34 <tr> 35 <td>${list.username }</td> 36 <td>${list.age }</td> 37 38 </tr> 39 </c:forEach> 40 </c:if> 41 </tbody> 42 </table> 43 </body> 44 </html> View Code
http://localhost:8080/SSM/user/findAllUser
得到的如下图:
把数据库的数据都显示出来了。