添加数据库以及mybatis maven依赖
jdbc:mybatis包含了spring-boot-starter-jdbc可以不用添加
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
|
编写mapper接口和相应的mapper xml文件
UserMapper.java
如果不添加@Mapper注解可以在SpringMybatisApplication.java也就是springboot启动类添加@MapperScan(“cn.kil.springboot_mybatis.mapper”)注解指定mapper接口的扫描路径。
1 2 3 4 5 6 7 8 9 10 11
| package cn.kil.springboot_mybatis.mapper;
import cn.kil.springboot_mybatis.pojo.User; import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper public interface UserMapper { public List<User> getAllUserList(); }
|
UserMapper.xml
1 2 3 4 5 6 7 8 9 10
| <?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.kil.springboot_mybatis.mapper.UserMapper"> <select id="getAllUserList" resultType="user"> select * from kivm.k_users </select> </mapper>
|
附加相应的实体类信息User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| package cn.kil.springboot_mybatis.pojo;
import java.util.Date;
public class User { private int uid; private String username; private String password; private String email; private String phone; private String icon; private Date createTime; private int status;
public User() { }
public User(int uid, String username, String password, String email, String phone, String icon, Date createTime, int status) { this.uid = uid; this.username = username; this.password = password; this.email = email; this.phone = phone; this.icon = icon; this.createTime = createTime; this.status = status; }
public int getUid() { return uid; }
public void setUid(int uid) { this.uid = uid; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
public String getIcon() { return icon; }
public void setIcon(String icon) { this.icon = icon; }
public Date getCreateTime() { return createTime; }
public void setCreateTime(Date createTime) { this.createTime = createTime; }
public int getStatus() { return status; }
public void setStatus(int status) { this.status = status; }
@Override public String toString() { return "User{" + "uid=" + uid + ", username='" + username + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", phone='" + phone + '\'' + ", icon='" + icon + '\'' + ", createTime=" + createTime + ", status=" + status + '}'; } }
|
编辑spring配置文件application.properties添加数据库以及Mapper xml信息
1 2 3 4 5 6 7 8 9
| spring: datasource: url: jdbc:mysql://localhost:3306/kivm?useSSl=true&useUnicode=true&characterEnding=utf8&serverTimezone=Asia/Shanghai username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:cn/kil/springboot_mybatis/mapper/*Mapper.xml #mapper文件路径 type-aliases-package: cn.kil.springboot_mybatis.pojo #实体类别名
|
测试
附加项目的文件目录信息
总结
- 添加数据库连接信息
- 设置@Mapper注解或者指定@MapperScan注解扫描的包路径
- 指定mapper-locations的路径