SSM整合小demo,菜鸟一枚,有误之处请指正谢谢各位大佬

 

 

项目结构如下:

SSM整合小demo,菜鸟一枚,有误之处请指正谢谢各位大佬_第1张图片

项目依赖如下:


    
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            commons-io
            commons-io
            2.5
        
        
            junit
            junit
            4.11
            test
        
        
            org.mybatis
            mybatis
            3.4.6
        
        
            mysql
            mysql-connector-java
            5.1.45
        
        
        
            org.springframework
            spring-context
            5.0.4.RELEASE
        
        
            org.springframework
            spring-beans
            5.0.4.RELEASE
        
        
            org.springframework
            spring-webmvc
            5.0.4.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.0.4.RELEASE
        
        
            org.springframework
            spring-aop
            5.0.4.RELEASE
        
        
            org.springframework
            spring-aspects
            5.0.4.RELEASE
        
        
            org.springframework
            spring-jms
            5.0.4.RELEASE
        
        
            org.springframework
            spring-context-support
            5.0.4.RELEASE
        
        
            org.springframework
            spring-test
            5.0.4.RELEASE
        
        
            org.mybatis
            mybatis-spring
            1.3.2
        
        
            org.springframework
            spring-web
            5.0.6.RELEASE
        
        
            com.alibaba
            druid
            1.1.6
        
        
            com.alibaba
            fastjson
            1.2.28
        
        
            org.junit.jupiter
            junit-jupiter-api
            5.1.0
        
    

    
        ssm_demo

        
            
            
                src/main/java
                
                    **/*.properties
                    **/*.xml
                
                false
            
        
    

添加controller:

package com.haochen.controller;

import com.haochen.pojo.User;
import com.haochen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.ContextLoaderListener;

import javax.sql.DataSource;

/**
 * @author zhang
 * @date 2019年03月13日 20:12
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/user/{id}")
    public User getUser(@PathVariable Integer id){
        System.out.println("查询");
        return userService.getUser(id);
    }
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public int save( User user){
        System.out.println("添加");
        return userService.save(user);
    }
    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public void modify(@RequestBody User user){
        System.out.println("修改");
         userService.modify(user);
    }
    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    public void save( @PathVariable int id){
        System.out.println(1111);
         userService.delete(id);
    }
}

添加service:

package com.haochen.service;

import com.haochen.pojo.User;

/**
 * @author zhang
 * @date 2019年03月13日 20:36
 */
public interface UserService {
   User getUser(int id);
   int save(User user);
    void delete(int id);
    void modify(User user);
}

添加实现类:

package com.haochen.service.impl;

import com.haochen.dao.UserMapper;
import com.haochen.pojo.User;
import com.haochen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhang
 * @date 2019年03月13日 20:37
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    public User getUser(int id) {
        return userMapper.findUserById(id);
    }

    public int save(User user) {
        return userMapper.save(user);
    }

    public void delete(int id) {
        userMapper.deleteUser(id);
    }

    public void modify(User user) {
        userMapper.updateUser(user);
        //int i=1/0;
    }

}

添加mapper接口和xml

UserMapper.java

package com.haochen.dao;
import com.haochen.pojo.User;
public interface UserMapper {
     int save(User user);
     User findUserById(int id);
     void deleteUser(int id);
     void updateUser(User user);
}

UserMapper.xml 




    
    
        
            SELECT LAST_INSERT_ID()
        
        INSERT INTO USER (username,sex,birthday,address) VALUES (#{username},#{sex},#{birthday},#{address})
    
    
        DELETE  from USER WHERE id= #{id}
    
    
        update user set username=#{username},sex=#{sex},birthday=#{birthday} where id=#{id}
    


添加pojo:

package com.haochen.pojo;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class User implements Serializable {
	private int id;
	private String username;// 用户姓名
	private String sex;// 性别
	@DateTimeFormat(pattern="yyyy-mm-dd")
	private Date birthday;// 生日
	private String address;// 地址

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", sex=" + sex
				+ ", birthday=" + birthday + ", address=" + address + "]";
	}

}

添加mybatis配置sqlmapconfig.xml





    
        
        
        
        
        
    




db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

 spring的配置文件

applicationContext-dao.xml




    

    

    
        
        
        
        
        
        
    

    
        
        
        
      
    

    
        
    
    
   



applicationContext-service.xml 



    


applicationContext-trans.xml  




   
    
    
        
    
    
    
    
    
        
            
            
            
            
            
            
            
            
            
            
        
    
    
    
        
    



springmvc.xml




    
    
    
        
            
                
                
                    
                        
                        WriteMapNullValue
                        
                        
                        WriteDateUseDateFormat
                    
                
            
        
    

    
    
        
        
        
    



web.xml




  Archetype Created Web Application
  
    contextConfigLocation
    classpath:spring/applicationContext-*.xml
  
  
  
    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
    
      forceEncoding
      true
    
  
  
    CharacterEncodingFilter
    /*
  

  
    org.springframework.web.context.ContextLoaderListener
  

  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring/springmvc.xml
    
  
  
    springmvc
    /
  

SSM整合小demo,菜鸟一枚,有误之处请指正谢谢各位大佬_第2张图片

你可能感兴趣的:(框架整合,ssm整合)