目录
1)准备mybatisdb.sql,创建数据库表和添加数据;
4)创建com.itheima.domain包,创建User实体类
5)操作数据库时,要基于dao,创建com.itheima.dao包,创建接口IUserDao,用户的持久层接口;
6)准备mybatis的环境,创建mybatis的主配置文件,在resources下新建SqlMapConfig.xml
7)创建映射配置文件,在resources下新建com/itheima/dao/IUserDao.xml
8)在resources下,创建log4j.properties
2)test/java/目录下,创建com.itheima.test.MybatisTest
源码:day01_eesy_02mybatis_annotation
Mybatis官网:https://mybatis.org/mybatis-3/zh/index.html
1、环境搭建
1)准备mybatisdb.sql,创建数据库表和添加数据;
drop database mybatis;
create database mybatis;
drop table if exists `mybatis`.`user`;
CREATE TABLE `mybatis`.`user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` datetime DEFAULT NULL COMMENT '生日',
`sex` char(1) DEFAULT NULL COMMENT '性别',
`address` varchar(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `mybatis`.`user` (`id`, `username`, `birthday`, `sex`, `address`) VALUES ('41', '老王', '2020-02-27 17:47:08', '男', '上海市嘉定区');
INSERT INTO `mybatis`.`user` (`id`, `username`, `birthday`, `sex`, `address`) VALUES ('42', '小王', '2020-03-01 13:17:25', '女', '上海市徐汇区');
INSERT INTO `mybatis`.`user` (`id`, `username`, `birthday`, `sex`, `address`) VALUES ('43', '老李', '2020-02-27 17:47:08', '男', '上海市嘉定区');
INSERT INTO `mybatis`.`user` (`id`, `username`, `birthday`, `sex`, `address`) VALUES ('44', '小李', '2020-02-28 17:47:08', '女', '上海市嘉定区');
2)创建Maven默认工程,不选择骨架进行创建;
3)引入mybatis依赖
<dependencies>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
4)创建com.itheima.domain包,创建User实体类
package com.itheima.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
// User实例类属性要与数据库user表中的列名一样
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
5)操作数据库时,要基于dao,创建com.itheima.dao包,创建接口IUserDao,用户的持久层接口;
方法的返回值是List类型
package com.itheima.dao;
import com.itheima.domain.User;
import java.util.List;
/**
* @author nikey
* @date 2020/3/16
* 用户的持久层接口
*/
public interface IUserDao {
// 查询所有操作
List<User> findAll();
}
6)准备mybatis的环境,创建mybatis的主配置文件,在resources下新建SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis的主配置文件-->
<configuration>
<!--配置properties-->
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://192.168.171.131:3306/mybatis"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</properties>

这篇博客详细介绍了mybatis的环境搭建步骤,包括创建Maven工程、引入mybatis依赖、配置User实体类和IUserDao接口、设置SqlMapConfig.xml与映射文件等。此外,还讲解了mybatis入门案例,包括MybatisTest的创建、设计模式的应用以及如何通过注解开发和编写Dao实现类。最后,提供了相关源码供参考。
最低0.47元/天 解锁文章
2655

被折叠的 条评论
为什么被折叠?



