eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建

这篇文章主要讲解在eclipse环境下SpringMVC+Maven+Mybatis+MySQL的项目搭建过程。

  1. 创建Maven工程。

  右击-->New->Other

  点击->Manven Porject

  点击->勾选快速框架

  输入项目名,包(Packaging,如果只是普通的项目,选jar就好了,如果是web项目就选war,这里是web项目,所以选择war)

  右击项目->Properties->Java Build Path->选择Libraries->点击选中 JRE System Library->Edit 

  选择默认JRE,注:必须系统安装了JDK。

  点击Project Facets 去掉勾选 Dynamic Web Module 再把 Java 改为系统设置版本。  

  再打开Project Facets ->勾选 Dynamic Web Module 选择3.0版本时,下面有个 Further configuration available... ->点击

  输入 content directory 地址: src/main/webapp

  创建好的目录如下:

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第1张图片

  1.  
  2. 配置Spring MVC

添加包的依赖,编辑pom.xml文件添加如下依赖:(因为我的JDK版本是1.8所以Spring 版本必须为4.0以上)

复制代码


    4.0.0
    com.liaoze
    stockmgt_maven_project
    0.0.1-SNAPSHOT
    war

    
        
        4.1.5.RELEASE
        
        1.6.6
        1.2.12
        
        4.10
        
        3.2.1
    

    
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        

        
        
            junit
            junit
            ${junit.version}
            test
        

        
        
        
            log4j
            log4j
            ${log4j.version}
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        

        
        
            org.springframework
            spring-test
            ${spring.version}
            test
        

        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        

        
        
            org.mybatis
            mybatis-spring
            1.2.0
        

        
        
            mysql
            mysql-connector-java
            5.1.29
        
    


复制代码

 

   加入配置文件

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第2张图片

  jdbc.properties

jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/stockmgt
jdbc_username=root
jdbc_password=123456

 

  application.xml

复制代码




       
     
        
            
               classpath:properties/*.properties
                
            
        
    


    
    
        
        
       
        
            ${jdbc_driverClassName}
        
        
            ${jdbc_url}
        
        
            ${jdbc_username}
        
        
            ${jdbc_password}
        
    

    
    
        
    

    
    
        
          
        
        
    

    
    

复制代码

 

   mybatis-config.xml

  
  
    

 

  spring-mvc.xml

复制代码




    
   
     

   
   

复制代码

 

  userMapper.xml

复制代码

  



    
        
        
        
    
    
    

复制代码

   进入mysql 数据库:

复制代码

/*新建数据库*/
CREATE DATABASE IF NOT EXISTS stockmgt DEFAULT CHARSET utf8;

/*新建表*/
COLLATE utf8_general_ci;
CREATE TABLE `t_user` (
  `USER_ID` INT(11) NOT NULL AUTO_INCREMENT,
  `USER_NAME` CHAR(30) NOT NULL,
  `USER_PASSWORD` CHAR(10) NOT NULL,
  PRIMARY KEY (`USER_ID`),
  KEY `IDX_NAME` (`USER_NAME`)
) ENGINE=INNODB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

/*插入测试数据*/
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD) VALUES (1, '张三', '123456');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD) VALUES (2, '李四', '123456');

复制代码

 

  编写java类:

  eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第3张图片

  User.java

复制代码

package com.liaoze.stockmgt.model;

public class User {
    
    private Integer userId;
    private String userName;
    private String userPassword;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

}

复制代码

  UserDao.java

复制代码

package com.liaoze.stockmgt.dao;

import com.liaoze.stockmgt.model.User;

public interface UserDao {
    /**
     * @param userId
     * @return User
     */
    public User selectUserById(Integer userId);
    
}

复制代码

 

  UserService.java

复制代码

package com.liaoze.stockmgt.service;

import com.liaoze.stockmgt.model.User;

public interface UserService {
    User selectUserById(Integer userId);  
}

复制代码

 

  UserServiceImpl.java

复制代码

package com.liaoze.stockmgt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.liaoze.stockmgt.dao.UserDao;
import com.liaoze.stockmgt.model.User;
import com.liaoze.stockmgt.service.UserService;

@Service  
public class UserServiceImpl implements UserService {

    @Autowired  
    private UserDao userDao;  

    public User selectUserById(Integer userId) {  
        return userDao.selectUserById(userId);  
    }  
}

复制代码

 

   编写测试类JUnit4进行单元测试:

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第4张图片

 

  SpringTestCase.java

复制代码

package com.liaoze.stockmgt.test;

import org.junit.runner.RunWith;  
import org.springframework.test.context.ContextConfiguration;  
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;  
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  

//指定bean注入的配置文件  
@ContextConfiguration(locations = { "classpath:application.xml" })  
//使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner  
@RunWith(SpringJUnit4ClassRunner.class)  
public class SpringTestCase extends AbstractJUnit4SpringContextTests {

}

复制代码

   UserServiceTest.java

复制代码

package com.liaoze.stockmgt.test.service;

import org.junit.Test;  
import org.springframework.beans.factory.annotation.Autowired;
import com.liaoze.stockmgt.model.User;
import com.liaoze.stockmgt.service.UserService;
import com.liaoze.stockmgt.test.SpringTestCase;  

public class UserServiceTest extends SpringTestCase {

    @Autowired  
    private UserService userService; 

    @Test  
    public void selectUserByIdTest(){  
        User user = userService.selectUserById(1);  
        System.out.println(user.getUserName() + ":" + user.getUserPassword());
    }  
}

复制代码

   选中方法名->右击->Run As -> 点击 JUnit Test

 eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第5张图片

  显示测试运行成功!

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第6张图片

  控制台输出如下:

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第7张图片

  新建Controller:

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第8张图片

 

  新建UserController.java

复制代码

package com.liaoze.stockmgt.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.liaoze.stockmgt.model.User;
import com.liaoze.stockmgt.service.UserService;

@Controller  
public class UserController {  

    @Resource  
    private UserService userService; 

    @RequestMapping("/")    
    public ModelAndView getIndex(){      
        ModelAndView mav = new ModelAndView("index"); 
        User user = userService.selectUserById(1); 
        mav.addObject("user", user); 
        return mav; 
    }    
}  

复制代码

  添加jsp 页面:在src/main/webapp/WEB-INF/新建view文件夹

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第9张图片

  再新建index.jsp 页面:

复制代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>



Hello World!

用户名: ${user.userName}
密码:${user.userPassword}

复制代码

 运行!

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第10张图片

  浏览器输入地址:http://127.0.0.1:8080/stockmgt_maven_project/

  无乱码!成功!

eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建_第11张图片

源代码:http://download.csdn.net/detail/liaoze2010/9854936

我的CSDN博客:http://blog.csdn.net/liaoze2010

最近实在是没分了,所以源代码下载,所需要1分的积分,见凉!

 

参考:http://www.open-open.com/lib/view/open1453195087105.html

 

你可能感兴趣的:(eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建)