2019独角兽企业重金招聘Python工程师标准>>>
1. pom添加相关依赖
org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 mysql mysql-connector-java
2.application.properties 增加数据库配置
##########数据库连接########### spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.启动类,增加Mapper 的路径扫描
@MapperScan(value = "com.example.demo.mapper")
4.使用mybaties的2中方式,
4.1 注解方式
public interface MemberMapper { @Select(" select * from member where id = #{id}") @Results({ @Result(property = "id", column = "id"), @Result(property = "userName", column = "user_name"), @Result(property = "passWord", column = "pass_word"), @Result(property = "email", column = "email"), @Result(property = "nickName", column = "nick_name"), @Result(property = "regTime", column = "reg_time"), }) Member findById(@Param("id") Long id); }
4.2 xml 方式
这里,需要注意,xml对应的map路径namespace 里面,是否正确
如果报错,说是mapper的xml无法找到
2中解决
1.需要时pom里面,配置
2.将mybaties的xml,放置在resources,在application.properties里面配置
mybatis.config-location=classpath:mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/*.xml
这样,方法就可以调用了,但是调用的时候,遇见一个问题,mybaties的驼峰设置,未开启,所以有的字段值,为null
进行配置
1.mybatis-config.xml 进行设置(放开注释)
2.在application.properties里面配置,但是需要注意configuration' and 'configLocation'不能同时使用。否则报错
#mybatis.configuration.map-underscore-to-camel-case=true