SSM框架整合(IDEA从零搭建)
依赖项的配置
当我们用maven创建一个JavaWeb项目,需要搭建整合一个SSM框架的时候,可以说是难为了很多的初学者,今天我将从零出发,完完整整的搭建一个SSM项目
首先是我们的pom.xml文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chang</groupId>
<artifactId>survey</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>survey Maven Webapp</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.2.6.RELEASE</spring.version>
<mybatis.version>3.5.4</mybatis.version>
<jackson.version>2.9.8</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
<build>
<finalName>survey</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.json</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
文件结构
当我们用maven创建一个JavaWeb项目的时候,在main目录下只有一个webapp,这时候我们需要把项目目录配置完整,这时候点击左侧的项目一栏,再点击main目录,确保新建的文件夹再main目录下,和webapp平级。这时候按住alt+insert,这时候出现一个功能栏,点击Directory,依次创建java目录和resource目录。
这时候我建议最好通过Tomcat跑一下项目,看一下能否跑通,当项目成功运行的时候,我们再进行接下来的整合,确保不会前功尽弃。
这时候我们再在Java的目录下创建项目所需的包
之后是resource目录下的包
核心搭建
我们需要在spring中配置mybatis,所以需要db.properties配置文件,内容如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/survey?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=1234
读者朋友们只需要把数据库名和密码修改一下即可。
然后我们写applicationContext中的内容,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--加载数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--扫描实现类中的注解-->
<context:component-scan base-package="com.chang.service"/>
<!--配置数据库信息-->
<bean id="dateSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--记载SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dateSource"/>
<!--加载mybatis-config主配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations">
<list>
<value>classpath:com/chang/mapper/*.xml</value>
</list>
</property>
</bean>
<!--配置扫描mapper文件-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.chang.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
然后是mybatis-config.xml的编写,如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
然后是spring-mvc.xml的编写:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.chang.controller"/>
</beans>
至此为止呢,我们已经完成了SSM框架的整合,接下来将演示一个例子来验证搭建的框架是否正常运行。
案例演示
数据库以及表
新建一个数据库survey,创建一个tb_admin的表,表信息如下(记得把id设置成自动增长):
然后再pojo包中新建实体类,创建setter and getter,此处省略。
用户访问一个网站内部的执行顺序是:用户访问展示层,展示层调用controller层,controller层调用service层,service层调用dao层,然后再返回。我们这里需要进行逆向开发,首先是dao层,即mapper的开发。
dao层开发
新建一个AdminMapper接口:
package com.chang.mapper;
import com.chang.pojo.Admin;
public interface AdminMapper {
public void add(Admin admin);
}
创建接口对应的mapper映射文件
<?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.chang.mapper.AdminMapper">
<select id="add" resultType="com.chang.pojo.Admin">
insert into tb_admin(account,password,name,phone,remark)
values(#{account},#{password},#{name},#{phone},#{remark})
</select>
</mapper>
service层开发
在service包下创建AdminService接口
package com.chang.service;
import com.chang.pojo.Admin;
public interface AdminService {
public void add(Admin admin);
}
创建一个impl的包,包下创建AdminServiceImpl实现类
package com.chang.service.impl;
import com.chang.mapper.AdminMapper;
import com.chang.pojo.Admin;
import com.chang.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AdminServiceImpl implements AdminService {
@Autowired
private AdminMapper adminMapper;
@Override
public void add(Admin admin) {
adminMapper.add(admin);
}
}
controller层开发
创建一个AdminController类
package com.chang.controller;
import com.chang.pojo.Admin;
import com.chang.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/admin")
public class AdminController {
@Autowired
private AdminService adminService;
@GetMapping("/add")
public void add(){
Admin admin = new Admin();
admin.setAccount("admin2");
admin.setPassword("1233");
admin.setName("管理员2");
admin.setPhone("12233443433");
admin.setRemark("超级系统管理员");
adminService.add(admin);
}
}
因为我们没有创建视图页面,所以我们开启Tomcat服务,访问网址:http://localhost:8080/survey/admin/add的时候,会出现404异常,但是我们插入数据库字段是成功的,我们这时候去查看数据库,发现数据库是插入成功的,至此我们的SSM框架整合已经完成(因为我在将项目交给Tomcat管理的时候,我项目路径设置的survey,小伙伴们可根据自己的设置进行访问)。
本次SSM框架整合确有不足之处,如果通过本次案例成功搭建成功且成功插入数据证明搭建没有什么太大的问题,可能有的地方跟要进行的项目开发还有出入。例如servlet,jsp等等的依赖项,但是也只是稍加完善即可。本次整合可能还有不足之处,欢迎朋友们留言批评指正。