这里仅仅以插入数据为例:

一, 创建基于MyBatis的项目

  具体流程参考之前帖

二,创建Mapper接口

public interface AccountMapper {

    @Insert("insert into accounts (nickName, lastName, password, email, mobilePhone, address, photo, authority, gender) " + "values(#{nickName}, #{lastName}, #{password}, #{email}, #{mobilePhone}, #{address}, #{photo}, #{authority}, #{gender})")

    Boolean insertAccount(@Param("nickName") String nickName,
@Param("lastName") String lastName,
@Param("password") String password,
@Param("email") String email,
@Param("mobilePhone") String mobilePhone,
@Param("address") String address,
@Param("photo") String photo,
@Param("authority") String authority,
@Param("gender") String gender);
}

三,在入口类注解 Mapper扫描路径(包名)

  @MapperScan

@SpringBootApplication
@MapperScan("com.nfdw.accountsystem.mappers")
public class AccountSystemApplication { public static void main(String[] args) {
SpringApplication.run(AccountSystemApplication.class, args);
} }

四,声明Service类,操作database

@EnableAutoConfiguration
@Component
public class AccountService { @Autowired
private AccountMapper accountMapper; public Boolean registerAccount(Account account) {
/*
* @Param("nickName") String nickName,
@Param("lastName") String lastName,
@Param("password") String password,
@Param("email") String email,
@Param("mobilePhone") String mobilePhone,
@Param("address") String address,
@Param("photo") String photo,
@Param("authority") String authority,
@Param("gender") String gender
* */
boolean result = false;
try {
result = accountMapper.insertAccount(
account.getNickName(),
account.getLastName(),
account.getPassword(),
account.getEmail(),
account.getMobilePhone(),
account.getAddress(),
account.getPhoto(),
account.getAuthority(),
account.getGender()
);
} catch (Exception e) {
System.out.println("register error: " + e);
result = false;
}
return result;
} }

  @EnableAutoConfiguration 运行自动配置

  @Component 注解为组件,运行@Autowired自动注入

五, 调用service类,插入数据

@EnableAutoConfiguration
@RestController
public class AccountController { private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private AccountService accountService; @PostMapping("/register")
public String register(@RequestBody AccountEntity account) {
logger.info("----------\nregister with account info: " + account);
boolean result = accountService.registerAccount(account); return "register result: " + result;
}
}

SpringBoot-(9)-MyBatis 操作数据库的更多相关文章

  1. SpringBoot 整合Mybatis操作数据库

    1.引入依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...

  2. kotlin + springboot整合mybatis操作mysql数据库及单元测试

    项目mybatis操作数据库参考: http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908 junit对controller ...

  3. JAVA - SpringBoot项目引用MyBatis操作数据库

    JAVA - SpringBoot项目引用MyBatis操作数据库 1. 创建SpringBoot项目,参考:https://www.cnblogs.com/1285026182YUAN/p/1232 ...

  4. springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验--异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档---jpa访问数据库及page进行分页---整合redis---定时任务

    springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验-- 异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档 ...

  5. SpringBoot:4.SpringBoot整合Mybatis实现数据库访问

    在公司项目开发中,使用Mybatis居多.在 SpringBoot:3.SpringBoot使用Spring-data-jpa实现数据库访问 中,这种jpa风格的把sql语句和java代码放到一起,总 ...

  6. mybatis 操作数据库(05)

    类型转换.动态排序,查询接口与mapper对应关系说明及其注意事项 一.MyBatis 自带写常见类型转换器.例如:java 类中 String 对应 mySQL中的varchar 二.自定义类型转换 ...

  7. 通过MyBatis操作数据库

    MyBatis是一款优秀的持久层框架,同样也是做OR Mapping的.与JPA不同,MyBatis里面需要我们自己来定制sql. MyBatis和JPA的选择 其实如果业务比较操作比较简单使用JPA ...

  8. 【使用篇二】SpringBoot使用JdbcTemplate操作数据库(12)

    Spring对数据库的操作在jdbc上面做了深层次的封装,提供了JdbcTemplate模板. 在SpringBoot使用JdbcTemplate很简单: 引入数据库驱动包(mysql或oracle) ...

  9. SpringBoot 使用Mybatis操作mysql示例

    1.准备数据库 创建数据库 create databases baodanjia; 创建帐号 create user 'baodanjia'@'%' identified by '123456' gr ...

  10. MyBatis操作数据库(基本增删改查)

    一.准备所需工具(jar包和数据库驱动) 网上搜索下载就可以 二.新建一个Java project 1.将下载好的包导入项目中,build path 2.编写MyBatis配置文件:主要填写prope ...

随机推荐

  1. 完美解决xhost +报错: unable to open display ""

    https://blog.csdn.net/wojiuguowei/article/details/79201845

  2. 问题:Linux 输入任何命令都显示 -bash: fork: Cannot allocate memory

    应该是某个程序吃掉了所有的内存,只能重启

  3. 如何轻松的把图片导入execl表格中

    在项目中有时候会遇到往数据库中导数据的时候,往往需要把图片也一起导入execl表格中,那怎么才能把图片一块导入至execl中呢?那么今天我们就来看看怎么实现吧! 如何实现?今天我们就来用jxl和poi ...

  4. Android-TextView属性ellipsize多行失效的解决思路

    多余文字显示省略号的常规做法 android:ellipsize="end" //省略号显示在末尾 android:ellipsize="middle" //省 ...

  5. 结构体和类中属性定义需要static地方

    private function Readxxx:Integer;static; public class property XXX:Integer read ReadXXx; Txxx =recor ...

  6. construct-binary-tree-from-preorder-and-inorder-traversal——前序和中序求二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:  You may assume tha ...

  7. 串匹配算法讲解 -----BF、KMP算法

      参考文章: http://www.matrix67.com/blog/archives/115     KMP算法详解 http://blog.csdn.net/yaochunnian/artic ...

  8. 解读Unity中的CG编写Shader系列1——初识CG

    CG=C for Graphics  用于计算机图形编程的C语言超集 前提知识点: 1.CG代码必须用 CGPROGRAM ... ENDCG括起来 2.顶点着色器与片段着色器的主函数名称可任意,但须 ...

  9. Jenkins 的安装与简单使用

    一.安装 项目中接触到了jenkins感觉是一个不错的项目发布构建工具,自己就简单的学习了一下,记录一下方便以后使用 jenkin下载地址:https://jenkins-ci.org/   我直接使 ...

  10. php实现类似淘宝最近浏览商品的功能模型代码

    <?php //TempNum 显示临时记录数 $TempNum=5; //setcookie("RecentlyGoods", "12,31,90,39" ...