#接口UserMapper
package com.itheima.mapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import com.itheima.pojo.User; import java.util.List; @Mapper public interface UserMapper { @Select("select * from user") public List<User> list(); }
#类User
package com.itheima.pojo; public class User { private Integer id; private String name; private short age; private Short gender; private String phone; public Integer getId() { return id; } public User(Integer id, String name, short age, Short gender, String phone) { this.id = id; this.name = name; this.age = age; this.gender = gender; this.phone = phone; } public User() { } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public short getAge() { return age; } public void setAge(short age) { this.age = age; } public Short getGender() { return gender; } public void setGender(Short gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", gender=" + gender + ", phone='" + phone + '\'' + '}'; } }
#application.properties
#驱动类名称 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #数据库连接的url spring.datasource.url=jdbc:mysql://localhost:3306/数据库名 #连接数据库的用户名 spring.datasource.username=账号 #连接数据库的密码 spring.datasource.password=密码
#测试SpringbootMybatisQuivkstartApplicationTests
package com.itheima; import com.itheima.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import com.itheima.pojo.User; import java.util.List; @SpringBootTest class SpringbootMybatisQuivkstartApplicationTests { @Autowired private UserMapper userMapper; @Test public void testListUser(){ List<User> userList=userMapper.list(); userList.stream().forEach(user -> { System.out.println(user); }); } }
#数据库mybatis
create table user( id int unsigned primary key auto_increment comment 'ID', name varchar(100) comment '姓名', age tinyint unsigned comment '年龄', gender tinyint unsigned comment '性别, 1:男, 2:女', phone varchar(11) comment '手机号' ) comment '用户表'; insert into user(id, name, age, gender, phone) VALUES (null,'白眉鹰王',55,'1','18800000000'); insert into user(id, name, age, gender, phone) VALUES (null,'金毛狮王',45,'1','18800000001'); insert into user(id, name, age, gender, phone) VALUES (null,'青翼蝠王',38,'1','18800000002'); insert into user(id, name, age, gender, phone) VALUES (null,'紫衫龙王',42,'2','18800000003'); insert into user(id, name, age, gender, phone) VALUES (null,'光明左使',37,'1','18800000004'); insert into user(id, name, age, gender, phone) VALUES (null,'光明右使',48,'1','18800000005');
==================================================================================================================================================
#数据库连接池
#idea默认是HikariDataSource(追光者)
#pom.xml更改(druid德鲁伊连接池,阿里)druid/druid-spring-boot-starter at master · alibaba/druid · GitHub
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
=================================================================================================================================================
#lombok
#pom.xml添加
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
#类
//@Getter //@Setter //@ToString //@EqualsAndHashCode @Data(等于前四个的集合) @NoArgsConstructor//无参构造 @AllArgsConstructor//全参构造