标签:str mapping ping splay lan 分离 else cal myeclips
Spring MVC:MVC框架,通过Model-View-Controller模式很好的将数据,业务与展现进行分离。
MyBatis:数据持久层框架
我这里使用的是MyEclipse 2016 CI
下面是具体的搭建流程:
1、项目结构:
2、安装Spring
3、依赖包(其中包括Spring MVC,Spring MVC验证包,MyBatis整合SpringMVC所需包)
4、web.xml
<?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" version="3.0"> <display-name>SpringMVC3</display-name> <!-- 监听服务器启动 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 读取spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/springMVC-servlet.xml</param-value> </context-param> <!-- 配置SpringMVC核心 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定springMVC-servlet.xml路径 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/springMVC-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.form</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> </web-app>
5、jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jsp?characterEncoding=utf-8
username=root
password=************
6、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> <settings> <!-- 允许JDBC支持生成的键 --> <setting name="useGeneratedKeys" value="true"/> </settings> <typeAliases> <!-- 配置数据类型的别名 --> <typeAlias type="com.cn.pojo.User" alias="user"/> </typeAliases> </configuration>
7、springMVC-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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 读取外面文件 --> <context:property-placeholder location="classpath:config/jdbc.properties"/> <!-- 配置数据源 --> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> </bean> <!-- 将mybatis交给Spring管理 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasource"></property> <property name="configLocation" value="classpath:config/mybatis-config.xml"></property> </bean> <!-- 扫描 --> <context:component-scan base-package="com.cn.action,com.cn.service"></context:component-scan> <!-- 启动驱动 --> <mvc:annotation-driven/> <!-- 注入校验bean --> <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"></bean> <!-- 扫描mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cn.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
8、pojo封装数据层:User.java
package com.cn.pojo; import java.io.Serializable; import javax.validation.constraints.Pattern; public class User implements Serializable{ private int id; @Pattern(regexp="\\w{2,12}",message="用户名必须在2-12位之间") private String username; @Pattern(regexp="\\w{6,12}",message="密码必须在6-12位之间") private String password; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public User() { super(); // TODO Auto-generated constructor stub } public User(String username, String password, String address) { super(); this.username = username; this.password = password; this.address = address; } }
9、dao数据持久层:
UserDAO.java:
package com.cn.dao; import com.cn.pojo.User; public interface UserDAO { public void saveUser(User user); }
UserDAO.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.cn.dao.UserDAO"> <insert id="saveUser" parameterType="user"> insert into t_user(username,password,address) values(#{username},#{password},#{address}) </insert> </mapper>
10、service服务接口层:
UserService.java
package com.cn.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.cn.dao.UserDAO; import com.cn.pojo.User; @Service("userService") public class UserService implements UserServiceInterf{ @Autowired private UserDAO userDAO; public void saveUser(User user) { userDAO.saveUser(user); } public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } }
UserServiceInterf.java
package com.cn.service; import com.cn.pojo.User; public interface UserServiceInterf { public void saveUser(User user); }
11、action控制器层:UserAction.java
package com.cn.action; import javax.annotation.Resource; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import com.cn.pojo.User; import com.cn.service.UserServiceInterf; @Controller @RequestMapping("/") public class UserAction { @Resource(name="userService") private UserServiceInterf userService; @RequestMapping("/regist") public String saveUser(@Valid @ModelAttribute("user") User user,BindingResult br){ if(br.hasErrors()){ return "regist.jsp"; }else{ userService.saveUser(user); return "welcome.jsp"; } } public UserServiceInterf getUserService() { return userService; } public void setUserService(UserServiceInterf userService) { this.userService = userService; } }
12、regist.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘regist.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"> --> <script type="text/javascript" src="js/jquery-1.12.3.min.js"></script> <script type="text/javascript"> $(function(){ alert("hello"); }); </script> </head> <body style="background: url(img/bground.jpg)"> <form:form modelAttribute="user" action="regist.form"> 用户名:<input type="text" value="${user.username }" name="username"/><span style="color:red"><form:errors path="username"></form:errors></span><br/> 密码:<input type="password" value="${user.password }" name="password"/><span style="color:red"><form:errors path="password"></form:errors></span><br/> 地址:<input type="text" value="${user.address }" name="address"/><br/> <input type="submit" value="注册"/> </form:form> </body> </html>
SMM框架的搭建和测试(Spring MVC+MyBatis)
标签:str mapping ping splay lan 分离 else cal myeclips
原文地址:http://www.cnblogs.com/Vito-Yan/p/7911833.html