对于ssm框架网上有很多,这里只是自己为大家提供的一个ssm整合框架参考分享,这个前提是基于maven的管理工具写的,
一 创建Maven项目具体步骤如下:
二 配置文件
2.1 配置mybatis-config.xml
2.2 配置mapper.xml
2.3 配置jdbc.properties
2.4 配置applicationContext.xml
2.5 配置springmvc-servlet.xml
2.6 配置web.xml
2.7 配置log4j.properties
2.8 引入依赖
三 测试
3.1 创建java文件
3.1.1 UserController类
3.1.2 User实体类
3.1.3 UserService接口
3.1.4 UserServiceImpl实现类
3.1.5 UserMapper类
3.2 创建MySql数据库表
3.3 创建users.jsp文件
3.4 结果
==============================================================================
1.设计数据库表如下:
/*Date: 2019-03-11 23:19:10*/
SET FOREIGN_KEY_CHECKS=0;------------------------------
--Table structure for tb_user
------------------------------
DROP TABLE IF EXISTS`tb_user`;CREATE TABLE`tb_user` (
`id`bigint(20) NOT NULLAUTO_INCREMENT,
`user_name` varchar(100) DEFAULT NULL COMMENT '用户名',
`password`varchar(100) DEFAULT NULL COMMENT '密码',
`name`varchar(100) DEFAULT NULL COMMENT '姓名',
`age`int(10) DEFAULT NULL COMMENT '年龄',
`sex`tinyint(1) DEFAULT NULL COMMENT '性别,1男性,2女性',
`birthday` dateDEFAULT NULL COMMENT '出生日期',
`created`datetime DEFAULT NULL COMMENT '创建时间',
`updated`datetime DEFAULT NULL COMMENT '更新时间',PRIMARY KEY(`id`),UNIQUE KEY `username` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;INSERT INTO `tb_user` VALUES (1, 'zhangsan', '123456', '张三', 30, 1, '1984-8-8', '2014-9-19 16:56:04', '2014-9-21 11:24:59');INSERT INTO `tb_user` VALUES (2, 'lisi', '123456', '李四', 21, 2, '1991-1-1', '2014-9-19 16:56:04', '2014-9-19 16:56:04');INSERT INTO `tb_user` VALUES (3, 'wangwu', '123456', '王五', 22, 2, '1989-1-1', '2014-9-19 16:56:04', '2014-9-19 16:56:04');INSERT INTO `tb_user` VALUES (4, 'zhangwei', '123456', '张伟', 20, 1, '1988-9-1', '2014-9-19 16:56:04', '2014-9-19 16:56:04');INSERT INTO `tb_user` VALUES (5, 'lina', '123456', '李娜', 28, 1, '1985-1-1', '2014-9-19 16:56:04', '2014-9-19 16:56:04');INSERT INTO `tb_user` VALUES (6, 'lilei', '123456', '李磊', 23, 1, '1988-8-8', '2014-9-20 11:41:15', '2014-9-20 11:41:15');INSERT INTO `tb_user` VALUES (8, 'xiaofeng', '123456', '萧峰', 30, 1, '2018-6-25', '2018-6-25 18:42:25', '2018-6-25 18:42:25');INSERT INTO `tb_user` VALUES (12, 'zhendeshuai', '565656', '吴彦祖', 26, 1, '2018-6-26', '2018-6-26 14:55:59', '2018-6-26 15:12:12');INSERT INTO `tb_user` VALUES (14, 'jiumozhi', '123456', '鸠摩智', 30, 1, '2018-6-26', '2018-6-26 23:53:15', '2018-6-26 23:53:15');INSERT INTO `tb_user` VALUES (21, 'zhangsan1234', '12345', '杰克', 20, 1, '2000-1-1', '2018-7-7 21:50:26', '2018-7-7 21:50:26');INSERT INTO `tb_user` VALUES (22, 'zhangsanqwe1', '123455', '张三', 30, 1, '1999-2-25', '2018-7-7 21:51:20', '2018-7-7 21:51:20');INSERT INTO `tb_user` VALUES (25, 'qianjiu', '123456', '钱九', 30, 1, '1989-1-1', '2018-7-7 21:51:20', '2018-7-7 21:51:20');INSERT INTO `tb_user` VALUES (26, 'zhaoniu', '123456', '赵牛', 28, 1, '1989-1-1', '2018-7-7 21:51:20', '2018-7-7 21:51:20');
2 创建一个ssmDemo项目(项目名称可以自定义),如图所示:
创建好之后如下图所示:
项目会报错,不用急,为什么呢?因为这是一个不完整的maven项目,缺少web.xml。
右键点击项目,如下操作,会自动补全web.xml文件
3,向ssmzh项目中的pom.xml导入spring-springmvc-mybatis整合所需要的依赖,具体依赖如下:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.agu
SSMDemo
1.0-SNAPSHOT
war
org.springframework
spring-context
4.3.13.RELEASE
org.springframework
spring-webmvc
4.3.13.RELEASE
org.springframework
spring-jdbc
4.3.13.RELEASE
org.springframework
spring-aspects
4.3.13.RELEASE
org.mybatis
mybatis
3.2.8
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
5.1.32
org.slf4j
slf4j-log4j12
1.7.22
com.fasterxml.jackson.core
jackson-databind
2.9.0
com.alibaba
druid
1.0.9
jstl
jstl
1.2
javax.servlet
servlet-api
2.5
javax.servlet
jsp-api
2.0
org.apache.tomcat.maven
tomcat7-maven-plugin
8080
/
4,配置spring.xml(企业常用applicationContext.xml命名表示是spring的配置,我这里命名为spring.xml)
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
classpath:jdbc.properties
5,spring-tx(spring事务的配置)如下:
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
6,spring-mybatis.xml整合配置如下:
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
7,springmvc.xml配置如下:
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
8,mybatis的配置如下:
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
9,Mybatis-generator的配置(这个需要请先安装mybatis-generator插件,然后即可),generatorConfig.xml配置如下:
10.jdbc.properties配置如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
user=root
passwd=123456
11.log4j.properties日志配置如下:
log4j.rootLogger=DEBUG,A1
log4j.logger.org.mybatis=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
12,配置web.xml,具体如下:<?xml version="1.0" encoding="UTF-8"?>
ssmzh
contextConfigLocation
classpath:spring/spring*.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF8
encodingFilter
/*
usermanage
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
1
usermanage
/
index.jsp
结构图所下:
运行结果如下图:
那么到此:一个完整的spring-springmvc-mybatis的整合框架就搭建好了,也许你觉得很复杂,那么这只是一个框架的开始,这个是必须要会的,不论怎么样,一定要多亲自搭建,一次,两次甚至更多,如果连这个基本框架都觉得难,那么你是否也在想应该放弃了。不管哪个行业都有它的难处,要想获得更高必须比别人付出更多。