整合SSM
1.环境搭建
- 导入相关的pom依赖
com.fasterxml.jackson.core
jackson-databind
2.9.8
com.alibaba
fastjson
1.2.62
org.mybatis
mybatis
3.5.2
org.mybatis
mybatis-spring
2.0.2
org.springframework
spring-webmvc
5.1.9.RELEASE
org.springframework
spring-jdbc
5.1.9.RELEASE
junit
junit
4.12
mysql
mysql-connector-java
5.1.47
com.mchange
c3p0
0.9.5.2
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.2
commons-fileupload
commons-fileupload
1.3.3
javax.servlet
javax.servlet-api
4.0.1
- Maven资源过滤设置
src/main/java
**/*.properties
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
false
- 建立基本结构
java目录下
- com.lgw.dao
- com.lgw.pojo
- com.lgw.service
- com.lgw.controller
resources目录下
- mapper
- mybatis
- spring
- 配置框架
mybatis目录下:mybatis-config.xml
Spring目录下:applicationContext.xml
Spring目录下:spirng-dao.xml
Spring目录下:spirng-service.xml
Spring目录下:spirng-mvc.xml
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
log4j ....
2.Mybatis层
Mybatis-config.xml
pojo实体类(在此省略方法)
package com.lgw.pojo;
public class Admin {
private int adminId;
private String adminName;
private String adminPassword;
}
dao层Mapper接口
package com.lgw.dao.admin;
import com.lgw.pojo.Admin;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public interface AdminMapper {
/**
* 查询所有管理员
* @return
*/
List QueryAdmins();
Admin QueryAdminById(@Param("id") int id);
/**
* 增加一个管理员
* @param admin
* @return
*/
int addAdmin(Admin admin);
/** 删除一个管理员
* @param id
* @return
*/
int deleteAdmin(@Param("id") int id);
/** 删除一个管理员
* @param map
* @return
*/
int updateAdmin(Map map);
}
对应的*Mapper.xml
insert into admin values(admin_name,admin_password)
delete from admin where admin_id=#{id}
update admin set admin_name=#{name} admin_password=#{password}
WHERE admin_id = #{id}
编写Service层的接口和实现类
接口
package com.lgw.service.admin;
import com.lgw.pojo.Admin;
import java.util.List;
import java.util.Map;
public interface AdminService {
/**
* 查询所有管理员
* @return
*/
List QueryAdmins();
Admin QueryAdminById( int id);
/**
* 增加一个管理员
* @param admin
* @return
*/
int addAdmin(Admin admin);
/** 删除一个管理员
* @param id
* @return
*/
int deleteAdmin(int id);
/** 删除一个管理员
* @param map
* @return
*/
int updateAdmin(Map map);
}
实现类
package com.lgw.service.admin;
import com.lgw.dao.admin.AdminMapper;
import com.lgw.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
public class AdminServiceImpl implements AdminService {
// service层调dao层
private AdminMapper adminMapper;
public void setAdminMapper(AdminMapper adminMapper) {
this.adminMapper = adminMapper;
}
public List QueryAdmins() {
return adminMapper.QueryAdmins();
}
public Admin QueryAdminById(int id) {
return adminMapper.QueryAdminById(id);
}
public int addAdmin(Admin admin) {
return adminMapper.addAdmin(admin);
}
public int deleteAdmin(int id) {
return adminMapper.deleteAdmin(id);
}
public int updateAdmin(Map map) {
return adminMapper.updateAdmin(map);
}
}
3.Spring层
整合Mybtais配置文件
spring-dao.xml
Mapper接口加注解,纳入Spring管理
@Repository
public interface AdminMapper {
}
Spring整合Service层
Service层实现类注解
@Service
public class AdminServiceImpl implements AdminService {
// service层调dao层
@Autowired
@Qualifier("adminMapper")
private AdminMapper adminMapper;
}
4.SpringMVC层
- 配置web.xml
index.jsp
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/applicationContext.xml
1
SpringMVC
/
encoding-fiflter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding-fiflter
/*
15
- spring-mvc.xml
- Spring配置文件整合 applicationContext.xml
- 代码编写
controller层
package com.lgw.controller;
import com.lgw.pojo.Admin;
import com.lgw.service.admin.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class AdminController {
// Controller 调用service
@Autowired
@Qualifier("adminServiceImpl")
private AdminService adminService;
@RequestMapping("/admin/all")
public String allAdmins(Model model){
List admins = adminService.QueryAdmins();
model.addAttribute("admins",admins);
return "Admins";
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
首页
查询管理员
在web-inf下创建view/Admin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${admins}
基本的SSM框架搭建成功,开始测业务了