基于IOC容器实现管理mybatis过程解析

网友投稿 202 2023-04-02

基于IOC容器实现管理mybatis过程解析

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

逻辑思路:

减少代码冗余,需要封装mybatisAPI。

可以注册SqlSessionFactoryBean,来完成SqlSessionFactory的实例化。

它的实例化需要(依赖)"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约束

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

http://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

http://springframework.org/schema/context/spring-context.xsd">

2、db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/数据库?serverTimezone=GMT%2B8

jdbc.username=root

jdbc.password=123

3、引入数据库配置文件

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

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

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

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

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-dao.xml");

StudentDao studentDao = (StudentDao) context.getBean("studentDao");

List students = studentDao.selectAll();

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

上一篇:万维易源api
下一篇:驾驶证实名api
相关文章

 发表评论

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