SpringBoot实战之整合mybatis
来源:互联网 发布:淘宝亏本冲销量 编辑:程序博客网 时间:2024/05/21 09:13
前言
引用百度百科:
- 简单易学:本身就很小且简单。没有任何第三方依赖,最简单安装只要两个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=9527spring.datasource.driver-class-name=org.mariadb.jdbc.Driverspring.datasource.url=jdbc:mariadb://192.168.175.128:3306/exercise?useUnicode=true&characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=root@mariadbmybatis.mapper-locations=classpath*:mybatis/*Mapper.xmlmybatis.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实战之入门
springboot实战之文章汇总
springboot实战之读取配置文件
springboot实战之整合jsp模版引擎
springboot实战之整合freemarker模版引擎
springboot实战之注册自定义Servlet
springboot实战之注册filter和listener
springboot实战之注册interceptor
springboot实战之整合slf4j日志系统
springboot实战之整合CommandLineRunner
springboot实战之整合restful工具swagger2
springboot实战之整合jdbc进行crud操作
阅读全文
0 0
- SpringBoot实战之整合mybatis
- springboot实战之整合CommandLineRunner
- SpringBoot入门之整合mybatis
- springBoot(四)整合之MyBatis整合
- springboot实战之整合jsp模版引擎
- springboot实战之整合freemarker模版引擎
- springboot实战之整合slf4j日志系统
- springboot实战之整合restful工具swagger2
- springboot之整合mybatis-annotation(注解方式)
- springboot【13】数据访问之整合Mybatis
- SpringBoot整合Mybatis之进门篇
- 一头扎进springboot之整合mybatis
- SpringBoot学习:SpringBoot整合mybatis
- ElasticSearch整合springboot实战
- SpringBoot和Mybatis整合
- springboot 整合mybatis
- springboot+mybatis+dubbo整合
- SpringBoot整合MyBatis
- 第15周项目3- B-树的基本操作
- Codeforces896A Nephren gives a riddle
- 百度AIG视觉项目组--面试题
- 翻译布尔表达式
- Python_爬虫学习_1
- SpringBoot实战之整合mybatis
- 第十五周项目1 验证算法(4)交换排序之快速排序
- 逻辑回归 — Logistic Regression
- Kubernetes dashboard1.5.2 Web UI搭建
- 非极大值抑制算法 NMS
- 测试小工具2——拍摄并保存多张图片
- 收藏』VUE优秀UI组件库合集
- Markdown插入图片
- Dubbo框架学习-负载均衡