【Spring】SSM框架整合Spring和SpringMVC
空谷忧人 2024-08-03 09:35:07 阅读 52
目录
1.项目结构
2.项目的pom.xml文件
3.spring.xml和springMVC配置文件
4.database.properties和mybatis.xml配置文件
5. 代码编写
6.测试整合结果
1.项目结构
首先创建一个名为ssm_pro的Mavew项目,然后再在主目录和资源目录下,创建如下所示的结构。任务是:通过ssm框架查询出所有的图书信息。
2.项目的pom.xml文件
其次导入本次项目需要的依赖在pom.xml中,其中大概有几个方面的依赖,分别是:
mybatis的相关依赖spring框架有关的依赖spring和mybatis整合的依赖springMVC的依赖数据库的依赖还有一些工具类的依赖,比如lombok,junit等
<code><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"code>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">code>
<modelVersion>4.0.0</modelVersion>
<groupId>com.its</groupId>
<artifactId>ssm_pro</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ssm_pro Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--01 mybatis相关依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<!--02 spring相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.26</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.26</version>
</dependency>
<!-- 03 spring和mybatis整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--04 springMvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.26</version>
</dependency>
<!--05 数据库相关 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<!--06 工具类 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<finalName>ssm_pro</finalName>
</build>
</project>
3.spring.xml和springMVC配置文件
mybatis和spring的整合,主要是将mybatis的配置信息整合到spring的配置信息中,下面是spring的配置信息,spring.xml文件的内容。
<?xml version="1.0" encoding="UTF-8"?>code>
<beans xmlns="http://www.springframework.org/schema/beans"code>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"code>
xmlns:context="http://www.springframework.org/schema/context"code>
xmlns:aop="http://www.springframework.org/schema/aop"code>
xmlns:tx="http://www.springframework.org/schema/tx"code>
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">code>
<!--01 开启注解扫描,用于扫描com.its下的所有类,用于IOC的管理-->
<context:component-scan base-package="com.its"/>code>
<!--02 数据源,这里是用于连接数据库,是原来mybatis配置文件的内容,在这里配置-->
<!--context标签用于引入外部的database.xml文件。-->
<context:property-placeholder location="classpath:database.properties"/>code>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">code>
<property name="driverClassName" value="${jdbc.driver}"/>code>
<property name="url" value="${jdbc.url}"/>code>
<property name="username" value="${jdbc.username}"/>code>
<property name="password" value="${jdbc.password}"/>code>
<!--连接池信息-->
<property name="maxIdle" value="100"/>code>
</bean>
<!--03 事务相关配置-->
<bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">code>
<property name="dataSource" ref="dataSource"/>code>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManger"/>code>
<!--04 Mybatis基础配置-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">code>
<!--数据源-->
<property name="dataSource" ref="dataSource"/>code>
<!--自动扫描mapper.xml文件,然后映射com/its/mapper下的所有xml文件,
不用像以前那样一个一个配置映射-->
<property name="mapperLocations" value="classpath:com/its/mapper/*xml"/>code>
<!--别名,这里是映射的实体类-->
<property name="typeAliasesPackage" value="com.its.pojo"/>code>
<!--引入mybatis.xml文件-->
<property name="configLocation" value="classpath:mybatis.xml"/>code>
</bean>
<!-- 05 Mapper代理开发,使用spring自动映射Mybatis的接口并装配-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">code>
<property name="basePackage" value="com.its.mapper"/>code>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>code>
</bean>
</beans>
由于还要使用springMVC的功能,所以需要springMVC的配置文件,如下
<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" 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">code>
<!-- 扫描 -->
<context:component-scan base-package="com.its.controller"/>code>
</beans>
4.database.properties和mybatis.xml配置文件
在spring.xml中又引入database.properties和mybatis.xml配置文件,配置文件内容分别如下
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql:///demo_test
jdbc.username = root
jdbc.password = root
<?xml version="1.0" encoding="UTF-8" ?>code>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https//mybatis.org/dtd/mybatis-3-config.dtd">
<!--核心配置文件-->
<configuration>
<!--设置-->
<settings>
<!--输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>code>
<!--驼峰映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>code>
</settings>
</configuration>
mapper层接口的映射文件BooksMapper.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>code>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.its.mapper.BooksMapper">code>
<select id="selectAllBooks" resultType="com.its.pojo.Books">code>
select * from demo_test.books
</select>
</mapper>
5. 代码编写
1.数据库表如下
2.Controller层的BooksController类的的代码如下
<code>package com.its.controller;
import com.its.pojo.Books;
import com.its.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/books")
public class BooksController {
@Autowired
BooksService booksService;
@RequestMapping("/list")
public void getAllBooks(){
List<Books> list = booksService.getAllBooks();
list.forEach(System.out::println);
}
}
3.service层的BookService接口和BookServiceImp代码如下
package com.its.service;
import com.its.pojo.Books;
import java.util.List;
public interface BooksService {
List<Books> getAllBooks();
}
package com.its.service.Impl;
import com.its.mapper.BooksMapper;
import com.its.pojo.Books;
import com.its.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BooksServiceImpl implements BooksService {
@Autowired
BooksMapper booksMapper;
@Override
public List<Books> getAllBooks() {
return booksMapper.selectAllBooks();
}
}
4..mapper层下的BooksMapper类的代码如下
package com.its.mapper;
import com.its.pojo.Books;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface BooksMapper {
List<Books> selectAllBooks();
}
5.用于查询数据户的映射文件BooksMapper.xml文件代码如下
<?xml version="1.0" encoding="UTF-8" ?>code>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.its.mapper.BooksMapper">code>
<select id="selectAllBooks" resultType="com.its.pojo.Books">code>
select * from demo_test.books
</select>
</mapper>
6.测试整合结果
(1)先编写mybatis和spring整合的测试代码,如下
package com.its;
import com.its.controller.BooksController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class Test01 {
@Autowired
private BooksController booksController;
@Test
public void test(){
booksController.getAllBooks();
}
}
结果输出了所有的图书信息,mybatis和spring整合测试成功。
(2)再测试springMVC是否整合成功,这里首先要配置一下tomacat服务器,再在空着层提供相应的访问路径,如果服务器启动成功,再地址栏输入访问地址后,控制台依然能够打印出相同的结果,则springMVC整合成功!
配置tomacat
启动tomacat,在地址栏输入http://localhost:8080/books/list,控制台显示所有图书,springMVC整合成功。
总结:mybatis整合spring和springMVC主要有以下几个步骤
设置项目结构分为,主文件下controller,service,mapper,pojo几个包。资源目录下,设置同包同名的sql映射文件,编写spring.xml,mybatis.xml,database.properties,springMvc.xml配置文件编写各个层代码。执行测试。
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。