前言
引用百度百科:
- 简单易学:本身就很小且简单。没有任何第三方依赖,最简单安装只要两个jar文件+配置几个sql映射文件易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现。
- 灵活:mybatis不会对应用程序或者数据库的现有设计强加任何影响。 sql写在xml里,便于统一管理和优化。通过sql基本上可以实现我们不使用数据访问框架可以实现的所有功能,或许更多。
- 解除sql与程序代码的耦合:通过提供DAL层,将业务逻辑和数据访问逻辑分离,使系统的设计更清晰,更易维护,更易单元测试。sql和代码的分离,提高了可维护性。
- 提供映射标签,支持对象与数据库的orm字段关系映射
提供对象关系映射标签,支持对象关系组建维护
提供xml标签,支持编写动态sql。
code实现
依赖
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>com.hsy.java</groupId>
<artifactId>java-bean</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
dao层实现
TExerciseZoneMapper.java
public interface TExerciseZoneMapper {
List<TExerciseZone> selectAll(@Param(value = "offset") Integer offset, @Param(value = "limit") Integer limit) ;
int update(@Param(value = "parentId") Integer parentId,@Param(value = "id") Long id) ;
}
mapper文件
<mapper namespace="com.hsy.springboot.mybatis.mapper.TExerciseZoneMapper">
<select id="selectAll" resultType="TExerciseZone">
select * from t_exercise_zone_test where 1 = 1 limit #{offset} , #{limit};
</select>
<select id="getProvinceById" resultType="TExerciseZone">
select * from t_exercise_zone_test WHERE 1 = 1
<if test="id!=null || ''!=id">
AND id = #{id}
</if>
</select>
<update id="update">
update t_exercise_zone_test set parent_id = #{parentId} WHERE id = #{id}
</update>
</mapper>
service层实现
@Service(value = "exerciseZoneService")
public class TExerciseZoneServiceImpl implements ITExerciseZoneService{
@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired private TExerciseZoneMapper tExerciseZoneMapper ;
@Override
public List<TExerciseZone> getAll(Integer offset,Integer limit) {
return tExerciseZoneMapper.selectAll(offset,limit);
}
@Transactional
@Override
public Boolean update(Integer parentId,Long id) {
tExerciseZoneMapper.update(100000,1l) ;
int i = 1 / 0 ;
tExerciseZoneMapper.update(110000,2l) ;
return true;
}
}
web层实现
@RestController
@RequestMapping("/api/rest")
public class RestfulController extends BaseController{
@Autowired private ITExerciseZoneService exerciseZoneService ;
@RequestMapping(value = "/update/{id}",method = RequestMethod.PUT,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseBodyBean<Boolean> update(@PathVariable Long id,@RequestParam Integer parentId){
return success(exerciseZoneService.update(parentId,id)) ;
}
@GetMapping(value = {"/v1/zones/{offset}/{limit}"})
public ResponseBodyBean<List<TExerciseZone>> zoneList(@PathVariable Integer offset, @PathVariable Integer limit){
return success(exerciseZoneService.getAll(offset,limit)) ;
}
}
配置文件
server.port=9527
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://192.168.175.128:3306/exercise?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root@mariadb
mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml
mybatis.type-aliases-package=com.hsy.java.bean.po
项目入口
@SpringBootApplication
@MapperScan("com.hsy.springboot.mybatis.mapper")
public class SpringBootMybatisApplication {
public static void main(String[] args){
SpringApplication.run(SpringBootMybatisApplication.class,args) ;
}
}
项目结构图
接口测试图
源码
springboot-mybatis
历史文章
springboot实战之注册filter和listener
springboot实战之整合CommandLineRunner