`
blackchocolate
  • 浏览: 25653 次
  • 性别: Icon_minigender_2
  • 来自: 重庆
社区版块
存档分类
最新评论

SpringMVC+Spring+mybatis项目搭建详细过程

阅读更多
创建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&amp;useLocalSessionState=true&amp;elideSetAutoCommit=true&amp;useUsageAdvisor=false&amp;useReadAheadInput=false&amp;useUnbufferedInput=false&amp;cacheServerConfiguration=true&amp;autoReconnect=true&amp;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>
一些配置需要改成自己的配置,比如数据库账号密码等。
然后再配web.xml文件,配置如下:
<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>
    <!-- filter end -->

<!-- Handles all requests into the application -->
    <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>
    </servlet-mapping>
再把mybatis的配置文件配上,mybatis-config.xml配置如下:
  
    <typeAliases>  
        <typeAlias alias="User" type="com.mkyong.common.entity.User"/>  
    </typeAliases>  
    <mappers>  
        <mapper resource="User.xml" />
           
    </mappers> 
配置就配置好了,然后写mapper.xml和service层和controller层。
UserMapper.java文件如下:
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层接口和实现类:
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>
一个项目就搭建完了,没有任何多余的东西,只包含了最简单的mybatis和spring的应用。项目源码在附件中。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics