详解Spring与MyBatis的整合的方法
作者:深情的大鹅
这篇文章主要为大家详细介绍了Spring与MyBatis的整合,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
一、问题提出
mybatis是根据mapper.xml文件动态生成mapper接口的实现类,按照Spring的常规组件扫描的方式不能添加到IOC容器中,需要做出相应的配置
二、解决方案
图片摘自尚硅谷尚筹网项目文档。
如图可知,需要在Spring和mybatis整合的配置文件中配置
- 启动Spring组件扫描
- 数据源
- SqlsessionFactoryBean
- MapperScannerConfigurer
三、MyBatis逆向工程
pom文件
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> </dependencies> <!-- 控制maven在构建过程中的配置 --> <build> <!--构建过程中用到的插件--> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.1</version> <!--插件用的依赖--> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> </dependencies> </plugin> </plugins> </build>
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--mybatis generator:generate --> <context id="wdhTables" targetRuntime="MyBatis3"> <commentGenerator> <!--是否去除自动生成的注释 true:是 ;false:否 --> <property name="suppressAllComments" value="ture" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mbook" userId="root" password="123456"> </jdbcConnection> <!-- 默认 false,把 JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把 JDBC DECIMAL 和 NUMERIC 类型解析为 java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--targetProject:生成 Entity类的路径 --> <javaModelGenerator targetProject=".\src\main\java" targetPackage="com.wdh.entity"> <!--enableSubPackages:是否让 schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!--从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--targetProject:XxxMapper.xml映射文件生成的路径 --> <sqlMapGenerator targetProject=".\src\main\java" targetPackage="com.wdh.mapper"> <!--enableSubPackages:是否让 schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!--targetPackage Mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetProject=".\src\main\java" targetPackage="com.wdh.mapper"> <!--enableSubPackages:是否让 schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 数据库表名字和我们的 entity类对应的映射指定 --> <table tableName="login" domainObjectName="Login" /> </context> </generatorConfiguration>
生成结果:
把生成的文件移动到对应的工程文件中
四、Spring与mybatis的配置文件
主模块依赖jar包
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.3.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.16</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> </dependencies>
spring-persist-mybatis.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.wdh"/> <!--引用jdbc.properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置druid数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="url" value="${jdbc.url}"/> <!--注意这里是dirverClassName--> <property name="driverClassName" value="${jdbc.driver}"/> </bean> <!--配置SqlSessionFactoryBean--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--配置数据源--> <property name="dataSource" ref="dataSource"/> <!--配置mybatis-config文件位置--> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <!--配置mapper映射文件位置--> <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/> </bean> <!--配置MapperScannerConfigurer--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--配置要扫描的包--> <property name="basePackage" value="com.wdh.mapper"/> </bean></beans><?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.wdh"/> <!--引用jdbc.properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置druid数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="url" value="${jdbc.url}"/> <!--注意这里是dirverClassName--> <property name="driverClassName" value="${jdbc.driver}"/> </bean> <!--配置SqlSessionFactoryBean--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--配置数据源--> <property name="dataSource" ref="dataSource"/> <!--配置mybatis-config文件位置--> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <!--配置mapper映射文件位置--> <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/> </bean> <!--配置MapperScannerConfigurer--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--配置要扫描的包--> <property name="basePackage" value="com.wdh.mapper"/> </bean> </beans>
文件目录
五、测试
//junit与Spring整合 @RunWith(SpringJUnit4ClassRunner.class) //加载Spring配置文件 @ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml"}) public class DataSourceTest { @Autowired private DataSource dataSource; @Autowired private LoginMapper loginMapper; @Test public void connTest() throws SQLException { System.out.println(dataSource.getConnection()); } @Test public void insertTest(){ Login login = new Login(2, "wdh", "123", false); int i = loginMapper.insert(login); System.out.println("受影响的行数:"+ i); } }
结果:
六、总结
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!