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;
    }
}

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值