SSM框架学习(四),简单小项目---客户管理系统

上篇讲了SSM整合,既然整合完了,就要使用,想着之前javaweb结课设计做过SSH的客户管理系统,不过代码都是书上的,这次就自己写个SSM的客户管理系统吧。

SSM整合连接接上 : https://blog.csdn.net/ignite_/article/details/91358157
这个小项目很简单,就是对数据库的增删改查操作,所以不是很困难,但是对于刚接触SSM的来说,练手再合适不过了。

使用技术:

前台:html css JavaScript JQuery ajax 使用框架bootstrap
后台:java SpringMVC Spring Mybatis

页面效果
1.登陆页面

SSM框架学习(四),简单小项目---客户管理系统_第1张图片

2.注册页面

SSM框架学习(四),简单小项目---客户管理系统_第2张图片

3.主页页面

SSM框架学习(四),简单小项目---客户管理系统_第3张图片

4.添加客户和更改客户按钮实现

SSM框架学习(四),简单小项目---客户管理系统_第4张图片

除登陆和注册,其他几个功能与后台交流都是通过ajax异步传递json数据完成的

SSM框架学习(四),简单小项目---客户管理系统_第5张图片

目录结构

SSM框架学习(四),简单小项目---客户管理系统_第6张图片

jar包

      org.mybatis
      mybatis
      3.4.6
    

    
      mysql
      mysql-connector-java
      5.1.37
    
    
      org.springframework
      spring-core
      4.2.4.RELEASE
    
    
      org.springframework
      spring-aop
      4.2.4.RELEASE
    
    
      org.springframework
      spring-context
      4.2.4.RELEASE
    
    
      org.springframework
      spring-aspects
      4.2.4.RELEASE
    
    
      org.springframework
      spring-beans
      4.2.4.RELEASE
    
    
      org.springframework
      spring-expression
      4.2.4.RELEASE
    
    
      org.springframework
      spring-instrument
      4.2.4.RELEASE
    
    
      org.springframework
      spring-web
      4.2.4.RELEASE
    
    
      org.springframework
      spring-oxm
      4.2.4.RELEASE
    
    
      org.springframework
      spring-jdbc
      4.2.4.RELEASE
    
    
      org.springframework
      spring-orm
      4.2.4.RELEASE
    
    
      org.springframework
      spring-tx
      4.2.4.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.2.4.RELEASE
    
    
      org.springframework
      spring-context-support
      4.2.4.RELEASE
    
    
      org.springframework
      spring-test
      4.2.4.RELEASE
    


    
      org.mybatis
      mybatis-spring
      1.3.2
    

    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.8
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.9.8
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.9.8
    
    

具体代码:

1.applicationContext.xml(用于配置Mybatis,ioc和di操作)




 
     
     
     
     
 

 
     
     
 

 
     
     
 



 
     
 

实体类(domain)

public class Custom {
  private int id;
  private String name;
  private String password;
  private String mobile;
  private String Email;
  }

set和get方法就不放出来了,可以直接通过编译器自动生成

Dao层(mapper)

CustomMapper.java(Mybatis动态代理对象)

public interface CustomMapper {
  Custom login(@Param("name") String name, @Param("password") String password);
  boolean register(@Param("name") String name,
                   @Param("password") String password,
                   @Param("mobile") String mobile,
                   @Param("Email") String Email);

  List query();

  boolean delete(@Param("id") int id);

  boolean insert(@Param("id") String id,
                 @Param("name") String name,
                 @Param("password") String password,
                 @Param("mobile") String mobile,
                 @Param("email") String email);

  boolean update(@Param("oldid") String oldid,
                  @Param("id") String id,
                 @Param("name") String name,
                 @Param("password") String password,
                 @Param("mobile") String mobile,
                 @Param("email") String email);
}

CustomMapper.xml(Mybatis的Mapper映射文件)




    
    
        insert into custom(id,name,password,mobile,Email) values (null ,#{name},#{password},#{mobile},#{Email})
    
    
    
        delete from custom where id = #{id}
    
    
        insert into custom values (#{id},#{name},#{password},#{mobile},#{email})
    
    
        update custom  set id=#{id},name=#{name},password=#{password},mobile=#{mobile},Email=#{email} where  id=#{oldid}
    

Service层

CustomServiceImpl

package service.Impl;

import domain.Custom;
import mapper.CustomMapper;
import service.CustomService;

import java.util.List;

public class CustomServiceImpl implements CustomService {
    CustomMapper customMapper;

    public void setCustomMapper(CustomMapper customMapper) {
        this.customMapper = customMapper;
    }

    @Override
    public Custom login(String name, String password) {
        return customMapper.login(name,password);
    }

    @Override
    public boolean register(Custom custom) {
        return customMapper.register(custom.getName(),
                                    custom.getPassword(),
                                    custom.getMobile(),
                                    custom.getEmail());
    }

    @Override
    public List query() {
        return customMapper.query();
    }

    @Override
    public boolean delete(int id) {
        return customMapper.delete(id);
    }

    @Override
    public boolean insert(Custom custom) {
        return customMapper.insert(String.valueOf(custom.getId()),
                                    custom.getName(),
                                    custom.getPassword(),
                                    custom.getMobile(),
                                    custom.getEmail());
    }

    @Override
    public boolean update(int oldid, Custom custom) {
        return customMapper.update(String.valueOf(oldid),
                                    String.valueOf(custom.getId()),
                                    custom.getName(),
                                    custom.getPassword(),
                                    custom.getMobile(),
                                    custom.getEmail());
    }
}

这里会调用Dao层,可以将customMapper通过Spring的依赖注入,所以customMapper需要set方法

handle(原生代码结构中的Servlet)
package handle;

import domain.Custom;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CustomService;

import java.util.List;

@Controller
@RequestMapping("custom")
public class CustomHandle {
    @Autowired
    CustomService customService;
    @RequestMapping(value = "/login.handle",method = RequestMethod.POST)
    public String login(String name,String password){
        System.out.println(name);
        System.out.println(password);
        Custom custom = customService.login(name, password);
        if (custom!=null) {
            return "query";
        }
        else
        return "error";
    }

    @RequestMapping(value = "/register.handle",method = RequestMethod.POST)
    public String register(String name,String password,String mobile,String Email){
        Custom custom = new Custom();
        custom.setName(name);
        custom.setPassword(password);
        custom.setMobile(mobile);
        custom.setEmail(Email);
        System.out.println(custom);
        if (customService.register(custom)){
            return "query";
        }
        return "error";
    }
    @ResponseBody
    @RequestMapping(value = "/query.handle")
    public List query(){
        return customService.query();
    }

    @ResponseBody
    @RequestMapping(value = "/delete.handle")
    public boolean delete(int id){
        return customService.delete(id);
    }

    @ResponseBody
    @RequestMapping(value = "/insert.handle")
    public Custom insert(int id,String name,String password,String mobile,String email) {
        List query = customService.query();
        int b=0;
        for (int i = 0; i < query.size(); i++) {
            if (id == query.get(i).getId()) {
               b=1;
                break;
            }

        }
        if (b!=1) {
            Custom custom = new Custom();
            custom.setId(id);
            custom.setName(name);
            custom.setPassword(password);
            custom.setMobile(mobile);
            custom.setEmail(email);
            System.out.println(custom);
            if (customService.insert(custom)) {
                return custom;
            }
        }else{
            Custom custom = new Custom();
            custom.setName("false");
            return custom;
        }
        return null;

    }
    @ResponseBody
    @RequestMapping(value = "/update.handle")
    public Custom update(int oldid,int id,String name,String password,String mobile,String email){
        Custom custom = new Custom();
        custom.setId(id);
        custom.setName(name);
        custom.setPassword(password);
        custom.setMobile(mobile);
        custom.setEmail(email);
        System.out.println(custom);
        if (customService.update(oldid,custom)){
        return custom;
        }
        else return null;
        }
}

通过拦截请求,交由这个类处理请求。

applicationContext-springmvc.xml(Springmvc配置文件)



    
        
        
    

    
    
    
    
    
    

前台html
index.html(登陆页面)



    
    Title
    
    

    
    

    
    
    


客户管理系统

注册
html/register.html(注册页面)



    
    注册
    
    

    
    

    
    
    


欢迎注册

html/error.html



    
    Title


账号或密码错误返回主页


html/query.html(主要页面)



    
    Title
    
    

    
    

    
    
    




客户管理系统

id号 账号 密码 电话 Email 操作

前台页面主要就是使用了jquery+ajax+bootstrap框架,用于显示与和后台进行数据交互

web.xml配置



  
    index.html
  

  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:applicationContext-springmvc.xml
    
    1
  
  
    springmvc
    *.handle
  


注意事项

1.SpringMVC对静态资源访问问题

可以在SpringMVC配置文件中加入一下代码


    
    
    
    
2.Spring使用注解问题

例如在handle使用了Spring的注解,这里在需要调用Service层,若使用IOC和Di操作,会出现空指针,所以在创建Service对象时,使用注解自动装配即可

3.handle给前台传递Json

需要注意两个问题
第一个:
需要使用注解@ResponseBody即可直接返回对象数据
第二个:
需要jar包,并且版本一致


      com.fasterxml.jackson.core
      jackson-databind
      2.9.8
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.9.8
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.9.8
    
4.Dao层数据类型问题

在创建实体类的时候,id是用了int类型,存入数据库时,因为输入类型是String,所以,在接口中,应该强转int为String
SSM框架学习(四),简单小项目---客户管理系统_第7张图片
主要就是这样,不是很复杂,但是对想我这样的新手练习使用SSM很有帮助。
bootstrap用的还不是很熟练,更改信息的时候,样式全被撑开了。。。
嗯。。大概就是这样了。。。不到一天就做完了,还不错。。加油

仅供学习,如若有地方不对,敬请前辈指出,感激不尽。

你可能感兴趣的:(java)