Spring MVC, Spring and Mybatis integrated environment to build

First, the basic environment preparation

 

1. Create a Java Web project and introduce dependent jars

    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. Create the database coredb in mysql and create the test table 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');

 

 

 

2. Write sample code

1. Create several packages

    com.tuozixuan.one.controller

    com.tuozixuan.one.service

    com.tuozixuan.one.service.impl

    com.tuozixuan.one.mapper

    com.tuozixuan.one.model

 

2. Write sample code

 

#############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#############

Create user.jsp under WebRoot with arbitrary content

 

 

 

3. SSM integration configuration file

1. The web.xml configuration is as follows:

 

<?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 configuration file-->
	<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring.xml</param-value>  
  	</context-param>
  	
  	<!-- Spring listening -->
  	<listener>
	 	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
  	<!-- Spring MVC configuration-->
	<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. Create a new spring-mvc.xml configuration file in the src directory

 

<?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">
     
    <!-- Annotation-driven -->
	<mvc:annotation-driven />
	
    <!-- Scan all classes in the controller package to complete the functions of Bean creation and automatic dependency injection-->
    <context:component-scan base-package="com.tuozixuan.one"/>
    
    <!-- Start the annotation function of Spring MVC and complete the mapping of requests and annotation POJOs -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    
    <!--View resolver-->
    <bean id="viewResolverCommon" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    	<property name="prefix" value="/" />  
    	<property name="suffix" value=".jsp" />  
	</bean>  

</beans>

  

 

3. Create a new spring.xml configuration file in the src directory

 

<?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 ">	



	<!-- Data source configuration -->
	<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&characterEncoding=UTF-8"/>
		<property name="username" value="root"/>
		<property name="password" value="password"/>
	</bean>
	
	<!-- mybatis file configuration, scan all mapper files -->
	<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 and mybatis integrated configuration, scan all 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. Create a new mybatis-config.xml configuration file in the src directory

 

<?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>

 

 

4. Operation and Results

1. Deploy the web project to tomcat, visit http://localhost:8080/one/getUser in the browser, find that you can access user.jsp, and print out the log content on the console:

getUser start

user:tuozixuan

getUser end

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327084584&siteId=291194637