大家好,又见面了,我是你们的朋友全栈君。
comi single blog(欢迎访问我的个人博客)SSM 整合 整合的思路是:
先创建spring框架
通过spring整合spring mvc
通过spring整合mybatis
工程创建 创建Maven工程–>create for archtype–>webapp
创建项目结构
在recourses目录下创建 dbconfig.properties,log4j.properties,mysqlConfig.xml,springmvc.xml,applicationContext.xml
工作流程在pom.xml添加相关依赖 — mybatis代码语言:javascript复制
基本思路建立spring框架1.创建bean
代码语言:javascript复制public class user implements Serializable {
private Integer id;
private String name;
private String gender;
private String email;
@Override
public String toString() {
return "user{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}2.创建bean的dao
代码语言:javascript复制public interface userDao {
public List
}3.创建bean的userService的接口
代码语言:javascript复制package club.twzw.service;
import club.twzw.bean.User;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
public List
}4.实现userServiceImpl,并给userServiceImpl 起别名userService
代码语言:javascript复制@Service("userService") <<<<-----------
public class userServiceImpl implements userService {
@Override
public List
System.out.println("查询所有用户。。。");
return null;//service.findAll();
}
}5.在resources文件下创建文件applicationContext.xml
代码语言:javascript复制
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
代码语言:javascript复制 @Test
public void testQueryUserList() {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
userService service = (userService) ac.getBean("userService"); // 因为给service起了别名,所以通过id的方式获取class
service.findAll();
}输出
至此spring框架已经创建。
建立spring mvc在web.xml文件中修改并配置过滤器,中文乱码过滤
代码语言:javascript复制
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
代码语言:javascript复制
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
import club.twzw.bean.User;
import club.twzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService service;
@RequestMapping("/findAll")
public String findAll(Model model){
System.out.println("success");
List
model.addAttribute("user",all);
return "list";
}
}index.jsp代码语言:javascript复制<%@ page contentType="text/html;charset=UTF-8" language="java" %>
success.jsp代码语言:javascript复制<%@ page contentType="text/html;charset=UTF-8" language="java" %>
scuccess
页面如果做成显示,自此成功添加spring mvc 开始整合在整合之前需要明白,我们需要在controller中调用service,最快捷的便是使用依赖注入,而至今使用Tomcat服务器只加载了springmvc.xml文件,并没有applicationContext.xml的加载(也就是spring并没有被加载),所以可以通过监听ServeltContext域对象,在创建时加载spring的配置文件(applicationContext.xml)
配置监听器在web.xml文件下添加listener,context-param设置监听和applicationContext.xml的文件路径
代码语言:javascript复制
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <<<<------ <<<<------
使用依赖注入并从新发布,如果正常,就可以在控制台看到两句话代码语言:javascript复制package club.twzw.controller;
import club.twzw.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserService service;
@RequestMapping("/findAll")
public String findAll(){
System.out.println("success");
service.findAll();
return "success";
}
}
这样spring mvc 就已经整合完毕了
建立mybatis环境在UserDao中使用注解查询
代码语言:javascript复制package club.twzw.dao;
import club.twzw.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserDao {
@Select("select * from user")
public List
}创建mybatis的核心配置文件mysqlConfig.xml
代码语言:javascript复制
PUBLIC "-//mybatis.org.//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
书写测试方法代码语言:javascript复制 @Test
public void test() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("mysqlConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(resourceAsStream);
SqlSession session = factory.openSession(true);
UserDao mapper = session.getMapper(UserDao.class);
List
for (User user : all) {
System.out.println(user);
}
}输出结果:
说明了mybatis可用,那么可以开始整合了
整理mybatis,思路相同,同样使用依赖注入,将mysqlConfig.xml添加到容器中,并自动注入在spring的文件中整合mybatis,配置连接池,factory,dao所在的包,此时有无将mysqlConfig.xml都不重要!
代码语言:javascript复制
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
代码语言:javascript复制@Repository <<<<<--------
public interface UserDao {
@Select("select * from user")
public List
}在serviceimpl中注入接口
代码语言:javascript复制package club.twzw.service.impl;
import club.twzw.bean.User;
import club.twzw.dao.UserDao;
import club.twzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("UserService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao dao;
public List
System.out.println("查询所有用户。。。");
return dao.findAll();
}
}修改controllerfindAll方法
代码语言:javascript复制package club.twzw.controller;
import club.twzw.bean.User;
import club.twzw.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService service;
@RequestMapping("/findAll")
public String findAll(Model model){
System.out.println("success");
List
model.addAttribute("user",all);
return "list";
}
}添加list.jsp
代码语言:javascript复制<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/10/29
Time: 21:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
实现访问
实现插入controller
代码语言:javascript复制 @RequestMapping("/save")
public String save(Model model){
System.out.println("success");
User u = new User();
u.setEmail("184611875@qq.com");
u.setGender("男");
u.setName("comi");
boolean b = service.Save(u);
System.out.println(b);
return "success";
}UserService
代码语言:javascript复制package club.twzw.service;
import club.twzw.bean.User;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
public List
public boolean Save(User u);
}UserServiceImpl
代码语言:javascript复制package club.twzw.service.impl;
import club.twzw.bean.User;
import club.twzw.dao.UserDao;
import club.twzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("UserService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao dao;
public List
System.out.println("查询所有用户。。。");
return dao.findAll();
}
@Override
public boolean Save(User u) {
return dao.Save(u);
}
}dao
代码语言:javascript复制package club.twzw.dao;
import club.twzw.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserDao {
@Select("select * from user")
public List
@Insert("insert into user (name,gender,email) values(#{name},#{gender},#{email})")
public boolean Save(User u);
}输出:
访问路径(http://localhost:8080/ssmWork_war_exploded/save)
这样我们的ssm框架就完成整合了,可以去干大事了!!!!
码云开源库:码云链接
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131315.html原文链接:https://javaforall.cn