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

Spring MVC、Spring和Mybatis整合环境搭建

程序员文章站 2022-05-11 17:19:12
...

一、基础环境准备

 

1. 创建Java Web工程,引入依赖的相关jar

    commons-logging-1.1.jar

    mybatis-3.3.0.jar

    mybatis-spring-1.2.4.jar

    mysql-connector-java-5.0.2.jar

    spring-aop-4.3.2.RELEASE.jar

    spring-beans-4.3.2.RELEASE.jar

    spring-context-4.3.2.RELEASE.jar

    spring-context-support-4.3.2.RELEASE.jar

    spring-core-4.3.2.RELEASE.jar

    spring-expression-4.3.2.RELEASE.jar

    spring-jdbc-4.3.2.RELEASE.jar

    spring-tx-4.3.2.RELEASE.jar

    spring-web-3.2.4.RELEASE.jar

    spring-webmvc-3.2.4.RELEASE.jar

 

2. 在mysql中创建数据库coredb,并创建测试表user

 

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(50) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', 'tuozixuan', '111111');

 

 

 

二、编写示例代码

1. 创建若干包

    com.tuozixuan.one.controller

    com.tuozixuan.one.service

    com.tuozixuan.one.service.impl

    com.tuozixuan.one.mapper

    com.tuozixuan.one.model

 

2. 编写示例代码

 

#############UserController#############

package com.tuozixuan.one.controller;

 

import javax.annotation.Resource;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

 

import com.tuozixuan.one.service.UserService;

 

@Controller

public class UserController

{

    @Resource

    private UserService userService;

    

    @RequestMapping("/getUser")

    public ModelAndView hello()

    {

        System.out.println("getUser start");

        userService.getUser();

        System.out.println("getUser end");

        ModelAndView mv = new ModelAndView("user");

        return mv;

    }

}

 

#############UserService#############

package com.tuozixuan.one.service;

 

import com.tuozixuan.one.model.UserModel;

 

public interface UserService

{

    UserModel getUser();

}

 

#############UserServiceImpl#############

package com.tuozixuan.one.service.impl;

 

import java.util.List;

 

import javax.annotation.Resource;

 

import org.springframework.stereotype.Service;

 

import com.tuozixuan.one.mapper.UserMapper;

import com.tuozixuan.one.model.UserModel;

import com.tuozixuan.one.service.UserService;

 

@Service

public class UserServiceImpl implements UserService

{

    @Resource

    private UserMapper userMapper;

    

    public UserModel getUser()

    {

        

        List<UserModel> userList = userMapper.getUser();

        if (userList.size() > 0)

        {

            System.out.println("user:" + userList.get(0).getUserName());

        }

        

        return null;

    }

    

}

 

 

#############UserMapper#############

package com.tuozixuan.one.mapper;

 

import java.util.List;

 

import com.tuozixuan.one.model.UserModel;

 

public interface UserMapper

{

    List<UserModel> getUser();

}

 

 

#############UserModel#############

package com.tuozixuan.one.model;

 

public class UserModel

{

    private String userName;

 

    public String getUserName()

    {

        return userName;

    }

 

    public void setUserName(String userName)

    {

        this.userName = userName;

    }

}

 

#############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="com.tuozixuan.one.mapper.UserMapper">  

 

<select id="getUser" resultType="com.tuozixuan.one.model.UserModel">  

   SELECT * FROM user  

</select>  

</mapper>

 

#############user.jsp#############

在WebRoot下创建user.jsp,内容任意

 

 

 

三、SSM整合配置文件

1. web.xml配置如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  	<!-- spring配置文件 -->
	<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring.xml</param-value>  
  	</context-param>
  	
  	<!-- Spring监听 -->
  	<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:spring-mvc.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>
</web-app>

 

 

2. 在src目录下新建spring-mvc.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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
     
    <!-- 注解的方式驱动 -->
	<mvc:annotation-driven />
	
    <!-- 对controller包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="com.tuozixuan.one"/>
    
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    
    <!--视图解析器 -->
    <bean id="viewResolverCommon" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    	<property name="prefix" value="/" />  
    	<property name="suffix" value=".jsp" />  
	</bean>  

</beans>

  

 

3. 在src目录下新建spring.xml配置文件

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 	xmlns:context="http://www.springframework.org/schema/context"
 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xmlns:util="http://www.springframework.org/schema/util"
 	xmlns:aop="http://www.springframework.org/schema/aop"
 	xmlns:tx="http://www.springframework.org/schema/tx"
  	xmlns:cache="http://www.springframework.org/schema/cache "
   	xmlns:p="http://www.springframework.org/schema/p"
 	xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/util 
      http://www.springframework.org/schema/util/spring-util.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.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/cache
      http://www.springframework.org/schema/cache/spring-cache-4.1.xsd ">	



	<!-- 数据源配置 -->
	<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/coredb?useUnicode=true&amp;characterEncoding=UTF-8"/>
		<property name="username" value="root"/>
		<property name="password" value="password"/>
	</bean>
	
	<!-- mybatis文件配置,扫描所有mapper文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="jdbcDataSource"></property>
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<property name="mapperLocations" value="classpath:com/tuozixuan/one/mapper/*.xml"></property>
	</bean>
	
	<!-- spring与mybatis整合配置,扫描所有dao -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<property name="basePackage" value="com.tuozixuan.one.mapper" ></property>
    	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" ></property>
  	</bean>
	

</beans>

 

 

 

4. 在src目录下新建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>

</configuration>

 

 

四、运行与结果

1. 把web工程部署到tomcat里,在浏览器中访问http://localhost:8080/one/getUser,发现可以访问到user.jsp, 并在控制台打印出了日志内容:

getUser start

user:tuozixuan

getUser end