SpringBoot(九):整合Mybatis
一、导入依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.1</version> </dependency>
二、在application配置文件中配置
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/blms?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis配置
mybatis.type-aliases-package=com.jms.edu.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
这里对mybatis进行了两种配置,一种是别名,一种是mapper文件的位置。
三、接口以及xml实现
接口
package com.jms.edu.mapper; import com.jms.edu.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Mapper @Repository public interface UserMapper { User queryUser(@Param("uname") String uname, @Param("upassword")String upassword); }
xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.jms.edu.mapper.UserMapper"> <select id="queryUser" resultType="user"> select uid, uname, upassword, unickname, uemail, uimage, upermission from edums.user where uname=#{uname} and upassword=#{upassword} </select> </mapper>