一、創建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