SSM整合连接接上 : https://blog.csdn.net/ignite_/article/details/91358157
这个小项目很简单,就是对数据库的增删改查操作,所以不是很困难,但是对于刚接触SSM的来说,练手再合适不过了。
前台:html css JavaScript JQuery ajax 使用框架bootstrap
后台:java SpringMVC Spring Mybatis
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
public class Custom {
private int id;
private String name;
private String password;
private String mobile;
private String Email;
}
set和get方法就不放出来了,可以直接通过编译器自动生成
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}
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方法
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;
}
}
通过拦截请求,交由这个类处理请求。
Title
客户管理系统
注册
欢迎注册
Title
账号或密码错误返回主页
Title
客户管理系统
id号
账号
密码
电话
Email
操作
前台页面主要就是使用了jquery+ajax+bootstrap框架,用于显示与和后台进行数据交互
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
可以在SpringMVC配置文件中加入一下代码
例如在handle使用了Spring的注解,这里在需要调用Service层,若使用IOC和Di操作,会出现空指针,所以在创建Service对象时,使用注解自动装配即可
需要注意两个问题
第一个:
需要使用注解@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
在创建实体类的时候,id是用了int类型,存入数据库时,因为输入类型是String,所以,在接口中,应该强转int为String
主要就是这样,不是很复杂,但是对想我这样的新手练习使用SSM很有帮助。
bootstrap用的还不是很熟练,更改信息的时候,样式全被撑开了。。。
嗯。。大概就是这样了。。。不到一天就做完了,还不错。。加油