SpringMVC与Mybatis集成开发环境搭建
程序员文章站
2022-05-31 21:04:29
...
SpringMVC与Mybatis集成开发环境搭建
1、导入相关JAR包
2、构建项目目录
3、配置文件
- web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>SpringMVC_Mybatis</display-name> <!-- 配置请求转发处理 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/web-config.xml, /WEB-INF/service-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
- web-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 配置数据源 --> <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe" /> <property name="username" value="scott" /> <property name="password" value="tiger" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jdbcDataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <!-- 让MapperFactoryBean来管理dao --> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="cn.richinfo.dao.UserDao" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- Controller 方法调用规则定义 --> <bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="action" /> <property name="defaultMethodName" value="list"/> </bean> <!-- 页面View层基本信息设定 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="suffix" value=".jsp" /> </bean> <!-- servlet映射列表,所有控制层Controller的servlet在这里定义 --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="user.do">userController</prop> </props> </property> </bean> <bean id="userController" class="cn.richinfo.controller.UserController" > <property name="userService" ref="userService" /> </bean> </beans>
- service-config.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="userService" class="cn.richinfo.service.UserService"> <property name="userDao" ref="userDao" /> </bean> </beans>
- 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="cn.richinfo.pojo.User" /> </typeAliases> <mappers> <mapper resource="mappers/UserMapper.xml"/> </mappers> </configuration>
- UserMapper.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="cn.richinfo.dao.UserDao"> <insert id="insertUser" parameterType="User"> INSERT INTO spring_user(user_id , user_name) VALUES (spring_user_sq.nextval , #{userName}) </insert> </mapper>
- UserController.java
package cn.richinfo.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import cn.richinfo.pojo.User; import cn.richinfo.service.UserService; public class UserController implements Controller { private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("UserController.handleRequest()"); request.setAttribute("userName", request.getParameter("userName")); User user = new User(); user.setUserName(request.getParameter("userName")); userService.saveUser(user); return new ModelAndView("index"); } }
- UserService.java
package cn.richinfo.service; import cn.richinfo.dao.UserDao; import cn.richinfo.pojo.User; public class UserService { private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void saveUser(User user) { System.out.println("userService : 存储用户..."); userDao.save(user); } }
- UserDao.java
package cn.richinfo.dao; import cn.richinfo.pojo.User; public interface UserDao { public void insertUser(User user); }
- User.java
package cn.richinfo.pojo; public class User { private int userId; private String userName; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String toString() { return "{'userId' : " + this.userId + ", 'userName' : " + this.userName + "}"; } }
- index.jsp
<%@ 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>用户名</title> </head> <body> <label >${requestScope.userName }</label> </body> </html>
-
### 创建数据库表 create table spring_user ( user_id number, user_name varchar(20) not null, constraint spring_user_id_pk primary key(user_id) ) ### 自动主键生成 create sequence spring_user_sq start with 1 increment by 1
请求:http://localhost:9000/SpringMVC_Mybatis/user.do?userName=liyang
推荐阅读
-
PHP开发环境搭建一:PHP集成环境XAMPP的安装与配置
-
AngularJS整合Springmvc、Spring、Mybatis搭建开发环境
-
详解webpack与SPA实践之开发环境搭建
-
Vue2.0开发环境与脚手架项目搭建批处理脚本文件
-
Android SDK 2.3与Eclipse最新版开发环境搭建(一) AndroidEclipseGoogleLinuxWindows
-
DCMTK 3.6.2(MD支持库)与Microsoft Visual Studio 2017开发环境的搭建
-
VirtualBox linux开发虚拟环境搭建踩坑(二)与主机文件共享
-
android NDK开发、编译、调试环境搭建与操作入门
-
OpenCV3.4.1安装与VS2017下开发环境搭建
-
【AOP 的实现之Aspectj(一)】:AspectJ开发环境搭建与Hello World