SMM框架详解
为了后面讲解的需要,我们取数据都会从数据库中获取,所以这里先讲讲三大框架(Spring、SpringMVC、MyBatis)的整合。
1、整合思路
①、表现层,也就是 Controller,由 SpringMVC 来控制,而SpringMVC 是Spring 的一个模块,故不需要整合。
②、业务层,也就是 service,通常由 Spring 来管理 service 接口,我们会使用 xml 配置的方式来将 service 接口配置到 spring 配置文件中。而且事务控制一般也是在 service 层进行配置。
③、持久层,也就是 dao 层,而且包括实体类,由 MyBatis 来管理,通过 spring 来管理 mapper 接口,使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
很明显,spring 在三大框架的整合中占据至关重要的地位,类似于一个大管家,将 MyBatis 和 SpringMVC 揉合在一起。
2、准备环境
①、数据库环境
数据库类型:MySQL 5.1
数据库名称:ssm
数据表:user
②、开发工具 eclipse
③、JDK 1.7
④、mybatis 3.3
⑤、SpringMVC 4.2.4
⑥、Spring 4.2.4
⑦、数据库连接池 dbcp1.2.2
⑧、数据库驱动包mysql5.1.26
⑨、日志 log4j 1.2
案例需求:输入用户名和密码进行登录验证
具体的 jar 下载见上面的源码下载链接!
项目的目录结构为:
3、整合 Dao 层
也就是整合 MyBatis 和 Spring
①、在 db.properties 文件中,保存数据库连接的基本信息
1 2 3 4 5 6 | #db.properties
dataSource=org.apache.commons.dbcp.BasicDataSource
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm
username=root
password=root
|
分别是数据库连接池数据源,数据库连接驱动,数据库连接URL,数据库连接用户名,数据库连接密码
②、mybatis全局配置文件 mybatis-configuration.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <? 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 >
<!-- 全局 setting 配置,根据需要添加 -->
<!--开启二级缓存 -->
< settings >
< setting name="cacheEnabled" value="true"/>
</ settings >
<!-- 配置别名 -->
< typeAliases >
<!-- 批量扫描别名 -->
< package name="com.ys.po"/>
</ typeAliases >
<!-- 配置mapper,由于使用 spring 和mybatis 的整合包进行 mapper 扫描,这里不需要配置了
必须遵循:mapper.xml 和 mapper.java 文件同名且在同一个目录下
-->
<!-- <mappers>
</mappers> -->
</ configuration >
|
通过 mapper 接口来加载映射文件,具体可以看这篇博客必须满足下面四点:
1、xxxMapper 接口必须要和 xxxMapper.xml 文件同名且在同一个包下,也就是说 UserMapper.xml 文件中的namespace是UserMapper接口的全类名
2、xxxMapper接口中的方法名和 xxxMapper.xml 文件中定义的 id 一致
3、xxxMapper接口输入参数类型要和 xxxMapper.xml 中定义的 parameterType 一致
4、xxxMapper接口返回数据类型要和 xxxMapper.xml 中定义的 resultType 一致
③、配置 Spring 文件
我们需要配置数据源、SqlSessionFactory以及mapper扫描器,由于这是对 Dao 层的整合,后面还有对于 业务层,表现层等的整合,为了使条目更加清新,我们新建 config/spring 文件夹,这里将配置文件取名为 spring-dao.xml 放入其中。
spring-dao.xml
④、根据逆向工程生成 po 类以及 mapper 文件
我们逆向工程要是一个额外的工程,生成我们所需的po类以及mapper文件后,在将其复制到我们当前项目中,如下:
由于我们这里是进行登录验证,所以在 UserMapper.java 中添加如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 | package com.ys.mapper;
import com.ys.po.User; import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface UserMapper { //通过用户名和密码查询User User selectUserByUsernameAndPassword(User user);
}
|
UserMapper.xml
1 2 3 4 | <!-- 通过用户名和密码查询User -->
<select id= "selectUserByUsernameAndPassword" resultType= "com.ys.po.User" parameterType= "com.ys.po.User" >
select * from user where username = #{username,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR}
</select>
|
dao 层整合完毕之后,我们进行一个测试,要养成每做完一个小模块必须测试的习惯。步步为营,如果整个项目配置完了然后在进行测试,那么有问题进行排除会变得很困难。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.ys.test;
import org.junit.Before; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ys.mapper.UserMapper; import com.ys.po.User;
public class DaoTest { ApplicationContext context = null ;
@Before
public void init(){
context = new ClassPathXmlApplicationContext( "classpath:spring/application-dao.xml" );
}
@Test
public void testSelectByPrimaryKey(){
UserMapper userMapper = (UserMapper) context.getBean( "userMapper" );
User user = userMapper.selectByPrimaryKey( 1 );
System.out.println(user.getPassword());
}
}
|
这里是根据 user 表的 id 进行查询。如果能打印出user对象的值,那么前面的配置是 OK的。
4、整合 service
前面我们整理了,这层就是用 Spring 来管理 service 接口,我们会使用 xml 配置的方式来将 service 接口配置到 spring 配置文件中。而且事务控制也是在 service 层进行配置。
这里我们以登录
①、定义 service 接口
1 2 3 4 5 6 7 8 9 10 | package com.ys.service.impl;
import com.ys.po.User; public interface IUserService {
//通过用户名和密码查询User
public User selectUserByUsernameAndPassword(User user);
}
|
②、编写 service 实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.ys.service;
import org.springframework.beans.factory.annotation.Autowired; import com.ys.mapper.UserMapper; import com.ys.po.User;
import com.ys.service.impl.IUserService;
public class UserServiceImpl implements IUserService{ @Autowired private UserMapper userMapper; //通过@Autowired向spring容器注入UserMapper
//通过用户名和密码查询User
@Override
public User selectUserByUsernameAndPassword(User user) {
User u = userMapper.selectUserByUsernameAndPassword(user);
return u;
}
}
|
通过@Autowired向spring容器注入UserMapper,它会通过spring配的扫描器扫描到,并将对象装载到spring容器中。
③、在spring容器中配置 Service 接口,这里我们使用 xml 的方式
在 config/spring 目录下,新建 spring-service.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?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">
<!--配置UserServiceImpl --> <bean id= "userService" class = "com.ys.service.UserServiceImpl" ></bean>
</beans>
|
④、在spring容器中配置 事务处理
在 config/spring 目录下,新建 spring-transaction.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?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">
<!-- 事务管理器 --> <!-- 对mybatis操作数据事务控制,spring使用jdbc的事务控制类 -->
<bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<!-- 数据源dataSource在spring-dao.xml中配置了 -->
<property name= "dataSource" ref= "dataSource" />
</bean>
<!-- 通知 --> <tx:advice id= "txAdvice" transaction-manager= "transactionManager" >
<tx:attributes>
<tx:method name= "save*" propagation= "REQUIRED" />
<tx:method name= "delete*" propagation= "REQUIRED" />
<tx:method name= "update*" propagation= "REQUIRED" />
<tx:method name= "insert*" propagation= "REQUIRED" />
<tx:method name= "find*" propagation= "SUPPORTS" read-only= "true" />
<tx:method name= "get*" propagation= "SUPPORTS" read-only= "true" />
<tx:method name= "select*" propagation= "SUPPORTS" read-only= "true" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- com.ys.service.impl包里面的所有类,所有方法,任何参数 -->
<aop:advisor advice-ref= "txAdvice" pointcut= "execution(* com.ys.service.impl.*.*(..))" />
</aop:config>
</beans>
|
4、整合 SpringMVC
①、配置前端控制器
在 web.xml 文件中添加如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?xml version= "1.0" encoding= "UTF-8" ?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns= "http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http: //java.sun.com/xml/ns/javaee
http: //java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC_01</display-name>
<!-- 配置前端控制器DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class >
<!--springmvc.xml 是自己创建的SpringMVC全局配置文件,用contextConfigLocation作为参数名来加载
如果不配置 contextConfigLocation,那么默认加载的是/WEB-INF/servlet名称-servlet.xml,在这里也就是 springmvc-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spirng/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping> <servlet-name>springmvc</servlet-name>
<!--第一种配置:*. do ,还可以写*.action等等,表示以. do 结尾的或者以.action结尾的URL都由前端控制器DispatcherServlet来解析
第二种配置:/,所有访问的 URL 都由DispatcherServlet来解析,但是这里最好配置静态文件不由DispatcherServlet来解析
错误配置:/*,注意这里是不能这样配置的,应为如果这样写,最后转发到 jsp 页面的时候,仍然会由DispatcherServlet进行解析,
而这时候会找不到对应的Handler,从而报错!!!
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
|
②、配置处理器映射器、处理器适配器、视图解析器
在 config/spring 目录下新建 springmvc.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?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-4.2.xsd
http: //www.springframework.org/schema/mvc
http: //www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http: //www.springframework.org/schema/context
http: //www.springframework.org/schema/context/spring-context.xsd
http: //www.springframework.org/schema/aop
http: //www.springframework.org/schema/aop/spring-aop-4.2.xsd
http: //www.springframework.org/schema/tx
http: //www.springframework.org/schema/tx/spring-tx.xsd">
<!--使用mvc:annotation-driven可以代替上面的映射器和适配器 这里面会默认加载很多参数绑定方法,比如json转换解析器就默认加载,所以优先使用下面的配置
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--批量配置Handler,指定扫描的包全称 -->
<context:component-scan base- package = "com.ys.controller" ></context:component-scan>
<!--配置视图解析器 --> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
</bean>
</beans>
|
③、编写 Handler,也就是 Controller
在 com.ys.controller 包下新建 UserController.java 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.ys.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ys.po.User; import com.ys.service.impl.IUserService;
@Controller public class UserController {
@Autowired
public IUserService userService;
@RequestMapping ( "/login" )
public ModelAndView login(User user){
ModelAndView mv = new ModelAndView();
User u = userService.selectUserByUsernameAndPassword(user);
//根据用户名和密码查询user,如果存在,则跳转到 success.jsp 页面
if (u != null ){
mv.addObject( "username" , u.getUsername());
mv.addObject( "user" , u);
mv.setViewName( "view/success.jsp" );
} else {
//如果不存在,则跳转到 login.jsp页面重新登录
return new ModelAndView( "redirect:/login.jsp" );
}
return mv;
}
}
|
④、加载 Spring 容器
我们在 classpath/spring 目录下新建了 spring-dao.xml,spring-service.xml,spring-transaction.xml 这些文件,里面有我们配置的 mapper,controller,service,那么如何将这些加载到 spring 容器中呢?
在 web.xml 文件中添加如下代码:
1 2 3 4 5 6 7 8 | <!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
<listener>
<listener- class >org.springframework.web.context.ContextLoaderListener</listener- class >
</listener>
|
由于配置文件比较多,我们使用通配符加载的方式。注意:这段代码最好要加在前端控制器的前面。
至此 SSM 三大框架整合就完成了,接下来我们进行测试。
5、测试
在 WebContent 目录下创建 login.jsp 页面,以及 success.jsp页面,如下图:
Tomcat 部署项目的三种方法
目录
- 1、下载 Tomcat 服务器
- 2、启动并部署 Tomcat 服务器
- 3、Tomcat 的目录结构
- 4、部署项目的第一种方法(项目直接放入 webapps 目录中)
- 5、部署项目的第二种方法(修改 conf/server.xml 文件 )
- 6、部署项目的第三种方法(apache-tomcat-7.0.52\conf\Catalina\localhost )
回到顶部
1、下载 Tomcat 服务器
①、官网下载地址:http://tomcat.apache.org/
②、tomcat 8.0 64位百度云下载地址:http://pan.baidu.com/s/1slbKPsx 密码:ewui
③、tomcat 8.0 32位百度云下载地址:http://pan.baidu.com/s/1o8G28rS 密码:k11n
回到顶部
2、启动并部署 Tomcat 服务器
①、解压 tomcat 安装包到一个非中文目录下
②、配置环境变量。JAVA_HOME(指向 JDK 安装的根目录)
③、双击 apache-tomcat-6.0.16\bin 目录下的 startup.bat,启动服务器(如果一闪而过,那就是没有配置 JAVA_HOME 的环境变量)
④、在浏览器中输入 http://localhost:8080
注意:Tomcat 启动不了的时候注意配置 JAVA_HOME:C:\Program Files\Java\jdk1.6.0_43这是安装 JDK的根目录
回到顶部
3、Tomcat 的目录结构
回到顶部
4、部署项目的第一种方法(项目直接放入 webapps 目录中)
1、将编写并编译好的web项目(注意要是编译好的,如果是 eclipse,可以将项目打成 war 包放入),放入到 webapps 中
2、启动tomcat服务器(双击 apache-tomcat-6.0.16\bin 目录下的 startup.bat,启动服务器)
3、在浏览器输入:http://localhost:8080/项目名/访问的文件名
回到顶部
5、部署项目的第二种方法(修改 conf/server.xml 文件 )
①、打开tomcat下conf/server.xml,在<Host> </Host>标签之间输入项目配置信息
1 | <Context path= "/WebProject" docBase= "D:/WebProject" reloadable= "true" />
|
path:浏览器访问时的路径名
docBase:web项目的WebRoot所在的路径,注意是WebRoot的路径,不是项目的路径。其实也就是编译后的项目
reloadble:设定项目有改动时,tomcat是否重新加载该项目
②、双击 startup.bat,启动 tomcat 服务器,然后在浏览器输入访问的项目名称路径
注意:如果你配置的 path="/xx",那么访问的时候就是这样:
回到顶部
6、部署项目的第三种方法(apache-tomcat-7.0.52\conf\Catalina\localhost )
①、进入到 apache-tomcat-7.0.52\conf\Catalina\localhost 目录,新建一个 项目名.xml 文件
②、在 那个新建的 xml 文件中,增加下面配置语句(和上面的是一样的,但是不需要 path 配置,加上也没什么用)
1 | <Context docBase= "D:/WebProject" reloadable= "true" />
|
③、在浏览器输入路径:localhost:8080/xml文件名/访问的文件名
总结:
①、第一种方法比较普通,但是我们需要将编译好的项目重新 copy 到 webapps 目录下,多出了两步操作
②、第二种方法直接在 server.xml 文件中配置,但是从 tomcat5.0版本开始后,server.xml 文件作为 tomcat 启动的主要配置文件,一旦 tomcat 启动后,便不会再读取这个文件,因此无法再 tomcat 服务启动后发布 web 项目
③、第三种方法是最好的,每个项目分开配置,tomcat 将以\conf\Catalina\localhost 目录下的 xml 文件的文件名作为 web 应用的上下文路径,而不再理会 <Context>中配置的 path 路径,因此在配置的时候,可以不写 path。
通常我们使用第三种方法
login.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <%@ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" >
<title>Insert title here</title>
</head>
<body>
<form action= "login" method= "post" >
<label>账号:</label>
<input type= "text" id= "txtUsername" name= "username" placeholder= "请输入账号" /><br/>
<label>密码:</label>
<input type= "password" id= "txtPassword" name= "password" placeholder= "请输入密码" /><br/>
<input type= "submit" value= "提交" />
<input type= "reset" value= "重置" />
</form>
</body>
</html>
|
success.jsp
1 2 3 4 5 6 7 8 9 10 11 12 | <%@ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" >
<title>Insert title here</title>
</head>
<body>
Hello ${user.username}
</body>
</html>
|
2、在浏览器输入:http://localhost:8080/SSMDemo/login.jsp
点击提交: