您好,登录后才能下订单哦!
这篇文章主要讲解了“Spring整合MyBatis的实现方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring整合MyBatis的实现方法是什么”吧!
Spring 整合 MyBatis 开发环境除了需要 Spring 的jar包和 MyBatis 的jar包,还需要 Spring 与 MyBatis 整合的中间件 mybatis-spring-xxx.jar,此外还需要数据库驱动jar包 mysql-connector-java-xxx.jar。
项目案例:Spring + MyBatis 实现对 user 数据库的查询。
实现步骤:
【1】创建一个 Maven 项目,然后再 pom.xml 文件中添加相关依赖
<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>cn.kgc.sm</groupId> <artifactId>spring06</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>spring06</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!--测试工具--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.9</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <!--IOC 依赖注入--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <!--mybatis&spring 整合包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <!--aspects 切面--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.9</version> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.9</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> <!-- 日志工具--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--mybatis分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.9</version> <scope>test</scope> </dependency> </dependencies> </project>
【2】在 resources —> config 目录下,创建数据库配置文件 jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///java2218?serverTimezone=UTC&useSSL=false&characterEncoding=UTF-8 jdbc.username=root jdbc.password=root
【3】在 resources —> config 目录下,创建 MyBatis 配置文件 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> <settings> <setting name="logImpl" value="LOG4J"/> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--全局开启二级缓存--> <setting name="cacheEnabled" value="true"/> </settings> <!--配置mybatis分页插件--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 配置mysql方言 --> <property name="helperDialect" value="mysql" /> <!-- 设置为true时,如果pageSize=0就会查询出全部的结果 --> <property name="pageSizeZero" value="true" /> <!-- 3.3.0版本可用,分页参数合理化,默认false禁用 --> <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 --> <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 --> <property name="reasonable" value="true" /> </plugin> </plugins> </configuration>
【4】在 resources —> config 目录下,创建 Spring 配置文件 spring-config.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:property-placeholder location="classpath:config/jdbc.properties"></context:property-placeholder> <!--初始化数据库连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--设置数据库连接池--> <property name="dataSource" ref="dataSource"></property> <!--设置mybatis全局配置文件位置--> <property name="configLocation" value="config/mybatis-config.xml"></property> <!--设置别名的包--> <property name="typeAliasesPackage" value="cn.kgc.sm"></property> <!--设置mapper.xml文件的位置--> <property name="mapperLocations" value="mapper/*.xml"></property> </bean> <!--mapper接口扫描 生成接口代理对象 同时完成对象的托管--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.kgc.sm.mapper"></property> </bean> <!--开启包扫描 base-package 设置需要扫描的包 --> <context:component-scan base-package="cn.kgc.sm"></context:component-scan> </beans>
【5】创建实体类 User
package cn.kgc.sm.entity; import lombok.Data; @Data public class User { private Integer uId; private String uName; private String uPassword; private Integer uState; }
【6】创建 Mapper 接口 UserMapper
package cn.kgc.sm.mapper; import cn.kgc.sm.entity.User; import java.util.List; public interface UserMapper { List<User> selectAll(); }
【7】创建与 UserMapper 接口映射的 UserMapper.xml 文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.kgc.sm.mapper.UserMapper"> <select id="selectAll" resultType="user"> select * from user </select> </mapper>
【8】创建 Service 业务层接口 UserService
package cn.kgc.sm.service; import cn.kgc.sm.entity.User; import cn.kgc.sm.mapper.UserMapper; import java.util.List; public interface UserService { List<User> selectAll(); }
【9】创建 UserService 接口的实现类 UserServiceImpl
package cn.kgc.sm.service.impl; import cn.kgc.sm.entity.User; import cn.kgc.sm.mapper.UserMapper; import cn.kgc.sm.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public List<User> selectAll() { System.out.println("users = "); List<User> users = userMapper.selectAll(); System.out.println("users = " + users); return users; } }
【10】创建测试类
package cn.kgc.sm.mapper; import cn.kgc.sm.entity.User; import cn.kgc.sm.service.UserService; import junit.framework.TestCase; 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; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:config/spring-config.xml") public class UserMapperTest { @Autowired UserService userService; @Test public void Test01(){ List<User> users = userService.selectAll(); users.forEach(user -> System.out.println("user = " + user)); // for (User user : users) { // System.out.println("user = " + user); // } } }
运行结果:
感谢各位的阅读,以上就是“Spring整合MyBatis的实现方法是什么”的内容了,经过本文的学习后,相信大家对Spring整合MyBatis的实现方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。