SpringBoot中配置mybatis

①pom.xml中引入


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.2.2

②resources下创建以下文件

SpringBoot中配置mybatis_第1张图片

mybatis-config.xml内容如下




    
        
    

AccountMapper.xml  这是一个映射文件 内容如下


#这边的包路径和sql语句需要根据自身的表的写


        

③编写Mapper接口

 

@Mapper
public interface AccountMapper {
    public Account getAccount(String userName);
}

④编写sql映射mapper接口



        

⑤application.yaml中配置mybatis规则

#配置mybatis规则
mybatis:
#  全局配置的文件位置
#  config-location: classpath:mybatis/mybatis-config.xml
#  映射路径
  mapper-locations: classpath:mybatis/mapper/*.xml
#  可以不写配置文件路径 写下面这个 但是上面的全局配置文件和下面这个不能同时存在 否则SpringBoot 不知道解析哪一个
  configuration: #指定mybatis全局配置文件中的配置项
    map-underscore-to-camel-case: true

接下去是使用:

@Service
public class AccountService {

    @Autowired
    AccountMapper accountMapper;

    public Account getAccountByuserName(String userName){
        return accountMapper.getAccount(userName);
    }
}

@Data
public class Account {

    private String userName;
    private String password;
}

@Autowired
AccountService accountService;

@ResponseBody
@GetMapping("/acct")
public Account getByuserName(@RequestParam("userName") String userName){

    return accountService.getAccountByuserName(userName);
}

 

你可能感兴趣的:(java,spring,boot)