pom.xml增加如下依赖:
<!--MyBatis的整合--> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.17</version> </dependency>
application.properties文件增加如下配置:
spring.datasource.url=jdbc:mysql://localhost:3306/sway-doctor-service-user spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 添加并配置第三方数据源Druid # 数据源类型 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 初始化连接数 spring.datasource.initialSize=20 # 最小空闲数 spring.datasource.minIdle=10 # 最大连接数 spring.datasource.maxActive=100 # 配置MyBatis的XML配置文件位置 mybatis.mapper-locations = classpath:mapper/*.xml # 配置XML映射文件中指定的实体类别名路径 #mybatis.type-aliases-package = cn.com.sway.doctor.service.common # 开启驼峰命名匹配映射 mybatis.configuration.map-underscore-to-camel-case=true #mybatis.configuration.log-impl = org.apache.ibatis.logging.stdout.StdOutImpl
编写DataSourceConfig.java数据源配置文件,代码如下:
package cn.com.sway.doctor.service.user.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource getDruid() { return new DruidDataSource(); } }
编写实体Account.java文件,代码如下:
package cn.com.sway.doctor.service.common.model; import lombok.Data; @Data public class Account { private String id; private String mobile; private String password; private String name; }
编写查询用额AccountQuery.java文件,代码如下:
package cn.com.sway.doctor.service.common.query; import lombok.Data; import javax.validation.constraints.Min; import javax.validation.constraints.Pattern; @Data public class AccountQuery { private String id; @Pattern(regexp = "1\\d{10}", message = "请输入正确的手机号啊") private String mobile; private String password; @Min(value = 0, message = "offset不能为负数") private Integer offset; @Min(value = 0, message = "limit不能为负数") private Integer limit; }
编写AccountDao.java文件,代码如下:
package cn.com.sway.doctor.service.user.dao; import cn.com.sway.doctor.service.common.model.Account; import cn.com.sway.doctor.service.common.query.AccountQuery; import org.apache.ibatis.annotations.Mapper; import org.springframework.validation.annotation.Validated; import java.util.List; @Mapper public interface AccountDao { List<Account> query(@Validated AccountQuery accountQuery); }
编写AccountMapper.xml,代码如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.com.sway.doctor.service.user.dao.AccountDao"> <resultMap id="account" type="cn.com.sway.doctor.service.common.model.Account"> <id property="id" column="id"/> <result property="mobile" column="mobile"/> <result property="name" column="name"/> </resultMap> <!-- 查询账号列表 --> <select id="query" resultMap="account"> SELECT * FROM account <where> <if test="mobile!=null and mobile!=''"> and mobile = #{mobile} </if> </where> </select> </mapper>
编写单元测,代码如下:
package cn.com.sway.doctor.service.user; import cn.com.sway.doctor.service.common.query.AccountQuery; import cn.com.sway.doctor.service.user.dao.AccountDao; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class ServiceUserApplicationTests { @Autowired private AccountDao accountDao; @Test public void testAccountMapper(){ AccountQuery accountQuery = new AccountQuery(); accountQuery.setMobile("18688814832"); System.out.println(accountDao.query(accountQuery).size()); } }