【视频&交流平台】
http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
https://gitee.com/happyangellxq520/spring-boot
http://412887952-qq-com.iteye.com/blog/2321532
需求缘起:
在前面算是比较详细的介绍了mybatis的使用,那么我们会有疑问,XML配置方式和注解方式是否共同使用呢?博主也不知道可不可以,只能尝试下。
尝试前准备:
我们spring-boot-mybatis-xml这个项目的代码,复制一份出来,取名为:spring-boot-mybatis-xml-annotation,我们会发现xml的项目中,对于注解方式的都已经配置好了,所以我们直接编码测试即可,无需做任何修改。
尝试1:查询所有
我们新建一个Mapper类,DemoMapper2,这里使用注解的方式进行使用:
package com.kfit.demo.mapper; import java.util.List; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; import com.kfit.demo.bean.Demo; /** * 测试注解和xml是否可以并存. * @author Angel --守护天使 * @version v.0.1 * @date 2017年8月17日 */ public interface DemoMapper2 { @Select("select *from demo") public List<Demo> selectAll(); @Insert("insert into demo(name) values(#{name})") @Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id") public int save(Demo demo); }
然后在DemoService注入DemoMapper2并且添加方法:
@Autowired private DemoMapper2 demoMapper2; public List<Demo> demoMapper2_selectAll(){ return demoMapper2.selectAll(); }
接着在DemoController添加访问方法:
@RequestMapping("/demoMapper2_selectAll") public List<Demo> demoMapper2_selectAll(){ return demoService.demoMapper2_selectAll(); }
Ok,到这里就可以进行测试了,访问:
http://127.0.0.1:8080/demoMapper2_selectAll 是不是能返回所有的数据,答案是可以:
{
id: 7,
name: "王五2",
}//省略…
尝试2:分页查询
分页查询,我们只需要修改下DemoMapper中的请求方法:
@RequestMapping("/demoMapper2_selectAll") public List<Demo> demoMapper2_selectAll(intpageNum){ PageHelper.startPage(pageNum,2); return demoService.demoMapper2_selectAll(); }
访问http://127.0.0.1:8080/demoMapper2_selectAll?pageNum=1 测试,可以进行分页查询。
尝试3:添加数据
在DemoMapper2添加一个一个保存数据的方法进行测试:
@Insert("insert into demo(name) values(#{name})") @Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id") public int save(Demo demo);
//省略service and controller代码…
访问:http://127.0.0.1:8080/demoMapper2_save?name=demoMapper2_save
可以看到:{"id":38,"name":"demoMapper2_save" }
说明保存也没有问题。
结论:
从上面的测试我们可以看出,我们可以使用XML方式进行开发,也可以使用注解方式进行开发,但是一般在一个项目中最好是统一风格,比较好管理。
视频&交流平台:
http://study.163.com/course/introduction.htm?courseId=1004329008
http://412887952-qq-com.iteye.com/blog/2321532
相关推荐
- [spring-boot-mybatis-xml-mulidatasource](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis/spring-boot-mybatis-xml-mulidatasource):Spring Boot + Mybatis (Xml 版) 多...
spring-boot-mybatis-annotation-mulidatasource:springboot+mybatis(注解版)多数据源最简解决方案 spring-boot-thymeleaf:simple spring boot thymeleaf demo spring-boot-jpa-thymeleaf-curd:spring boot + ...
spring-boot-mybatis-annotation-mulidatasource:springboot+mybatis(注解版)多数据源最简解决方案 spring-boot-thymeleaf:simple spring boot thymeleaf demo spring-boot-jpa-thymeleaf-curd:spring boot +...
Spring Boot整合MyBatis-Plus,使用MySQL,基于xml开发及注解开发
zeta-kotlin是使用kotlin语言基于spring boot、mybatis-plus、sa-token等框架开发的项目脚手架。zeta-kotlin目前只提供了一个最基础的RBAC用户角色权限功能。不像其它开源项目那样大而全,zeta-kotlin项目相当精简
适合初学者借鉴学习sping框架 IDEA Spring-boot+mybatis
报表后端采用技术: SpringBoot整合SSM(Spring+Mybatis-plus+ SpringMvc),spring security 全注解式的权限管理和JWT方式禁用Session,采用redis存储token及权限信息 报表前端采用Bootstrap框架,结合Jquery Ajax,...
MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs (Plain Old Java Objects, 普通的 Java 对象)映射成数据库中的记录。 以下是 MyBatis 的主要特点: 简单易学:MyBatis 框架结构...
此处SpringBoot整合Mybatis是xml形式的整合,注解整合点我 1.1 新建boot项目并导入依赖,依赖如下 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter ...
SpringBoot集成Mybatis的xml,不是注解方式,而是xml配置文档格式
基于注解的sql写法,零XML,极简配置,一键前后台代码生成功能简介 1. 用户管理 2. 角色管理 3. 部门管理 4. 菜单管理 5. 系统日志 6. 代码生成 7.内容管理 所用框架 前端 1. Bootstrap 2. jQuery 3. bootstrap-...
在Spring Boot中,可以通过整合MyBatis和JPA来与数据库进行交互。以下是分别整合MyBatis和JPA的步骤: 整合MyBatis: 1. 添加依赖: 在项目的 pom.xml 文件中添加MyBatis和MySQL依赖 2.配置数据源: 在 application....
- Spring Boot 数据库操作,包括SpringJDBC、JPA、Mybatis注解版 & XML版、MongoDB。其中,每个版本都有其对应的多数据源解决方案。 - springboot-caches - Spring Boot 缓存,包括redis、ehcache、spring-cache...
Spring Boot Examples Spring Boot使用的各种示例...spring-boot-mybatis-annotation-mulidatasource:springboot+mybatis(注解版)多数据源最简解决方案 spring-boot-thymeleaf:simple spring boot thymeleaf demo s
#spring-boot-dubbo脚手架 ##框架包含那些? mybatis-plus 增强版 mybatis,简化mapper,service等待代码编写任务 spring-boot 简化新Spring应用的初始搭建以及开发过程 spring-boot-starter-dubbo 基于dubbo 2.5.3的...
有关Mybatis雪花ID主键插件前面写了两篇博客作为该项目落地的铺垫。 1、 2、 该插件项目可以直接运用于实际开发中,作为分布式数据库表主键ID使用。 一、项目概述 1、项目背景 在生成表主键ID时,我们可以考虑主键...
springboot + mybatis(通用mapper) + HikariCP(比durid更快)多数据源,从此单表的xml文件和crud就不需要自己写了
使用了SpringMVC框架,Spring4的xml配置和注解配置。 5. 前台展示页面使用了JQuery EasyUI的布局、列表和消息组件。 6. 系统实现了较为完善的用户模块,对用户数据实现了增、删、改、查、分页等的功能。
很好的实例,简单易懂,适合新手
Spring Boot 2.0版本以上的源码 使用 Spring Boot 的优势 使用 Spring Boot 开发项目,会给我们带来非常美妙的开发体验,可以从以下几个方面展开来说明 Spring Boot 让开发变得更简单 Spring Boot 对开发效率的提升...