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
junit
junit
4.12
commons-logging
commons-logging
1.2
log4j
log4j
1.2.17
mysql
mysql-connector-java
5.1.41
com.mchange
c3p0
0.9.5.1
com.mchange
mchange-commons-java
0.2.10
org.mybatis
mybatis
3.4.2
org.mybatis
mybatis-spring
1.3.1
taglibs
standard
1.1.2
jar
javax.servlet
jstl
1.2
jar
javax.servlet.jsp
javax.servlet.jsp-api
2.3.3
provided
javax.servlet
javax.servlet-api
4.0.1
provided
com.fasterxml.jackson.core
jackson-annotations
2.8.6
com.fasterxml.jackson.core
jackson-core
2.8.1
com.fasterxml.jackson.core
jackson-databind
2.7.5
org.springframework
spring-core
4.3.7.RELEASE
org.springframework
spring-beans
4.3.7.RELEASE
org.springframework
spring-context
4.3.7.RELEASE
org.springframework
spring-expression
4.3.7.RELEASE
org.springframework
spring-aop
4.3.7.RELEASE
org.springframework
spring-aspects
4.3.7.RELEASE
org.aspectj
aspectjrt
1.8.13
org.aspectj
aspectjweaver
1.8.13
aopalliance
aopalliance
1.0
org.springframework
spring-jdbc
4.3.7.RELEASE
org.springframework
spring-tx
4.3.7.RELEASE
org.springframework
spring-web
4.3.7.RELEASE
org.springframework
spring-webmvc
4.3.7.RELEASE
org.springframework
spring-test
4.3.7.RELEASE
基本思路
建立spring框架
1.创建bean
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
public interface userDao {
public List findAll();
}
3.创建bean的userService的接口
package club.twzw.service;
import club.twzw.bean.User;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
public List findAll();
}
4.实现userServiceImpl,并给userServiceImpl 起别名userService
@Service("userService")<<<
public class userServiceImpl implements userService {
@Override
public List findAll() {
System.out.println("查询所有用户。。。");
return null;//service.findAll();
}
}
5.在resources文件下创建文件applicationContext.xml
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">
7.书写测试方法
@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文件中修改并配置过滤器,中文乱码过滤
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">
spring-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
spring-dispatcher
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/
在resources文件夹下创建springmvc.xml文件,开启注解扫描,视图解析器以及过滤静态资源和springmvc注解支持
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">
书写controller
package club.twzw.controller;
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 all = service.findAll();
model.addAttribute("user",all);
return "list";
}
}
index.jsp
success.jsp
scuccessscuccess
页面如果做成显示,自此成功添加spring mvc 开始整合
在整合之前需要明白,我们需要在controller中调用service,最快捷的便是使用依赖注入,而至今使用Tomcat服务器只加载了springmvc.xml文件,并没有applicationContext.xml的加载(也就是spring并没有被加载),所以可以通过监听ServeltContext域对象,在创建时加载spring的配置文件(applicationContext.xml)
配置监听器
在web.xml文件下添加listener,context-param设置监听和applicationContext.xml的文件路径
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">
<<<
org.springframework.web.context.ContextLoaderListener
<<<
contextConfigLocation
classpath:applicationContext.xml
spring-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
spring-dispatcher
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/
这样我们就可以使用依赖注入了
使用依赖注入并从新发布,如果正常,就可以在控制台看到两句话
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中使用注解查询
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 findAll();
}
创建mybatis的核心配置文件mysqlConfig.xml
/p>
PUBLIC "-//mybatis.org.//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
书写测试方法
@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 all = mapper.findAll();
for (User user : all) {
System.out.println(user);
}
}
输出结果:
说明了mybatis可用,那么可以开始整合了
整理mybatis,思路相同,同样使用依赖注入,将mysqlConfig.xml添加到容器中,并自动注入
在spring的文件中整合mybatis,配置连接池,factory,dao所在的包,此时有无将mysqlConfig.xml都不重要!
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">
使用依赖注入
在dao类上添加@Repository注解
@Repository<<<<
public interface UserDao {
@Select("select * from user")
public List findAll();
}
在serviceimpl中注入接口
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 findAll() {
System.out.println("查询所有用户。。。");
return dao.findAll();
}
}
修改controllerfindAll方法
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 all = service.findAll();
model.addAttribute("user",all);
return "list";
}
}
添加list.jsp
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/10/29
Time: 21:07
To change this template use File | Settings | File Templates.
--%>
Title${user.id}${user.name}${user.gender}${user.email}实现访问
实现插入
controller
@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
package club.twzw.service;
import club.twzw.bean.User;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
public List findAll();
public boolean Save(User u);
}
UserServiceImpl
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 findAll() {
System.out.println("查询所有用户。。。");
return dao.findAll();
}
@Override
public boolean Save(User u) {
return dao.Save(u);
}
}
dao
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 findAll();
@Insert("insert into user (name,gender,email) values(#{name},#{gender},#{email})")
public boolean Save(User u);
}
输出:
访问路径(http://localhost:8080/ssmWork_war_exploded/save)
这样我们的ssm框架就完成整合了,可以去干大事了!!
码云开源库:码云链接