SpringBoot-(9)-MyBatis 操作数据库
这里仅仅以插入数据为例:
一, 创建基于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 操作数据库的更多相关文章
- SpringBoot 整合Mybatis操作数据库
1.引入依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...
- kotlin + springboot整合mybatis操作mysql数据库及单元测试
项目mybatis操作数据库参考: http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908 junit对controller ...
- JAVA - SpringBoot项目引用MyBatis操作数据库
JAVA - SpringBoot项目引用MyBatis操作数据库 1. 创建SpringBoot项目,参考:https://www.cnblogs.com/1285026182YUAN/p/1232 ...
- springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验--异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档---jpa访问数据库及page进行分页---整合redis---定时任务
springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验-- 异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档 ...
- SpringBoot:4.SpringBoot整合Mybatis实现数据库访问
在公司项目开发中,使用Mybatis居多.在 SpringBoot:3.SpringBoot使用Spring-data-jpa实现数据库访问 中,这种jpa风格的把sql语句和java代码放到一起,总 ...
- mybatis 操作数据库(05)
类型转换.动态排序,查询接口与mapper对应关系说明及其注意事项 一.MyBatis 自带写常见类型转换器.例如:java 类中 String 对应 mySQL中的varchar 二.自定义类型转换 ...
- 通过MyBatis操作数据库
MyBatis是一款优秀的持久层框架,同样也是做OR Mapping的.与JPA不同,MyBatis里面需要我们自己来定制sql. MyBatis和JPA的选择 其实如果业务比较操作比较简单使用JPA ...
- 【使用篇二】SpringBoot使用JdbcTemplate操作数据库(12)
Spring对数据库的操作在jdbc上面做了深层次的封装,提供了JdbcTemplate模板. 在SpringBoot使用JdbcTemplate很简单: 引入数据库驱动包(mysql或oracle) ...
- SpringBoot 使用Mybatis操作mysql示例
1.准备数据库 创建数据库 create databases baodanjia; 创建帐号 create user 'baodanjia'@'%' identified by '123456' gr ...
- MyBatis操作数据库(基本增删改查)
一.准备所需工具(jar包和数据库驱动) 网上搜索下载就可以 二.新建一个Java project 1.将下载好的包导入项目中,build path 2.编写MyBatis配置文件:主要填写prope ...
随机推荐
- Laravel 5.1 框架返回状态拦截
Laravel 5.1 返回信息状态拦截 可以在app\Exceptions\Handler.php文件中修改, public function render($request, Exception ...
- Vue中的computed 解读
computed相当于属性的一个实时计算,如果实时计算里关联了对象,那么当对象的某个值改变的时候,同事会出发实时计算.比如: <body id="content"> & ...
- ajax 将整个表单提交到后台处理
$(document).on('click', '.user-save-btn', function () { $.ajax({ url: 'index', type: 'post', dataTyp ...
- Struts学习总结(一)
1,需要包下载地址: http://archive.apache.org/dist/struts/binaries/ 2.报错:org/apache/commons/lang3/StringUtils ...
- 2017-10-29-afternoon-清北模拟赛
T1 洗澡 贪心:将未匹配的右括号花费1变为左括号,最有多余的左括号有一半变成右括号 #include <cstring> #include <cstdio> ); int n ...
- 洛谷—— P2895 [USACO08FEB]流星雨Meteor Shower
P2895 [USACO08FEB]流星雨Meteor Shower 题目描述 Bessie hears that an extraordinary meteor shower is coming; ...
- android学习资源
https://codelabs.developers.google.com/ https://developer.android.com/ http://v.youku.com/v_show/id_ ...
- andriod 获得应用程序名称
import android.annotation.TargetApi; import android.app.Activity; import android.app.AlertDialog; im ...
- apache一个IP绑定多个域名
用ServerAlias <VirtualHost *:80>ServerAdmin i@kuigg.comDocumentRoot /www/kuigg.comServerName ku ...
- sublime的tab和spaces空格切换的坑
python是严格要求对齐或者叫缩进的: 使用sublime对python进行编程时,可以使用tab或者空格,但是不能混用.特别是从外面把代码拷贝进sublime的时候,更要注意是否一致. 简单介绍一 ...