一、创建SpringBoot整合Mybatis的项目
【1】勾选web
、JDBC
、MySQL
、MyBatis
:
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