首先感谢一下润和软件,指引我走上了Spring MVC Web开发的道路。
下面进入正题
搭建开发环境:
Netbeans8.0.2 + MySql5.6 + JDK1.7 + tomcat8.0.15
本次采用的Spring MVC Jar包如下:
spring-aop-4.1.3.RELEASE.jar
spring-beans-4.1.3.RELEASE.jar
spring-context-4.1.3.RELEASE.jar
spring-context-support-4.1.3.RELEASE.jar
spring-core-4.1.3.RELEASE.jar
spring-expression-4.1.3.RELEASE.jar
spring-jdbc-4.1.3.RELEASE.jar
spring-oxm-4.1.3.RELEASE.jar
spring-tx-4.1.3.RELEASE.jar
spring-web-4.1.3.RELEASE.jar
spring-webmvc-4.1.3.RELEASE.jar
当然还有其他的依赖jar,如有需要请留下邮箱~!
1.首先创建Web项目
web.mvc
2.引入所有的jar文件
3.配置web.xml
做web开发的都知道,web.xml是项目的入口,我们既然使用spring那么当然要添加Spring的支持啦!
spring默认加载的是applicationContext.xml,由于我们需要集成MyBatis
所以我把Spring的配置文件强制指定名称,并与MyBatis一起配置,减少配置文件数量
先指定spring配置文件名名称与路径
<!-- Spring 上下文参数 加载Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:xmlconfig/springmvc-mybatis.xml</param-value> </context-param>再执行Spring上下文监听配置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
添加支持Spring mvc 的配置
说明:添加支持目前有3种方式。
第一种:拦截 /*.do
<!-- spring mvc 拦截器配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/xmlconfig/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/*.do</url-pattern> </servlet-mapping>优势:拦截明确,不会和其他的拦截冲突
缺点:URL不美观
第二种:拦截 /*
<!-- spring mvc 拦截器配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/xmlconfig/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>优点:支持rest风格,Url美观
第三种,也是我推荐的一种:拦截 /
<!-- spring mvc 拦截器配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/xmlconfig/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>优点:支持rest风格,URL美观,不拦截静态资源
大家都知道,web项目最怕的就是乱码,而spring为我们提供了字符编码的过滤器,我们也给配置到web.xml中
<!-- 字符编码配置 --> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>我们采用的是tomcat服务器,为了让静态的资源直接交给tomcat处理,所以我们在web.xml中配置了以下配置
<!-- 激活Tomcat的defaultServlet来处理静态文件 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping>
4.配置springmvc-mybatis.xml和springmvc-servlet.xml
相信很多人都在这里卡主了,因为配置实在是太多了,各种方式都有
为了不妨碍大家,我决定直接将完整的配置直接贴出来
springmvc-mybatis.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <!-- 数据源定义--> <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"> <property name="driverClass" value="jdbc:mysql://127.0.0.1:3306/web" /> <property name="jdbcUrl" value="root" /> <property name="username" value="123456" /> <property name="password" value="com.mysql.jdbc.Driver" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath*:dbmap/*Mapper.xml" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"/> <!-- 查 找 类 路 径 下 的 映 射 器 并 自 动 将 它 们 创 建 成 MapperFactoryBean--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ansitech.web.orm.mapper" /> </bean> </beans>
spring-servlet.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <description>Spring MVC Configuration</description> <!-- 对静态资源文件的访问,交给default servlet handler处理 --> <mvc:default-servlet-handler/> <!-- 启用spring mvc 注解 --> <context:annotation-config /> <!-- 默认的注解映射的支持 --> <mvc:annotation-driven /> <!-- 设置使用注解的类所在的jar包 --> <context:component-scan base-package="com.ansitech.web"></context:component-scan> <!-- 完成请求和注解POJO的映射 --> <bean class= "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- JSP视图文件解析配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> <property name="viewNames" value="jsp/*"/> <property name="order" value="1"/> </bean> <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> <property name="order" value="2"/> </bean> <!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1024(B)=10485760 bytes --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maxUploadSize"> <value>5242880</value> </property> </bean> </beans>web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 程序Session配置 --> <session-config> <!-- Session过期时间(单位:分) --> <session-timeout>30</session-timeout> </session-config> <!-- 激活Tomcat的defaultServlet来处理静态文件 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <!-- Spring 上下文参数 加载Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:xmlconfig/springmvc-mybatis.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring mvc 拦截器配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/xmlconfig/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 字符编码配置 --> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 欢迎页 --> <welcome-file-list> <welcome-file>/index.html</welcome-file> </welcome-file-list> </web-app>接下来就是包的结构了
前缀大家可以自己定义自己喜欢的,但是后边的结构,是整个框架的核心,所以请大家按照我的格式去建
前缀com.ansitech
com.ansitech.orm
com.ansitech.orm.base
com.ansitech.orm.mapper
com.ansitech.service
com.ansitech.service.po
com.ansitech.service.impl
com.ansitech.web
com.ansitech.web.controller
大家可能不理解,为什么要这么建呢?
那么我们来以用户管理模块来解释,用户对象为User
那么我们需要建立基础对象:
com.ansitech.orm.base.User.java
package com.ansitech.orm.base; /** * 用户对象 * * @author qsyang */ public class User { private int id; private String loginName; private String loginPwd; /** * 用户类型 1 管理员 2 普通用户 */ private int typeId; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getLoginPwd() { return loginPwd; } public void setLoginPwd(String loginPwd) { this.loginPwd = loginPwd; } public int getTypeId() { return typeId; } public void setTypeId(int typeId) { this.typeId = typeId; } }基础对象建好了,我们需要做一个Mapper来实现用户对象的增删改查
com.ansitech.orm.mapper.UserMapper.java
package com.ansitech.orm.mapper; import com.ansitech.orm.base.User; import org.apache.ibatis.annotations.Param; /** * 用户数据接口 * * @author qsyang */ public interface UserMapper { void add(User user); User get(@Param("id") int id); void update(User user); int delete(@Param("id") int id); }
使用ORM模式,我们需要创建一个抽象的用户对象来解决额外属性查询的问题
com.ansitech.orm.AbstractUser.java
package com.ansitech.orm; import com.ansitech.orm.base.User; /** * 抽象用户对象 * * 提供其他属性获取 * * @author qsyang */ public abstract class AbstractUser extends User{ //获取当前用户所属分类名称 public abstract String findTypeName(); }
有了数据接口了,我们需要对外提供服务
com.ansitech.service.IUserService.java
package com.ansitech.service; import com.ansitech.orm.AbstractUser; import com.ansitech.orm.base.User; /** * 用户服务接口 * * @author qsyang */ public interface IUserService { //添加系统管理员 void addSystemUser(String loginName, String loginPwd); //添加普通用户 void addNormalUser(String loginName, String loginPwd); //根据用户Id查询抽象用户 AbstractUser getAbstractUserById(int userId); //根据用户Id查询用户 User getUserById(int userId); //修改用户信息 void updateUser(int userId, String loginName, String loginPwd); //根据用户Id删除用户 int deleteUser(int userId); }
有了服务接口,就要有实现接口
com.ansitech.service.impl.UserService.java
package com.ansitech.service.impl; import com.ansitech.orm.AbstractUser; import com.ansitech.orm.base.User; import com.ansitech.orm.mapper.UserMapper; import com.ansitech.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * 用户接口实现类 * * @author qsyang */ @Service public class UserService implements IUserService { @Autowired private UserMapper userMapper; @Override public void addSystemUser(String loginName, String loginPwd) { User user = new User(); user.setLoginName(loginName); user.setLoginPwd(loginPwd); //用户类型 1 管理员 2 普通用户 user.setTypeId(1); userMapper.add(null); } @Override public void addNormalUser(String loginName, String loginPwd) { User user = new User(); user.setLoginName(loginName); user.setLoginPwd(loginPwd); //用户类型 1 管理员 2 普通用户 user.setTypeId(2); userMapper.add(null); } @Override public AbstractUser getAbstractUserById(int userId) { throw new UnsupportedOperationException("Not supported yet."); } @Override public User getUserById(int userId) { return userMapper.get(userId); } @Override public void updateUser(int userId, String loginName, String loginPwd) { //先根据用户Id查询用户对象 User user = userMapper.get(userId); if (user != null) { user.setLoginName(loginName); user.setLoginPwd(loginPwd); //修改用户 userMapper.update(user); } } @Override public int deleteUser(int userId) { return userMapper.delete(userId); } }细心的便宜,可能已经看到了,getAbstractUserById()这个方法我们还没有实现
package com.ansitech.service.po; import com.ansitech.orm.AbstractUser; import com.ansitech.orm.base.User; import com.ansitech.service.impl.UserService; /** * 用户数据操作对象 * * @author qsyang */ public class UserPO extends AbstractUser { private final UserService userService; public UserPO(UserService userService) { this.userService = userService; } public UserPO copyFrom(User obj) { this.setId(obj.getId()); this.setLoginName(obj.getLoginName()); this.setLoginPwd(obj.getLoginPwd()); this.setTypeId(obj.getTypeId()); return this; } @Override public String findTypeName() { return userService.findTypeName(this.getTypeId()); } }对象已经创建好了,这个时候我们可以修改下service实现类的方法了,并且我们增加了一个方法
@Override public AbstractUser getAbstractUserById(int userId) { User user = userMapper.get(userId); if (user != null) { UserPO po = new UserPO(this); po.copyFrom(user); return po; } return null; }新增的方法:
public String findTypeName(int typeId) { if (typeId == 1) { return "系统管理员"; } else { return "普通用户"; } }只有一来,Service架构和orm架构就已经完成了,下面只需要建立控制器就可以了
package com.ansitech.web.controller.user; import com.ansitech.orm.base.User; import com.ansitech.service.impl.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 用户控制器 * * @author qsyang */ @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/user/user-index") public String index() { User user = userService.getUserById(1); if (user != null) { System.out.println("user name = " + user.getLoginName()); } return "jsp/user/index"; } }
大家到现在有没有理解这个架构呢?
如有疑问,请留言!版权声明:本文为博主原创文章,未经博主允许不得转载。