IDEA创建Mybatis项目
第一步:创建库表
-- 创建数据库
create database mybatis_db;
-- 使用数据库
use mybatis_db;
-- 创建user表
CREATE TABLE user (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 插入数据
INSERT INTO user (username, password, email) VALUES
('aaa', 'pass1', 'user1@example.com'),
('bbb', 'pass2', 'user2@example.com'),
('ccc', 'pass3', 'user3@example.com');
第二步:创建项目引入maven依赖
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
第三步:创建mybatis-config.xml 配置文件
在创建好 mybatis-config.xml 中将下面这段内容贴进去,根据项目情况进行修改
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
修改内容示例如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--驱动-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--url,后面是使用的数据库名称-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_db"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- <mappers>-->
<!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
<!-- </mappers>-->
</configuration>
第四步:创建UserDao.xml
创建UserDao.xml文件
创建好UserDao.xml后将mybatis官网中的模板内容粘贴进来,根据项目内容进行修改
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
修改内容示例如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xu.dao.UserDao">
<!--namespace标签为该xml对应的接口全路径-->
<!--id标签同接口中的方法名findAll-->
<!--resultType标签标识返回值的类型,这里也要用全路径-->
<select id="findAll" resultType="com.xu.pojo.User">
<!--sql语句-->
select * from user
</select>
</mapper>
UserDao 接口类如下:
import java.util.List;
public interface UserDao {
/**
* 查找数据库中所有的用户信息
* @return
*/
List<User> findAll();
}
User类如下:
public class User {
/**
* id
*/
private int id;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 邮箱
*/
private String email;
public int getId() {
return id;
}
public void setId(int 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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
}
第五步:测试
import com.xu.dao.UserDao;
import com.xu.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Demo {
public static void main(String[] args) throws IOException {
//mybatis配置文件的路径
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//传入对应配置文件的输入流,读取配置文件获得SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//通过SqlSessionFactory,获取SqlSession对象(这里可以理解为是一个数据库连接)
SqlSession session = sqlSessionFactory.openSession();
//获取UserDao对象
UserDao userDao = session.getMapper(UserDao.class);
//调用findAll方法
List<User> userList = userDao.findAll();
for (User user : userList) {
System.out.println(user);
}
//资源关闭,释放资源
session.close();
}
}
执行结果如下:
以上便是基于maven对mybatis框架的初步使用步骤,欢迎各位大佬评论区讨论交流~
插曲
如果遇到了下面的异常信息
Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
可以修改数据库连接里面的url,禁用SSL连接即可(useSSL=false)
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_db?useSSL=false"/>