springBoot15_数据访问02_整合mybatis、注解mybatis、使用MapperScan批量扫描所有的Mapper接口、配置版mybatis

新建项目,选择依赖
在这里插入图片描述
这种依赖不是springboot官方出的,官方是spring开头
在这里插入图片描述
引入druid数据源
在这里插入图片描述
1、配置druid数据源
2、引入sql文件建表,注意classpath:后没有空格

在这里插入图片描述
创建完表后记得注释掉上边schema部分,否则每次程序启动都会重新创建表
引入log4j.properties以及log4j依赖

<!--引入log4j依赖-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

3、创建javaBean封装数据

注解mybatis

//指定操作数据库的mapper
@Mapper
public interface DepartmentMapper {

    @Select("select * from department where id=#{id}")
    public Department getDeptById(Integer id);

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

    @Update("update department set departmentname=#{departmentName} where id=#{id}")
    public int updateDept(Department department);

    //使用自动生成的主键,并告知bean中哪个属性封装主键,这样查询结果id也有了
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into department(departmentname) values(#{departmentName})")
    public int insertDept(Department department);
}

controller

@RestController
public class DeptController {

    @Autowired
    DepartmentMapper departmentMapper;

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

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

问题:

当我改变表的column为以下形式
在这里插入图片描述
会发现封装不上
在这里插入图片描述
需要自定义mybatis配置,给容器中添加一个ConfigurationCustomizer;

//mybatis自定义配置
@Configuration
public class MybatisConfig {

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

这时可以查出
在这里插入图片描述
也可以直接在application.yml中配置,就不需要定义配置类了
在这里插入图片描述

使用MapperScan批量扫描所有的Mapper接口;

这样就不需要在每个mapper都加注解了

@MapperScan(value = "com.sjg.springboot.mapper")
@SpringBootApplication
public class Springboot06DataMybatisApplication {

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

配置版mybatis

employeeMapper.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.sjg.springboot.mapper.EmployeeMapper">

    <select id="getEmployeeById" parameterType="int" resultType="com.sjg.springboot.bean.Employee">
        SELECT * from employee where id=#{id}
    </select>

    <insert id="insertEmp" parameterType="com.sjg.springboot.bean.Employee">
        insert into employee(lastname, email, gender, d_id) values(#{lastName}, #{email}, #{gender}, #{dId})
    </insert>
</mapper>

配置文件路径
这里之前的驼峰设置要注释,否则报错,config和config-location不能同时出现
在这里插入图片描述
需要在mybatis配置文件中设置驼峰命名
mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值