如何实现管理mybatis过程

发布时间:2020-07-16 13:11:58 作者:小猪
来源:亿速云 阅读:124

这篇文章主要讲解了如何实现管理mybatis过程,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

SqlSessionFactory是mybatis的基础中的基础,必须实例!

逻辑思路:

它的实例化需要(依赖)"mybatis-config.xml"文件,

其中有三大抽象:1、数据源;2、别名;3、注册mapper

可以把依赖(作为属性)注入(DI)到SqlSessionFactoryBean中,
来完成SqlSessionFactory的实例化。

pom:junit、webmvc、mysql-connector、spring-jdbc、mybatis、mybatis-spring、lombok

1、spring-dao.xml:bean约束

<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
  http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

2、db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库&#63;serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123

3、引入数据库配置文件

<context:property-placeholder location="classpath:db.properties"/>

4、从spring自带jdbc配置数据源

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

5、利用SqlSessionFactoryBean获取配置SqlSessionFactory实例

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath*:mapper/*.xml"/>
    <property name="typeAliasesPackage" value="pojo"/>
  </bean>

6、扫描dao包,同时生成sqlsessionTemplate和注入mapper接口的实现类

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="dao" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

7、加载spring-dao.xml获取上下文,从而为dao接口自动装配

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-dao.xml");
StudentDao studentDao = (StudentDao) context.getBean("studentDao");
List<Student> students = studentDao.selectAll();

看完上述内容,是不是对如何实现管理mybatis过程有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. mybatis调用oracle存储过程
  2. 整合mybatis和spring过程报错怎么办?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis 如何实现

上一篇:Go36-16,17-goroutine

下一篇:ipconfig--/release和/renew个人记录

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》