栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring快速入门--整合mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring快速入门--整合mybatis

Spring整合mybatis 1、添加依赖
        
        
            org.springframework
            spring-webmvc
            5.3.3
        

        
            org.mybatis
            mybatis-spring
            2.0.6
        

        
            org.springframework
            spring-jdbc
            5.3.3
            compile
        

        
            org.mybatis
            mybatis
            3.5.2
        

        
            mysql
            mysql-connector-java
            8.0.26
        

        
            junit
            junit
            4.12
            test
        
2、方式一 编写配置文件
dataSource-----sqlSessionFactory--------sqlSession


    
    

    

    
        
        
        
        
    
    
    
        
        
        
        
        
    
    
    
        
    

    
        
    



编写对应接口及其实现类
package dao;

import org.apache.ibatis.annotations.Mapper;
import pojo.People;

@Mapper
public interface PeopleMapper {
    public People findById(int id);
}

实现类

package dao;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Component;
import pojo.People;

@Component
public class PeopleMapperImpl implements PeopleMapper{
    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){
        this.sqlSessionTemplate=sqlSessionTemplate;
    }
    @Override
    public People findById(int id) {
        PeopleMapper mapper = sqlSessionTemplate.getMapper(PeopleMapper.class);

        return mapper.findById(1);
    }
}
编写sql逻辑




    
    
        select * from springboot.people where id=#{id};
    






    
        
    


测试
@Test
public void test11(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    PeopleMapper people1 = context.getBean("people1", PeopleMapper.class);
    People people1ById = people1.findById(1);
    System.out.println(people1ById);
}
3、方式二

SqlSessionDaoSupport

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

只需修改spring配置文件



    
    

    

    
        
        
        
        
    
    
    
        
        
        
    
    
        
    



package dao;

import org.apache.ibatis.session.SqlSession;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import pojo.People;

public class PeopleMapperImpl extends SqlSessionDaoSupport implements PeopleMapper{
    @Override
    public People findById(int id) {
        SqlSession sqlSession = getSqlSession();
        PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class);
        return mapper.findById(1);
    }
}

测试

@Test
public void test11(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    PeopleMapper people1 = context.getBean("people1", PeopleMapper.class);
    People people1ById = people1.findById(1);
    System.out.println(people1ById);
}
4、方式三

MapperFactoryBean

需要注意的是:所指定的映射器类必须是一个接口,而不是具体的实现类。在这个示例中,通过注解来指定 SQL 语句,但是也可以使用 MyBatis 映射器的 XML 配置文件。

配置好之后,你就可以像 Spring 中普通的 bean 注入方法那样,将映射器注入到你的业务或服务对象中。MapperFactoryBean 将会负责 SqlSession 的创建和关闭。 如果使用了 Spring 的事务功能,那么当事务完成时,session 将会被提交或回滚。最终任何异常都会被转换成 Spring 的 DataAccessException 异常。


    
    

测试

@Test
public void test12(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    PeopleMapper people1 = context.getBean("mapperFactoryBean", PeopleMapper.class);
    People people1ById = people1.findById(1);
    System.out.println(people1ById);
}
Spring整合mybatis----注解 1、接口
package dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import pojo.People;

@Mapper
public interface PeopleMapper {
    @Select("select * from springboot.people where id=#{id}")
    public People findById(int id);
}
2、编写配置类
package config;

import dao.PeopleMapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;


@Configuration
@ComponentScan({"dao"})
public class springConfig {

    //dataSource
    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setUrl("jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return dataSource;
    }
    //sqlSessionFactory
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception{
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        //PathMatchingResourcePatternResolver可以用来解析资源文件,主要是用来解析类路径下的资源文件。
        //当然它也可以用来解析其它资源文件,如基于文件系统的本地资源文件。
        sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PeopleMapper getPeople() throws Exception {
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactoryBean());
        PeopleMapper mapper = sqlSessionTemplate.getMapper(PeopleMapper.class);
        return mapper;
    }

}
3、mybatis配置文件



    
        
    


    


4、测试
@Test
public void test13(){
    BeanFactory context = new AnnotationConfigApplicationContext(springConfig.class);
    PeopleMapper getPeople = context.getBean("getPeople", PeopleMapper.class);
    System.out.println(getPeople.findById(1));
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396929.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号