目录
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等
<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/maven-v4_0_0.xsd">
<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"?>
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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">
<!--01 开启注解扫描,用于扫描com.its下的所有类,用于IOC的管理-->
<context:component-scan base-package="com.its"/>
<!--02 数据源,这里是用于连接数据库,是原来mybatis配置文件的内容,在这里配置-->
<!--context标签用于引入外部的database.xml文件。-->
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" 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}"/>
<!--连接池信息-->
<property name="maxIdle" value="100"/>
</bean>
<!--03 事务相关配置-->
<bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManger"/>
<!--04 Mybatis基础配置-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
<!--自动扫描mapper.xml文件,然后映射com/its/mapper下的所有xml文件,
不用像以前那样一个一个配置映射-->
<property name="mapperLocations" value="classpath:com/its/mapper/*xml"/>
<!--别名,这里是映射的实体类-->
<property name="typeAliasesPackage" value="com.its.pojo"/>
<!--引入mybatis.xml文件-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!-- 05 Mapper代理开发,使用spring自动映射Mybatis的接口并装配-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.its.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</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">
<!-- 扫描 -->
<context:component-scan base-package="com.its.controller"/>
</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" ?>
<!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"/>
<!--驼峰映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
mapper层接口的映射文件BooksMapper.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.its.mapper.BooksMapper">
<select id="selectAllBooks" resultType="com.its.pojo.Books">
select * from demo_test.books
</select>
</mapper>
5. 代码编写
1.数据库表如下
2.Controller层的BooksController类的的代码如下
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" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.its.mapper.BooksMapper">
<select id="selectAllBooks" resultType="com.its.pojo.Books">
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配置文件
- 编写各个层代码。
- 执行测试。