博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot-(9)-MyBatis 操作数据库
阅读量:4625 次
发布时间:2019-06-09

本文共 3111 字,大约阅读时间需要 10 分钟。

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

一, 创建基于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@Componentpublic 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@RestControllerpublic 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;    }}

 

转载于:https://www.cnblogs.com/yangzigege/p/10433723.html

你可能感兴趣的文章
C#根据html生成PDF
查看>>
Neutron SDN 手动实现手册
查看>>
linux下core文件调试方法
查看>>
20个创意404错误页面设计的启示
查看>>
基础训练 芯片测试
查看>>
如何用命令将本地项目上传到git
查看>>
JavaScript 实现鼠标拖动元素
查看>>
js 模糊查询 (360接口)
查看>>
python+rabbitMQ实现生产者和消费者模式
查看>>
“模态”对话框和“后退”按钮
查看>>
关于javascript实现的网站页面侧边悬浮框"抖动"问题
查看>>
linux_命令格式和命令提示符
查看>>
Cocos2d-X-3.0之后的版本的环境搭建
查看>>
when case group by 的用法集合
查看>>
洛谷P1908 逆序对
查看>>
转义符
查看>>
poj 1019
查看>>
asp.net mvc上传文件
查看>>
bitmq集群高可用测试
查看>>
主成分分析(PCA)原理详解
查看>>