SpringMVC+Spring+mybatis project construction detailed process

创建maven-web项目,为了方便的管理jar包。首先在maven中导入所需的包。在pom.xml中加入以下代码:<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
然后在spring的配置文件application.xml中添加如下代码:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
<property name="driverClassName" value="com.mysql.jdbc.Driver" />    
<property name="url" value="jdbc:mysql://localhost:3306/courseman?defaultFetchSize=25&useLocalSessionState=true&elideSetAutoCommit=true&useUsageAdvisor=false&useReadAheadInput=false&useUnbufferedInput=false&cacheServerConfiguration=true&autoReconnect=true&zeroDateTimeBehavior=convertToNull" />
   <property name="username" value="root" />
    <property name="password" value="mysql" />
</bean> 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
<property name="dataSource" ref="dataSource" /> 
   </bean> 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
    </bean> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />      
    <property name="basePackage" value="com.mkyong.common.mapper"/>       
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0"  ref="sqlSessionFactory" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Some configurations need to be changed to their own configuration, such as database account password and so on.
Then configure the web.xml file and configure it as follows:
<display-name>Spring MVC Application</display-name>

<!-- RESTful filter -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        < filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url -pattern>
    </filter-mapping>
    <!


    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:applicationContext.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/</url-pattern> Then add the configuration file of mybatis. The configuration of mybatis-config.xml is as follows:
    </servlet-mapping>

  
    <typeAliases>  
        <typeAlias ​​alias="User" type="com.mkyong.common.entity.User"/>  
    </typeAliases>  
    <mappers>  
        <mapper resource="User.xml" />
           
    </mappers> 
configuration is configuration Well, then write mapper.xml and service layer and controller layer.
The UserMapper.java file is as follows:
package com.mkyong.common.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.mkyong.common.entity.User;

public interface UserMapper {

public User getUserById(@Param("userId") int userId);
}


service layer interface and implementation class:
package com.mkyong.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mkyong.common.entity.User;
import com.mkyong.common.mapper.UserMapper;
import com.mkyong.common.service.HelloService;


@Service("HelloService")
public class HelloServiceImpl implements HelloService {

@Autowired
private UserMapper userMapper;

public User getUserById(int userId){
return userMapper.getUserById(userId);
}
}


package com.mkyong.common.service;

import com.mkyong.common.entity.User;


public interface HelloService {
public User getUserById(int userId);
}

controller层:
package com.mkyong.common.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mkyong.common.entity.User;
import com.mkyong.common.service.HelloService;


@Controller
@RequestMapping("/aaaa")
public class HelloController {

@Autowired
HelloService helloService;

@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
int userId = 1;
User user = helloService.getUserById(userId);
model.addAttribute("userName", user.getUsername());
model.addAttribute("userPaw", user.getPassword());
return "hello";

}
}


User.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.mkyong.common.mapper.UserMapper">  
    <select id="getUserById" parameterType="int"  resultType="User">  
          SELECT * FROM t_user WHERE id = #{userId}
    </select>  

</mapper> 


Hello.jsp:
<html>
<body>
<h1>userName : ${userName}</h1>
<h1>userPaw : ${userPaw}</h1>
</body>
</html>
A project is built, there is no extra stuff, only the most Simple application of mybatis and spring. The source code of the project is in the attachment.

Guess you like

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