一:概述
SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛。
Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP。
SpringMVC是Spring实现的一个Web层,相当于Struts的框架,但是比Struts更加灵活和强大!
Mybatis是 一个持久层的框架,在使用上相比Hibernate更加灵活,可以控制sql的编写,使用 XML或注解进行相关的配置!
根据上面的描述,学习SSM框架就非常的重要了!
二:搭建一个SSM的过程
使用Maven管理项目
使用Maven在Eclipse中创建一个webapp的项目 ,具体的创建过程不做演示,如有不会创建的创建项目
也可以使用Maven命令进行创建,在Dos窗口进入指定的目录,执行下面命令:
mvn archetype:create -DgroupId=org.ssm.dufy -DartifactId=ssm-demo -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
使用命令要注意,系统安装了Maven,并配置好了环境变量!Maven的安装和环境变量配置
1、导入项目(命名创建),添加依赖
如下项目结构:
然后直接配置 pom.xml文件中的包依赖!
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>org.dufy</groupId> 5 <artifactId>ssm</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>ssmDemo</name> 9 <url>http://maven.apache.org</url> 10 <properties> 11 <spring.version>4.0.5.RELEASE</spring.version> 12 <mybatis.version>3.2.1</mybatis.version> 13 <slf4j.version>1.6.6</slf4j.version> 14 <log4j.version>1.2.12</log4j.version> 15 <mysql.version>5.1.35</mysql.version> 16 17 18 </properties> 19 <dependencies> 20 <!-- 添加Spring依赖 --> 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-core</artifactId> 24 <version>${spring.version}</version> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-webmvc</artifactId> 29 <version>${spring.version}</version> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-context</artifactId> 34 <version>${spring.version}</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-context-support</artifactId> 39 <version>${spring.version}</version> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-aop</artifactId> 44 <version>${spring.version}</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-aspects</artifactId> 49 <version>${spring.version}</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-tx</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-jdbc</artifactId> 59 <version>${spring.version}</version> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-web</artifactId> 64 <version>${spring.version}</version> 65 </dependency> 66 <!--spring单元测试依赖 --> 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>spring-test</artifactId> 70 <version>${spring.version}</version> 71 <scope>test</scope> 72 </dependency> 73 74 <!-- spring webmvc相关jar --> 75 <dependency> 76 <groupId>org.springframework</groupId> 77 <artifactId>spring-webmvc</artifactId> 78 <version>${spring.version}</version> 79 </dependency> 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-web</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 86 <!-- mysql驱动包 --> 87 <dependency> 88 <groupId>mysql</groupId> 89 <artifactId>mysql-connector-java</artifactId> 90 <version>${mysql.version}</version> 91 </dependency> 92 93 <!-- alibaba data source 相关jar包--> 94 <dependency> 95 <groupId>com.alibaba</groupId> 96 <artifactId>druid</artifactId> 97 <version>0.2.23</version> 98 </dependency> 99 100 <!-- alibaba fastjson 格式化对 --> 101 <dependency> 102 <groupId>com.alibaba</groupId> 103 <artifactId>fastjson</artifactId> 104 <version>1.1.41</version> 105 </dependency> 106 107 <!-- logback start --> 108 <dependency> 109 <groupId>log4j</groupId> 110 <artifactId>log4j</artifactId> 111 <version>${log4j.version}</version> 112 </dependency> 113 <dependency> 114 <groupId>org.slf4j</groupId> 115 <artifactId>slf4j-api</artifactId> 116 <version>${slf4j.version}</version> 117 </dependency> 118 <dependency> 119 <groupId>ch.qos.logback</groupId> 120 <artifactId>logback-classic</artifactId> 121 <version>1.1.2</version> 122 </dependency> 123 <dependency> 124 <groupId>ch.qos.logback</groupId> 125 <artifactId>logback-core</artifactId> 126 <version>1.1.2</version> 127 </dependency> 128 <dependency> 129 <groupId>org.logback-extensions</groupId> 130 <artifactId>logback-ext-spring</artifactId> 131 <version>0.1.1</version> 132 </dependency> 133 134 <!--mybatis依赖 --> 135 <dependency> 136 <groupId>org.mybatis</groupId> 137 <artifactId>mybatis</artifactId> 138 <version>${mybatis.version}</version> 139 </dependency> 140 141 <!-- mybatis/spring包 --> 142 <dependency> 143 <groupId>org.mybatis</groupId> 144 <artifactId>mybatis-spring</artifactId> 145 <version>1.2.0</version> 146 </dependency> 147 <!-- 添加servlet3.0核心包 --> 148 <dependency> 149 <groupId>javax.servlet</groupId> 150 <artifactId>javax.servlet-api</artifactId> 151 <version>3.0.1</version> 152 </dependency> 153 <dependency> 154 <groupId>javax.servlet.jsp</groupId> 155 <artifactId>javax.servlet.jsp-api</artifactId> 156 <version>2.3.2-b01</version> 157 </dependency> 158 <!-- jstl --> 159 <dependency> 160 <groupId>javax.servlet</groupId> 161 <artifactId>jstl</artifactId> 162 <version>1.2</version> 163 </dependency> 164 <!--单元测试依赖 --> 165 <dependency> 166 <groupId>junit</groupId> 167 <artifactId>junit</artifactId> 168 <version>3.8.1</version> 169 <scope>test</scope> 170 </dependency> 171 </dependencies> 172 <build> 173 <finalName>ssmDemo</finalName> 174 </build> 175 </project>
2、创建数据库和表,生成代码
创建数据库我参考别人的博客数据库设计,这块没有自己去书写,直接添上代码
1 DROP TABLE IF EXISTS `user_t`; 2 3 CREATE TABLE `user_t` ( 4 `id` int(11) NOT NULL AUTO_INCREMENT, 5 `user_name` varchar(40) NOT NULL, 6 `password` varchar(255) NOT NULL, 7 `age` int(4) NOT NULL, 8 PRIMARY KEY (`id`) 9 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 10 11 /*Data for the table `user_t` */ 12 13 insert into `user_t`(`id`,`user_name`,`password`,`age`) values (1,‘测试‘,‘123456‘,24)
生成代码请查看:
生成的代码导入图片解释:
3、Spring 和 mybatis整合,连接数据库,进行Junit测试
将生成的代码拷贝到项目中,然后进行Spring和Mybatis的整合,添加配置文件!
原文地址:https://www.cnblogs.com/haw2106/p/10364506.html
时间: 2024-10-06 20:22:46