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 ...
随机推荐
- inline关键词的使用(转载)
(一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...
- 初级Springboot(一)
初级Springboot(一) 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 一.了解Springboot 做Java开发的小伙伴都知道,我们在做项目的时候,需要去写大量的配置文件 ...
- MySQL 几种调式分析利器
目录 pstack gdb strace perf pstack 获取堆栈信息 问题线程的定位 负载较低 mysql_pid=4522 pstack $mysql_pid>pstack.info ...
- Ubuntu 16.04下使用Wine安装Xshell 4和Xftp 4
说明: 1.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 2.由于Xshell 5的C++库无法在这个Wine版本运行,即使升级官方原版的2+版 ...
- Oracle hidden costs revealed, Part2 – Using DTrace to find why writes in SYSTEM tablespace are slower than in others
http://blog.tanelpoder.com/2008/09/02/oracle-hidden-costs-revealed-part2-using-dtrace-to-find-why-wr ...
- Div 浮动到另一个div之上
转自原文Div 浮动到另一个div之上 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <met ...
- 如果当前地图文档中有独立的Table,通过Engine如何获取该Table?
将IMap转为ITableCollection,通过ITableCollection.get_Table(int index);来获取该Table
- SharpSSH
SharpSSH sharpssh is a pure .NET implementation of the SSH2 client protocol suite. It provides an AP ...
- android-BroadcastReceive广播接收器
应用可以使用它对外部事件进行过滤,只对感兴趣的外部事件(如当电话呼入时,或者数据网络可用时)进行接收并做出响应.广播接收器没有用户界面.然而,它们可以启动一个activity或service来响应它们 ...
- 使用Golang利用ectd实现一个分布式锁
http://blog.codeg.cn/post/blog/2016-02-24-distrubute-lock-over-etcd/ By zieckey · 2016年02月24日 · 1205 ...