考易   发表于 2020-1-21 23:05:36 |栏目:





1. 通用 Mapper 介绍2. 快速入门2.1 搭建环境2.2 集成通用 Mapper3. 常用注解3.1 @Table3.2 @Column3.3 @Id3.4 @GeneratedValue3.5 @Transient4. 基础方法4.1 selectOne 方法4.2 select 方法4.3 selectAll 方法4.4 selectByPrimaryKey 方法4.5 selectCount 方法4.6 insert 方法4.7 insertSelective 方法4.8 updateByPrimaryKey 方法4.9 updateByPrimaryKeySelective 方法4.10 delete 方法4.11 deleteByPrimaryKey 方法5. Example 方法5.1 selectByExample 方法5.2 selectCountByExample 方法5.3 updateByExample 方法5.4 updateByExampleSelective 方法5.5 deleteByExample 方法

通用 Mapper 是一个辅助 MyBatis 单表开发的组件,它使我们能极其方便的使用 MyBatis 单表的增删改查。




创建数据库 mapper,和 table_user 表
DROP TABLE IF EXISTS `table_user`;
CREATE TABLE `table_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(20) DEFAULT NULL,
`user_gender` char(2) DEFAULT '未知',
`user_address` varchar(30) DEFAULT NULL,
`user_birthday` date DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of table_user
-- ----------------------------
INSERT INTO `table_user` VALUES ('1', '张三', '男', '北京', '2020-01-15');
INSERT INTO `table_user` VALUES ('2', '李四', '男', '上海', '2020-01-14');
INSERT INTO `table_user` VALUES ('3', '王五', '女', '广州', '2020-01-12');
1234567891011121314151617


创建工程

打开 IDEA --> Create New Project --> Empty Project --> 填写项目名 --> Finish
New Module --> Maven --> Next --> 填写项目信息 --> Finish



引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zt</groupId>
<artifactId>mapper_demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<dependencies>
<!--jdbc的启动器,默认使用 HikariCP 连接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--SpringBoot-test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
123456789101112131415161718192021222324252627282930313233343536373839404142


配置 application.properties
# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/mapper?serverTimezone=GMT+8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

12345


创建引导类
@SpringBootApplication
public class MapperApplication {
public static void main(String[] args) {
        SpringApplication.run(MapperApplication.class,args);
}
}
1234567


创建测试类,测试能否获取数据库连接
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MapperApplication.class)
public class ConnectionTest {
@Autowired
private DataSource dataSource;
@Test
public void testGetConnection() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
}
}
123456789101112
运行结果:
HikariProxyConnection@737756161 wrapping com.mysql.cj.jdbc.ConnectionImpl@2532b351
1





引入依赖
<!--通用 Mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
123456


创建实体类 User
/**
* 建立数据库与实体类之间的关系
*/
@Table(name = "table_user")
public class User implements Serializable {
private Integer userId;
private String userName;
private String userGender;
private String userAddress;
private Date userBirthday;
public User() {
}
public User(Integer userId, String userName, String userGender, String userAddress, Date userBirthday) {
this.userId = userId;
this.userName = userName;
this.userGender = userGender;
this.userAddress = userAddress;
this.userBirthday = userBirthday;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserGender() {
return userGender;
}
public void setUserGender(String userGender) {
this.userGender = userGender;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public Date getUserBirthday() {
return userBirthday;
}
public void setUserBirthday(Date userBirthday) {
this.userBirthday = userBirthday;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", userGender='" + userGender + '\'' +
", userAddress='" + userAddress + '\'' +
", userBirthday=" + userBirthday +
'}';
}
}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374


创建持久层 UserMapper
/**
* 具体操作数据库的 Mapper 接口,需要继承通用 mapper 提供的核心接口:Mapper<User>
* 泛型类型就是实体类的类型
*/
@org.apache.ibatis.annotations.Mapper
public interface UserMapper extends Mapper<User> {
}
123456789


创建 UserMapper 测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MapperApplication.class)
public class MapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectOne() {
        User user = new User(null, "张三", null, null, null);
        System.out.println(userMapper.selectOne(user));
}
}
12345678910111213
运行结果:
User{userId=1, userName='张三', userGender='男', userAddress='北京', userBirthday=Wed Jan 15 00:00:00 CST 2020}
1






作用:建立实体类和数据库表之间的对应关系。


默认规则:实体类类名首字母小写作为表名。User 类 --> user 表。


实例:
@Table(name = "table_user")
public class User implements Serializable {
12





作用:建立实体类字段和数据库表字段之间的对应关系。


默认规则:
实体类字段:驼峰式命名
数据库表字段:使用 “_” 区分各个单词


实例:
@Column(name = "user_id")
private Integer id;
12





作用:建立实体类主键字段和数据库表主键字段之间的对应关系。


实例:
@Id
private Integer userId;
12





作用:让通用 Mapper 在执行 insert 操作之后将数据库自动生成的主键值回写到实体类对象中。


实例:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer userId;
123
@Test
public void testInsert() {
    User user = new User(null, "小红", "女", "杭州", new java.util.Date());
    userMapper.insert(user);

    System.out.println(user);
}
1234567
运行结果:
User{userId=10, userName='小红', userGender='女', userAddress='杭州', userBirthday=Sat Jan 18 14:52:38 CST 2020}
1





作用:用于标记不与数据库表字段对应的实体类字段。


实例:
@Transient
private String otherThings; //非数据库表中字段
12






说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号


实例:
@Test
public void testSelectOne() {
    User user = new User(null, "张三", null, null, null);
    System.out.println(userMapper.selectOne(user));
}
12345
运行结果:
User{id=1, userName='张三', userGender='男', userAddress='北京', userBirthday=Wed Jan 15 00:00:00 CST 2020}
1





说明:根据实体中的属性值进行查询,查询条件使用等号


实例:
@Test
public void testSelect() {
    User user = new User(null, null, "男", null, null);
    List<User> users = userMapper.select(user);
for (User user1 : users) {
        System.out.println(user1);
}
}
12345678
运行结果:
User{userId=1, userName='张三', userGender='男', userAddress='北京', userBirthday=Wed Jan 15 00:00:00 CST 2020}
User{userId=2, userName='李四', userGender='男', userAddress='上海', userBirthday=Tue Jan 14 00:00:00 CST 2020}
User{userId=7, userName='小明', userGender='男', userAddress='深圳', userBirthday=Sat Jan 18 00:00:00 CST 2020}

1234





说明:查询全部结果,select(null)方法能达到同样的效果


实例:
@Test
public void testSelectAll() {
    List<User> users = userMapper.selectAll();
for (User user : users) {
        System.out.println(user);
}
}
1234567
运行结果:
User{userId=1, userName='张三', userGender='男', userAddress='北京', userBirthday=Wed Jan 15 00:00:00 CST 2020}
User{userId=2, userName='李四', userGender='男', userAddress='上海', userBirthday=Tue Jan 14 00:00:00 CST 2020}
User{userId=3, userName='王五', userGender='女', userAddress='广州', userBirthday=Sun Jan 12 00:00:00 CST 2020}
User{userId=7, userName='小明', userGender='男', userAddress='深圳', userBirthday=Sat Jan 18 00:00:00 CST 2020}

12345





说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号


注意:该方法需要使用 @Id 指定实体类中的主键,否则通用 Mapper 会默认将所有实体类字段作为联合主键


实例:
@Test
public void testSelectByPrimaryKey() {
    System.out.println(userMapper.selectByPrimaryKey(3));
}
1234
运行结果:
User{userId=3, userName='王五', userGender='女', userAddress='广州', userBirthday=Sun Jan 12 00:00:00 CST 2020}

回复 显示全部楼层 使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作者相关信息

更多资源

精品推荐

极品资源

原创模板

下载排行

热门标签

快速回复 返回顶部 返回列表