SpringBoot(九)——————SpringBoot整合Mybatis

源码地址:https://github.com/877148107/springboot_integrate

Mybatis官方文档:https://mybatis.org/mybatis-3/

  • Mybatis自动配置类MybatisAutoConfiguration

@org.springframework.context.annotation.Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration implements InitializingBean {

    ........

}
  • 引入Maven依赖

<!--导入Mybatis依赖-->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.1.1</version>
</dependency>
  •  目录结构

  • Mybatis注解方式

1)、Mapper接口配置

这里占位符里面的参数必须要和实体的属性一样,严格区分大小写 

public interface PersonMapper {

    @Select("SELECT * FROM person WHERE id = #{id}")
    public Person getPersonById(Integer id);

    @Delete("delete FROM person WHERE id = #{id}")
    public int deletePersonById(Integer id);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("INSERT INTO person(NAME,birthday,age)VALUES(#{name},#{birthday},#{age})")
    public int insertPerson(Person person);

    @Update("UPDATE person SET birthday=#{birthday},age=#{age} where id = #{id}")
    public int updatePerson(Person person);
}

2)、自定义Mybatis配置 

Mybatis需要开启驼峰命名法

使用@MapperScan(value = "com.wmy.integrate.mybatis.mapper")扫描该包下的所有mapper接口

@MapperScan(value = "com.wmy.integrate.mybatis.mapper")
@org.springframework.context.annotation.Configuration
public class MybatisConfig {

    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer() {
            @Override
            public void customize(Configuration configuration) {
                //开启驼峰命名法
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

3)、其他类 

@RestController
public class PersonController {

    @Autowired
    private PersonMapper personMapper;

    @GetMapping("/person/{id}")
    public Person getPerson(@PathVariable("id")Integer id){
        return personMapper.getPersonById(id);
    }

    @GetMapping("/person")
    public Person insertPerson(Person person){
        personMapper.insertPerson(person);
        return person;
    }
}

4)、测试效果

  • Mybatis配置方式

1)、添加Mybatis配置文件

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
    <!--开启驼峰命名法-->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>

2)、配置Mpper.xml文件

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.wmy.integrate.mybatis.mapper.DepartmentMapper">
    <select id="getDeptById" resultType="com.wmy.integrate.mybatis.model.Department">
        SELECT * FROM department where id=#{id}
    </select>
    
    <select id="insertDept">
        insert into department(dept_name)values(#{deptName})
    </select>
</mapper>

3)、其他代码

public interface DepartmentMapper {

    public Department getDeptById(Integer id);

    public void insertDept(Department department);
}
@RestController
public class PersonController {

    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @GetMapping("/person/{id}")
    public Person getPerson(@PathVariable("id")Integer id){
        return personMapper.getPersonById(id);
    }

    @GetMapping("/person")
    public Person insertPerson(Person person){
        personMapper.insertPerson(person);
        return person;
    }

    @GetMapping("/dept")
    public Department insertDept(Department department){
        departmentMapper.insertDept(department);
        return department;
    }

    @GetMapping("/dept/{id}")
    public Department getDept(@PathVariable("id")Integer id){
        return departmentMapper.getDeptById(id);
    }
}

4 )、SpringBoot配置文件配置


mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

5)、测试效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值