SSM整合(无Maven)
1、开发环境
IDE: Eclipse Mars2
Jdk: 1.8
数据库: MySQL
2、创建数据库
数据库sql文件位置如下图:
创建crm数据库,执行sql语句
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (`id` int(11) DEFAULT NULL,`username` varchar(60) DEFAULT NULL,`password` varchar(60) DEFAULT NULL,`name` varchar(60) DEFAULT NULL,`birthday` date DEFAULT NULL,`gender` int(11) DEFAULT NULL,`created` datetime DEFAULT NULL,`updated` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `user` VALUES ('1', 'zhangsan', '123456', '张三', '2019-06-10', '1', '2019-06-10 10:03:52', '2019-06-10 10:03:55');
INSERT INTO `user` VALUES ('2', 'lisi', '123456', '李四', '2019-06-03', '0', '2019-06-09 10:04:17', '2019-06-12 10:04:21');
3、工程搭建
工程使用Springmvc、spring、mybatis框架整合完成。
3.1.需要的jar包
1.spring(包括springmvc)
2.mybatis
3.mybatis-spring整合包
4.数据库驱动
5.第三方连接池。
6.Json依赖包Jackson
jar包位置如下图:
3.2.整合思路
Dao层:
1、SqlMapConfig.xml,空文件即可,但是需要文件头。
2、applicationContext-dao.xml
1、数据库连接Druid
2、SqlSessionFactory对象,需要spring和mybatis整合包下的。
3、配置mapper文件扫描器。Mapper动态代理开发 增强版
Service层:
1、applicationContext-service.xml包扫描器,扫描@service注解的类。
2、applicationContext-trans.xml配置事务。
Controller层:
1、springMvc.xml
1、包扫描器,扫描@Controller注解的类。
2、配置注解驱动
3、配置视图解析器
Web.xml文件:
1、配置spring监听器
2、配置前端控制器。
3.3.创建工程
创建动态web工程,步骤如下图:
创建ssm,如下图:
3.4.加入jar包
加入前面照片中的jar包
3.5.加入配置文件
创建config资源文件夹,在里面创建mybatis和spring文件夹
3.5.1.SqlMapConfig.xml
空文件即可
<?xml version="1.0" encoding="UTF-8"?><!--约束文档-->
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--别名--><typeAliases><package name="com.pojo"/></typeAliases>
</configuration>
3.5.2.applicationContext-dao.xml
需要配置:
加载properties文件,数据源,SqlSessionFactory,Mapper扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "><!-- 配置 读取properties文件 jdbc.properties --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置 数据源 --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${db.driver}"></property><property name="url" value="${db.url}"></property><property name="username" value="${db.username}"></property><property name="password" value="${db.password}"></property></bean><!-- 配置SqlSessionFactory --><bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 设置MyBatis核心配置文件 --><property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /><!-- 设置数据源 --><property name="dataSource" ref="dataSource" /></bean><!-- 配置Mapper扫描 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 设置Mapper扫描包 --><property name="basePackage" value="com.mapper" /><property name="sqlSessionFactoryBeanName" value="ssf"></property></bean>
</beans>
3.5.3.jdbc.properties
配置数据库信息:
jdbc.properties:数据库的连接
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql:///crm?characterEncoding=utf8
db.username=root
db.password=root
3.5.4.log4j.properties
配置日志信息
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
3.5.5.applicationContext-service.xml
配置service扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd "><!----><context:component-scan base-package="com.service"/></beans>
3.5.6.applicationContext-trans.xml
声明式事务 ;
配置事务管理:事务管理器、通知、切面
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd "><bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 4.2 配置事务策略 --><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="query*" read-only="true" /><tx:method name="find*" read-only="true" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /></tx:attributes></tx:advice><!-- 4.3 织入事务 --><aop:config><aop:pointcut expression="execution(* com.service..*(..))" id="myPointcut" /><aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" /></aop:config>
</beans>
3.5.7.Springmvc.xml
配置SpringMVC表现层:Controller扫描、注解驱动、视图解析器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "><!-- 配置Controller扫描 --><context:component-scan base-package="com.controller"/><!-- 配置注解驱动 --><mvc:annotation-driven /><!-- 配置视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--前缀跟后缀--><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean>
</beans>
3.5.8.Web.xml
配置Spring、SpringMVC、解决post乱码问题
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>myssm</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
<!-- 配置过滤器,解决post的乱码问题 --><filter><filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext-*.xml</param-value></context-param><listener><!-- 项目加载时,加载applicationContext.xml配置文件,获取context对象并且放置在application域管理 默认加载配置文件路径: /WEB-INF/applicationContext.xml--><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--1.前端控制器 --><servlet><servlet-name>mvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springMVC.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>mvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>
</web-app>
3.6.加入静态资源
最终效果如下图:
//index.jsp页面
<body><a href="test.do">显示数据</a>
</body>
//show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><table border="1" cellspacing="0" cellpadding="5" width="500px"><tr><td>编号</td><td>用户名</td><td>名称</td><td>性别</td></tr><c:forEach items="${list }" var="user"><tr><td>${user.id }</td><td>${user.username }</td><td>${user.name }</td><td><c:if test="${user.gender == 1}">男</c:if><c:if test="${user.gender == 0}">女</c:if></td></tr></c:forEach></table>
</body>
</html>
4.实现页面展示
4.1.代码实现
编写UserController 显示用户列表
@Controller
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/test.do")public String getUsers(Model model) {List<User> list = userService.selUsers();model.addAttribute("list", list);return "show";}
}
4.2.实现DAO开发
4.2.1.pojo
因为页面显示的名字是下划线方式,和数据库表列名一样,根据页面的样式,编写pojo
public class User {private Integer id;private String username;private String password;private String name;private Date birthday;private Integer gender;//get set方法getter/setter…..
}
4.2.2.Mapper
编写UserMapper接口
//编写UserMapper
public interface UserMapper {//查询用户列表 List<User> selUsers();
}
```## 标题### 4.2.3.Mapper.xml
编写UserMapper.xml进行实现```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aura.mapper.UserMapper"><select id="selUsers" resultType="User">select id,user_name username,`name`,`password`,gender from `user`</select>
</mapper>
4.3.实现Service开发
4.3.1.UserService 接口
public interface UserService {//查询所有用户List<User> selUsers();
}
4.3.2.UserServiceImpl 实现类
@Service
public class UserServiceImpl implements UserService {@Autowired UserMapper userMapper;@Overridepublic List<User> selUsers() {return userMapper.selUsers();}
}
最后附上两张截图
SSM整合(无Maven)相关推荐
- SSM整合时Maven项目的pom.xml版本兼容的代码备份
场景 jdk:1.8 Tomcat:7.0 本地mysql:8.0 Eclipse:Eclipse Jee Photon Spring:4.3.8 AOP:1.8.0 mybatis:3.3.0 My ...
- 第一个SSM整合的Maven入门级项目(超详细步骤)
环境 jdk:1.8 Tomcat:7.0 本地mysql:8.0 Eclipse:Eclipse Jee Photon Spring:4.3.8 AOP:1.8.0 mybatis:3.3.0 My ...
- SSM整合的Maven项目中引入Bootstrap,三步实现高大上插拔UI,后端也有美感
对比效果 引入bootstrap前 引入bootstrap后 实现 第一步下载并引入bootstrap bootstrap3下载 https://v3.bootcss.com/getting-star ...
- 基于Maven+SpringMVC+Spring+MyBatis+Layui整合框架,超详细的SSM整合❤️
人生有太多不如意,我们要学会去努力 参考文档:layUI文档:spring家族文档:mybatis文档 前言:SSM 整合 整合的思路是: 先创建spring框架 通过spring整合spring m ...
- Maven+SSM整合
Maven+SSM整合 1.工程体系图 2.创建mavenWeb项目,添加pom.xml文件依赖以及插件 <properties><project.build.sourceEncod ...
- 【SSM整合】SSM详细整合-maven分模块架构
本文从一个简单的三层案例入手,分模块整合SSM框架,从环境搭建到测试,每一步都比较详细,希望能对你有所帮助. 文章难免存在错误,望大佬及时指教. 文章所有代码在文章末尾给出. 0x01.SSM整合说明 ...
- 基于ssm整合的网上书城
基于ssm整合的网上书城 采用当前最流行的框架Spring-SpringMVC-MyBatis设计,分为前后台,前台用户可以购买书籍,后台管理员可以对书籍进行分类,增删改查 注意:本系统不支持jdk1 ...
- SSM框架学习文档以及SSM整合(附Github地址=含SSM学习时的实例代码)
SSM框架学习 软件架构: 基于流行SSM框架:Spring+SpringMVC+Mybatis 项目配置: 使用Maven进行项目jar导入 使用Git进行版本控制,并将每次编写的代码上传到Gi ...
- Java(SpringMVC03)(SSM整合1)
Java(SpringMVC03)(SSM整合) 参考视频:17. ssm整合:Mybatis层(狂神) 10. SSM整合 10.1 环境要求 环境: IDEA(我的是2021.2) MySQL 5 ...
- ssm整合笔记(1)-curd
1.建立相关的github地址 https://github.com/MRtianyanxiaobai/ssm_curd 建立好相关网址后,配置IDEA 的git环境 ssm整合笔记(1)-curd ...
最新文章
- tomcat线程释放时间_聊下并发和Tomcat线程数(错误更正)
- Python中使用元组对ndarray矩阵的某个维度进行选取和调序的操作
- 个人c++ 错误记录
- element from表单个别select 出现 一开始就校验了数据,且有数据还通不过校验,选中的项叉不掉问题。
- SAP UI5 应用开发教程之五十二 - 如何使用 SAP UI5 的标准控件结合 Cordova 插件调用手机摄像头进行条形码扫描
- td外边加div为啥不隐藏_过年炸油饼注意了,秘制配方比例教给你,柔软不吸油,放凉了不硬...
- python字符串筛选输出_「每日一练」巧用Python对字符串进行筛选
- Windows Phone开发手记-WinRT下启动器替代方案
- 修改eclipse3.7默认字体
- GET /favicon.ico HTTP/1.1 404
- 基于Renascence架构的SQL查询引擎设计
- 数学建模(6)-Matlab绘制图像精细修改
- 达威尔液晶电子手写板儿童涂鸦画板写字板,培养小孩创造力和想象力的好工具!
- 有趣的12张数学原理动图,令人舒心却又伤脑!你看懂几个?
- xprivacy改IMEI
- 用png格式图片和非png格式图片做水印图片
- PIR热释电传感器使用笔记
- 在Word中巧改厘米标尺(转)
- 温度传感器温度数据LED屏幕展示--物联网服务器搭建
- 易语言 超简单的可视化窗口编程语言
热门文章
- 10.修改sysctl.conf提示没权限
- PSD文件不小心删除怎么恢复?PS崩溃看这里
- 抗疫三年,“医”路有你
- 【ES笔记01】ElasticSearch数据库之index索引、doc文档、alias别名、mappings映射结构的基本操作
- 巨杉数据库助力民生银行、恒丰银行云化架构升级
- 哪种串口卡支持linux4.9,乐扩工业型PCIe串口卡
- 第一季度贡献奖励分配结果与评级 | IOST合伙人计划
- vue根据条件给标签添加属性
- win10如何android skd,雨林木风win10系统android sdk manager 无法更新的步骤介绍
- 交换机转发速率、吞吐量、背板带宽计算(详解)