Eclipse上创建一个Spring+Spring MVC+Mybatis项目整合

断断续续摸索两周,把环境搭建起来了,在这记录一下

1.项目结构图
Eclipse上创建一个Spring+Spring MVC+Mybatis项目整合_第1张图片
image.png
2添加maven依赖

    4.0.0
    cn.com.xie
    Spring-SpringMvc-mybatis
    war
    0.0.1-SNAPSHOT
    Spring-SpringMvc-mybatis Maven Webapp
    http://maven.apache.org
    
        UTF-8
        UTF-8
        4.3.5.RELEASE
        3.4.1
        3.3.8
        7.0
        2.4
        5.1.41
        4.1.0
        1.3.1
        1.2.2
        1.8.6
        2.9.0
        5.2.4.Final
    

    

        
        
            javax
            javaee-api
            ${javaee.version}
        

        
        
            net.sf.json-lib
            json-lib
            jdk15
            ${net.sf.json}
        


        
        
            mysql
            mysql-connector-java
            ${mysql.version}
            runtime
        
        
        
            commons-dbcp
            commons-dbcp
            ${commons-dbcp.version}
        

        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
        
            com.github.pagehelper
            pagehelper
            ${pagehelper.varsion}
        

        
        
            org.mybatis
            mybatis-spring
            ${mybatis.spring.version}
        
        
            tk.mybatis
            mapper
            ${tk.mybatis.mapper.version}
        
        
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.aspectj
            aspectjweaver
            ${aspectj.version}
        
        
        
            junit
            junit
            RELEASE
        

        
        
            org.hibernate
            hibernate-validator
            ${validator.version}
        
        
        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        
        
        
            log4j
            log4j
            1.2.17
        
    
    
        spring-springmvc-mybatis
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    



3.配置数据库连接信息jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssmtest?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=xgr1234

4.配置web.xml



    Archetype Created Web Application

    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    


    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring/spring-*.xml
        
        
        1
        
        true
    
    
        SpringMVC
        /
    

   

    
        index.jsp
    




6.配置spring-mvc.xml



    
    

    
    

    
     

    
    
        
        
        
    




7.配置mybatis-config.xml


    

    
    

    
    

    
    
        
        
        
        
    

    
    
        
        
        
         
         
         
        
    

    
    
        
        
        
         
    

    
    
        
        
    

    
    


8.配置mybatis-config.xml





    
        
        
    



9.mapper数据库映射文件(TestMapper.xml)


 

    
    

    
    
    
    
        insert into user(ID,username,password,address)
        values(#{id},#{username},#{password},#{address})
    



10.数据库建表
Eclipse上创建一个Spring+Spring MVC+Mybatis项目整合_第2张图片
image.png
11.测试类
Controller
package com.xie.controller;

import java.util.List;

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

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xie.entity.Testuser;
import com.xie.service.ServiceTest;




/*
 * controller 控制器
 * 
 * */

@Controller
@RequestMapping("api/test")
public class ControllerTest {
    
    @Autowired
    private ServiceTest servicetest;
    
    /*
     * 查询所有用户信息
     * 
     * */
    @RequestMapping("/getUser")
    @ResponseBody
    public List query(){
        List userlist = servicetest.getAllUser();
        return userlist;
    }
    
    /*
     * 添加用户信息
     * 
     * */
    @RequestMapping("/add")
    @ResponseBody
    public boolean add(){
        return  servicetest.addoneuser();
    }
    
    
}

Service
package com.xie.service;

import java.util.List;

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


import com.xie.dao.Mapperuser;
import com.xie.entity.Testuser;





@Service
public class ServiceTest {

    @Autowired
    private Mapperuser mapperuser;
    
    /*查询所有用户信息*/
    
    public List getAllUser(){
        List list = mapperuser.queryTestInfo();
        return list;
    }
    
    /*插入一条数据*/
    
    public boolean addoneuser(){
        /*模拟post提交数据*/
        Testuser user=new Testuser();
        user.setId(4);
        user.setUsername("新增数据");
        user.setPassword("www.123");
        user.setAddress("北京");
        return mapperuser.adduser(user);
    }
    
    
}

Dao
package com.xie.dao;

import java.util.List;

import com.xie.entity.Testuser;

import tk.mybatis.mapper.common.Mapper;

public interface Mapperuser extends Mapper {
    
     /*查询用户信息*/
     List queryTestInfo();
     /*新增用户信息*/
     boolean adduser(Testuser user);
}

entity实体类
package com.xie.entity;

public class Testuser  {
    private Integer id;
    private String username;
    private String password;
    private String address;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Testuser [id=" + id + ", username=" + username + ", password=" + password + ", address=" + address
                + "]";
    }
    

    
}

10.测试结果
Eclipse上创建一个Spring+Spring MVC+Mybatis项目整合_第3张图片
image.png
Eclipse上创建一个Spring+Spring MVC+Mybatis项目整合_第4张图片
image.png
新增数据
Eclipse上创建一个Spring+Spring MVC+Mybatis项目整合_第5张图片
image.png

你可能感兴趣的:(Eclipse上创建一个Spring+Spring MVC+Mybatis项目整合)