首页 > 其他分享 >12.整合Mybatis

12.整合Mybatis

时间:2022-10-31 19:23:22浏览次数:55  
标签:12 sqlSessionTemplate UserMapper selectUser user 整合 Mybatis public

12.整合Mybatis

12.1.步骤:

1.导入相关jar包

  • junit
  • mybatis
  • mysql数据库
  • spring相关的
  • aop织入
  • mybatis-spring【new】

2.编写配置文件

3.测试

12.2.回忆Mybatis

  • 编写实体类
  • 编写核心配置文件
  • 编写接口
  • 编写Mapper.xml
  • 编写utils
  • 测试类

12.3.Mybatis-spring

1.编写数据源配置

ApplicationContext-Mybatis.xml

<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="<http://www.springframework.org/schema/beans>"
       xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>"
       xsi:schemaLocation="<http://www.springframework.org/schema/beans>
        <https://www.springframework.org/schema/beans/spring-beans.xsd>
        ">
    <!--DataSource:使用Spring的数据源替换Mybatis的配置  c3p0 dbcp druid
    我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasource.DriverManagerDataSource
    -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;userUnicode=ture&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/itxiaofei/mapper/*.xml"/>
    </bean>

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

ApplicationContext.xml

<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="<http://www.springframework.org/schema/beans>"
       xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>"
       xsi:schemaLocation="<http://www.springframework.org/schema/beans>
        <https://www.springframework.org/schema/beans/spring-beans.xsd>
        ">

    <!--导入Mybatis-spring配置文件-->
    <import resource="ApplicationContext-Mybatis.xml"/>
    <!--注入UserMapperImpl-->
    <bean id="UserMapper" class="com.itxiaofei.mapper.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
    </bean>
</beans>

2.sqlSessionFactory

3.sqlSessionTemplate

4.需要给接口加实现类【***MappeImpl】

public class UserMapperImpl implements  UserMapper{

    //我们的所有操作,都使用sqlSession来执行,
    // 现在都是用sqlSessionTemplate来执行
    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    public List<User> selectUser() {
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        List<User> user = mapper.selectUser();
        return user;
    }
}

5.将自己写的实现类注入到Spring中

<bean id="UserMapper" class="com.itxiaofei.mapper.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>

6.测试即可

@Test
    public void selectUser(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserMapper userMapper = context.getBean("UserMapper", UserMapper.class);
        for (User user : userMapper.selectUser()) {
            System.out.println(user);
        }
    }

SqlSessionDaoSupport

SqlSessionDaoSupport是一个抽象的支持类,用来为你提供 SqlSession。调用 getSqlSession()方法你会得到一个 SqlSessionTemplate,之后可以用于执行 SQL 方法。

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{

    public List<User> selectUser() {
//        SqlSession sqlSession = getSqlSession();
//        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//        List<User> user = mapper.selectUser();
//        return user;
//        以下为精简版
        return getSqlSession().getMapper(UserMapper.class).selectUser();
    }
}

标签:12,sqlSessionTemplate,UserMapper,selectUser,user,整合,Mybatis,public
From: https://www.cnblogs.com/itxiaofei/p/16845440.html

相关文章

  • Mybatis入门
    一、MyBatis历史及特性MyBatis最初是Apache的一个开源项目iBatis,2010年6月这个项目由ApacheSoftwareFoundation迁移到了GoogleCode。随着开发团队转投GoogleCode......
  • 使用int128记录大整数
    部分编译器支持128bit的整数表示,但是需要手写输入输出。注意:__int128等价于__int128_t,不存在int128和int128_t。输出函数:voidPrint128(__int128num){if(num<0......
  • 【性能测试】Loadrunner12.55(二)-飞机订票系统-脚本录制
    1.1飞机订票系统Loadrunner12.55不会自动安装飞机订票系统,要自己手动安装。我们需要下载WebTools以及一个小插件strawberryhttps://marketplace.microfocus.com/appd......
  • VMware WorkStation Pro 16安装MacOS 13 ,12手把手图文教程(全流程详细图解)
    因为工作需要使用VMwarePro16虚拟机进行安装MacOS12系统(macOS13安装同样教程,同样附了macOS13的下载,这里就以12为例进行了安装)下面我就把自己安装的过程以及需要的文......
  • Mybatis查看最终执行的SQL
    SimpleExecutor-->doQuery  ......
  • 力扣 112. 路径总和
    112.路径总和给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标......
  • mac版 AutoCAD(LT)安装失败,提示错误“Error 112”的解决方法
    很多网友反映,第一次安装AutoCAD(LT)2022或者2023的时候都能成功,但是有问题卸载后,想要重装时,安装到一定进度后,进度条会回退到0,然后提示安装失败,错误Error112。,这种情况如何......
  • Springboot整合mybatis-plus-2增删改查
    文章目录​​一、插入​​​​1.主键id配置自增的​​​​2.时间等自动填充​​​​1.直接在数据字段设置即可​​​​2.使用ioc反射原理​​​​二、改(update)​​​​三、......
  • Springboot 整合 thymeleaf-1
    文章目录​​thymeleaf百叶香​​​​一、添加依赖​​​​1.创建项目的时候引入依赖​​​​2.直接在pom里面添加依赖​​​​二、建立页面html并添加命名空间​​​​三......
  • Spring-12-事务
    文章目录​​1.使用注解写​​​​1.在ApplicationContext.xml写入代码​​​​2.在实现接口类的公共方法上面写注解​​​​方法一:​​​​方法二:​​​​2.使用在配置中实......