栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springMVC+mybatis+maven框架搭建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springMVC+mybatis+maven框架搭建

怕忘记搭建步骤如下;

1.新建

项目新建完成

我们看到jsp页面在报错,我们只要配置jdk和tomcat 就好了

2.点击项目build path配置jdk1.8,tomcat 8.5

 3.新增文件夹src/test/resorces,因为框架一般有4个,所以我们需要手动添加一个

 tes

 4.删除web.xml因为版本太低了,然后重新配置一个,如下

把勾去掉,点击apply,然后勾上选择2.5,然后配置web.xml

 

 5.pom.xml配置引用

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    4.0.0
    com.demo
    ssm_demo
    war
    0.0.1-SNAPSHOT
    study_demo Maven Webapp
    http://maven.apache.org
    
    

        5.0.0.RELEASE

        1.1.3
        3.2.7
        1.4.0
        2.17.1
        2.9.5


        3.0.1

        4.12

        1.8
        1.2
        1.1.2
        2.3.2
    
    
        
            junit
            junit
            4.12
            test
        

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

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

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

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


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

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


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

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

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

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

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

        
            org.springframework
            spring-context-support
            ${org.springframework.version}
        

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


        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        

        
        
        
            org.mybatis
            mybatis-spring
            1.3.2
        


        
        
            mysql
            mysql-connector-java
            8.0.28
        

        
        
        
            javax.servlet
            jstl
            1.2
        

        
        
        


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

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


        
        
            commons-fileupload
            commons-fileupload
            1.3.3
        

        
            commons-io
            commons-io
            2.4
        

        
            commons-codec
            commons-codec
            1.9
        


        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        

        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson.version}
        

        
            com.fasterxml.jackson.core
            jackson-annotations
            ${jackson.version}
        

        
        
            com.github.pagehelper
            pagehelper
            5.1.2
        

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                

            

        

        ssm_demo
    


 

6.配置文件

applicationContext.xml代码如下


    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.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 ">

    

    
             >
        
    

    
    
                     expression="org.springframework.stereotype.Controller" />
    

    
    

db.properties

 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/study
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123

log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
--------------------- 
u4F5Cu8005uFF1A 
u6765u6E90uFF1A
u539Fu6587uFF1A
u7248u6743u58F0u660EuFF1Au672Cu6587u4E3Au535Au4E3Bu539Fu521Bu6587u7AE0uFF0Cu8F6Cu8F7Du8BF7u9644u4E0Au535Au6587u94FEu63A5uFF01

mybatis.xml


  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">

   
   
       
   

    

  
   
   
       
           
           
           
       

   

spring-mvc.xml


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd ">

    
    

    
    

    


    

    
             
        id="internalResourceViewResolver">
    
        
    
        
     

    
             >
        
        
        
    

spring-mybatis.xml


    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.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 ">

    
    
             >
        
        
        
        
        

    

   
    
        
        
        
             public String index(Model model){
        model.addAttribute("msg", "");
        return "login";
    }
    
    @RequestMapping(value="/login")
    public String login(Model model, // 向前台页面传的值放入model中
            HttpServletRequest request // 从前台页面取得的值
            ){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        boolean flag = LoginCheck(username, password);
        if(flag){
            return "userList";
        }else{
            model.addAttribute("msg", "用户名或密码不正确,请重新登录");
            return "login";
        }
    }
    
   
    private boolean LoginCheck(String username,String password){
        User user = userService.selectByUserName(username);
        if(user == null || "".equals(user)){
            return false;
        }
        if(user.getPassword().equals(password)){
            return true;
        }else{
            return false;
        }
    }

}
 

UserController.java

package com.demo.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.entity.Msg;
import com.demo.entity.User;
import com.demo.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class UserController {
    @Autowired
    IUserService userService;

    
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    @ResponseBody
    public Msg getUser(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {// 分页参数

        
        PageHelper.startPage(pn, 7);
        // startPage后紧跟的查询即为分页查询
        List list = userService.userList();
        // 使用pageInfo包装查询后的结果集,封装详细的分页信息,5是连续显示5页
        PageInfo pageInfo = new PageInfo(list, 5);
        return Msg.success().add("pageInfo", pageInfo);
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @ResponseBody
    public Msg addUser(User user) {

        userService.addUser(user);

        return Msg.success();
    }

}
login.jsp


    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.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 ">

    
    
             >
        
        
        
        
        

    

   
    
        
        
        
        
    

    
    
    
        
        
    


    
    
             >
        
    

    
    
    
    


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号