pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<groupId>org.example</groupId>
<artifactId>mybatis-demo-fangneng</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0-alpha5</version>
<scope>test</scope>
</dependency>
</dependencies>
User文件:
public class User {
private Integer id;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", age=" + age +
", score=" + score +
'}';
}
private String username;
private Integer age;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
private Integer score;
}
UserMapper文件:
public interface UserMapper {
/**
* 通过用户id查询用户名称
*
* @param id 用户id
* @return 用户名称
/
@Select(“SELECT username FROM user WHERE id = #{id}”)
String selectUsernameById(Integer id);
/*
* 通过用户id查询用户年龄
*
* @param id 用户id
* @return 用户年龄
*/
Integer selectUserAgeById(Integer id);
User selectUserById(Integer id);
}
UserMapper文件:
<resultMap id="userMap" type="entity.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="age" column="age"/>
<result property="score" column="score"/>
</resultMap>
mybatis-config文件:
<?xml version="1.0" encoding="UTF-8" ?>
JDBCDemo文件:
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis_demo", "root", "1003");
//3.获取Statement对象
PreparedStatement preparedStatement = connection.prepareStatement("select * from user WHERE id = ?");
preparedStatement.setInt(1, 1);
//4.执行SQL语句返回结果集
ResultSet resultSet = preparedStatement.executeQuery();
//5.遍历结果集
while (resultSet.next()) {
System.out.println("username: " + resultSet.getString("username"));
System.out.println("age: " + resultSet.getString("age"));
}
//6.释放资源
resultSet.close();
preparedStatement.close();
connection.close();
}
}
StartWithXml文件:
@SuppressWarnings({"SqlResolve", "SqlNoDataSourceInspection", "Duplicates"})
public class StartWithXml {
public static void main(String[] args) throws SQLException, IOException {
//读取配置文件
InputStream configuration = Resources.getResourceAsStream("mybatis-config.xml");
// 得到 SqlSessionFactory 核心类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
// 开始一个 sql 会话
SqlSession session = sqlSessionFactory.openSession();
// 得到 sql 连接并运行 sql 语句
PreparedStatement preStatement = session
.getConnection()
.prepareStatement("SELECT * FROM user WHERE id = ?");
preStatement.setInt(1, 1);
ResultSet result = preStatement.executeQuery();
// 验证结果
while (result.next()) {
System.out.println("username: " + result.getString("username"));
System.out.println("age: " + result.getString("age"));
}
// 关闭会话
session.close();
}
}
UserTest文件:
@SuppressWarnings({"Duplicates"})
public class UserTest {
public static void main(String[] args) throws SQLException, IOException {
//读取配置文件
InputStream configuration = Resources.getResourceAsStream("mybatis-config.xml");
// 得到 SqlSessionFactory 核心类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
// 开始一个 sql 会话
SqlSession session = sqlSessionFactory.openSession();
// 得到 mapper
UserMapper mapper = session.getMapper(UserMapper.class);
// 调用注解的SQL
String username = mapper.selectUsernameById(1);
System.out.println("username: " + username);
// 调用xml的SQL
Integer age = mapper.selectUserAgeById(1);
System.out.println("age: " + age);
// 调用xml的SQL来获取对象
User user = mapper.selectUserById(1);
System.out.println(user);
// 关闭会话
session.close();
}
}
测试运行结果文件:

该博客展示了如何在 Java 中使用 MyBatis 框架进行数据库操作,包括配置 `pom.xml` 文件引入 MySQL 和 MyBatis 依赖,创建 `User` 类,定义 `UserMapper` 接口及其 XML 映射文件,以及配置 `mybatis-config.xml`。同时,给出了使用 JDBC 连接 MySQL 数据库的示例代码,演示了如何查询数据并打印结果。在 MyBatis 示例中,通过 `UserMapper` 查询了用户名、年龄和完整的用户对象。

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



