欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC与Mybatis集成开发环境搭建

程序员文章站 2022-05-31 21:04:29
...

SpringMVC与Mybatis集成开发环境搭建

 

1、导入相关JAR包


SpringMVC与Mybatis集成开发环境搭建
            
    
    博客分类: Web后端 springspringMVCmybatis集成 
 

2、构建项目目录


SpringMVC与Mybatis集成开发环境搭建
            
    
    博客分类: Web后端 springspringMVCmybatis集成 
 
 

3、配置文件

  1. 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>
    
     
  2. 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>
    
     
  3. 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>
    
     
  4. 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>
     
  5. 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>
    
     
  6. 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");
        }
    }
    
     
  7. 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);
    	}
    }
    
     
  8. UserDao.java
    package cn.richinfo.dao;
    
    import cn.richinfo.pojo.User;
    
    public interface UserDao {
    
    	public void insertUser(User user);
    }
    
     
  9. 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 + "}";
    	}
    }
    
     
  10. 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>
     
  11. ### 创建数据库表
    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
  • SpringMVC与Mybatis集成开发环境搭建
            
    
    博客分类: Web后端 springspringMVCmybatis集成 
  • 大小: 14.7 KB
  • SpringMVC与Mybatis集成开发环境搭建
            
    
    博客分类: Web后端 springspringMVCmybatis集成 
  • 大小: 15.2 KB