详解Spring与MyBatis的整合的方法

网友投稿 244 2022-08-19


详解Spring与MyBatis的整合的方法

目录一、问题提出二、解决方案三、MyBatis逆向工程pom文件generatorConfig.xml四、Spring与mybatis的配置文件主模块依赖jar包spring-persist-mybatis.xml 文件目录五、测试六、总结总结

一、问题提出

mybatis是根据mapper.xml文件动态生成mapper接口的实现类,按照Spring的常规组件扫描的方式不能添加到IOC容器中,需要做出相应的配置

二、解决方案

图片摘自尚硅谷尚筹网项目文档。

如图可知,需要在Spring和mybatis整合的配置文件中配置

启动Spring组件扫描数据源SqlsessionFactoryBeanMapperScannerConfigurer

三、MyBatis逆向工程

pom文件

org.mybatis

mybatis

3.5.9

org.mybatis.generator

mybatis-generator-maven-plugin

1.4.1

org.mybatis.generator

mybatis-generator-core

1.4.1

com.mchange

c3p0

0.9.5.5

mysql

mysql-connector-java

8.0.25

generatorConfig.xml

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

driverClass="com.mysql.cj.jdbc.Driver"

connectionURL="jdbc:mysql://localhost:3306/mbook"

userId="root"

password="123456">

driverClass="com.mysql.cj.jdbc.Driver"

connectionURL="jdbc:mysql://localhost:3306/mbook"

userId="root"

password="123456">

targetPackage="com.wdh.entity">

targetPackage="com.wdh.entity">

targetPackage="com.wdh.mapper">

targetPackage="com.wdh.mapper">

targetProject=".\src\main\java"

targetPackage="com.wdh.mapper">

targetProject=".\src\main\java"

targetPackage="com.wdh.mapper">

生成结果:

把生成的文件移动到对应的工程文件中

四、Spring与mybatis的配置文件

主模块依赖jar包

org.springframework

spring-orm

5.3.16

org.springframework

spring-webmvc

5.3.16

mysql

mysql-connector-java

8.0.25

org.mybatis

mybatis

3.5.9

com.alibaba

druid

1.2.8

org.mybatis

mybatis-spring

2.0.7

spring-persist-mybatis.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

文件目录

五、测试

//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);

}

}

结果:

六、总结

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:剑指Offer之Java算法习题精讲数组与字符和等差数列
下一篇:剑指Offer之Java算法习题精讲求和篇
相关文章

 发表评论

暂时没有评论,来抢沙发吧~