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

 

上一篇:Python wordcloud 库 自学7


下一篇:Django学习(文件上传与下载)