- 建表
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROp TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `real_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `age` int(11) NULL DEFAULT NULL, `sex` tinyint(255) NULL DEFAULT NULL, `birthday` timestamp NULL DEFAULT NULL, `created` timestamp NULL DEFAULT NULL, `updated` timestamp NULL DEFAULT NULL, `remarks` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, 'zhangsan', '123456', '张三', 22, 1, '1997-05-13 00:00:00', '2009-06-01 00:00:00', '2011-07-15 00:00:00', '张三同学在学习Java'); INSERT INTO `user` VALUES (2, 'lisi', '123456', '李四', 26, 2, '1993-09-14 07:37:18', '2013-07-11 00:00:00', '2017-07-23 00:00:00', '李斯同学在学习数据结构'); INSERT INTO `user` VALUES (3, 'wangwu', '123456', '王五', 23, 1, '1996-04-13 00:00:00', '2015-09-06 00:00:00', '2017-08-25 00:00:00', '王五同学在学习HTML'); SET FOREIGN_KEY_CHECKS = 1;
-
创建springboot项目
-
导入依赖
org.springframework.boot spring-boot-starter-data-jpa org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime
- application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:/springbootdb?userUnicode=true&characterEncoding=UTF-8
hikari:
maximum-pool-size: 30
minimum-idle: 10
idle-timeout: 60000
- 创建实体类
public class User {
private Integer id;
private String userName;
private String password;
private Integer age;
private Date birthday;
private Date created;
private Date updated;
private String remarks;
public User() {
}
public User(Integer id, String userName, String password, Integer age, Date birthday, Date created, Date updated, String remarks) {
this.id = id;
this.userName = userName;
this.password = password;
this.age = age;
this.birthday = birthday;
this.created = created;
this.updated = updated;
this.remarks = remarks;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
}
- 测试
@SpringBootTest
class DemoApplicationTests {
@Autowired
private DataSource dataSource;
@Test
void contextLoads() {
try {
System.out.println(dataSource.getConnection());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
- 测试结果:
创建UserMapper.java
@Mapper
public interface UserMapper {
@Select("select * from user where id = #{id}")
public User findById(int id);
}
测试:
@Resource
private UserMapper userMapper;
@Test
void contextLoads() {
System.out.println(userMapper.findById(1));
}



