源码地址: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