SpringBoot1.5.10整合MyBatis

一、创建SpringBoot整合Mybatis的项目

【1】勾选webJDBCMySQLMyBatis
在这里插入图片描述
pom.xml文件中mybatis适配spring的starter依赖:

<!--mybatis适配spring的starter-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

【2】配置数据源的相关属性
【3】给数据库建表(相关配置见 )
【4】创建JavaBean以及对应的Mapper

二、注解版

【1】使用@Mapper

  作用:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
  添加位置:在接口类上面

@Mapper  //在编译之后会生成相应的接口实现类
public interface DepartmentMapper {

    @Select("select id,department_name from department where id=#{id}")
    public Department getDepartById(Integer id);

    @Delete("delete from department where id = #{id}")
    public int deleteById(Integer id);

    //useGeneratedKeys:是否使用自增长主键;keyProperty:指定自增长的主键字段
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into department(id,department_name) values(null,#{departmentName})")
    public int insertDept(Department dept);

    @Update("update department set department_name=#{departmentName} where id=#{id}")
    public int updateDeptById(Department dept);
}

  如果有很多个Mapper,想要每个接口都变成实现类的话会添加很多个@Mapper,比较麻烦,可以使用@MapperScan注解批量扫描接口类。

【2】使用@MapperScan

  作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类。@MapperScan的值可以是字符串也可以是数组({包1,包2})
  添加位置:在Springboot启动类上面添加。

//使用@MapperScan批量扫描
@MapperScan(value = "com.zm.springboot.mapper")
@SpringBootApplication
public class SpringbootDataMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDataMybatisApplication.class, args);
    }
}

【3】开启自增长主键的支持

//useGeneratedKeys:是否使用自增长主键;keyProperty:指定自增长的主键字段
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(id,department_name) values(null,#{departmentName})")
public int insertDept(Department dept);

【4】自定义MyBatis的配置规则:ConfigurationCustomizer

@Configuration
public class MyBatisConfig {

    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer() {
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                //开启驼峰命名映射规则
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

三、配置文件版

【1】使用@Mapper或者@MapperScan开启注解扫描
【2】创建Mapper对应的sql映射文件

Employee.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zm.springboot.mapper.EmployeeMapper">
    <!--public Employee getEmpById(Integer id);-->
    <select id="getEmpById" resultType="com.zm.springboot.bean.Employee" parameterType="int" >
    select * from employee where id = #{id}
  </select>

    <!--public int insertEmp(Employee employee);-->
    <insert id="insertEmp" parameterType="com.zm.springboot.bean.Employee" useGeneratedKeys="true" keyProperty="id">
        insert into employee(lastName,email,gender,d_id) values(#{lastName},#{email},#{gender},#{dId})
    </insert>
</mapper>

【3】MyBatis全局配置文件

myBatis-config.xml:

<!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>

【4】在配置文件中注册MyBatis全局配置文件xml映射文件

mybatis:
  # mybatis全局配置文件位置
  config-location: classpath:mybatis/mybatis-config.xml
  # sql映射的xml文件位置
  mapper-locations:
    - classpath:mybatis/mapper/*.xml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值