Mybatis之第一个程序
程序员文章站
2022-06-19 14:58:23
今天开始学习mybatis的知识,编写一个小程序,实现的是查询数据库。1、首先导入依赖 mysql mysql-connector-java
今天开始学习mybatis的知识,编写一个小程序,实现的是查询数据库。
1、首先导入依赖
<!--导入依赖-->
<dependencies>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
2、编写mybatis-config.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">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/lizheng/dao/UserMapper.xml"/>
</mappers>
</configuration>
3、编写工具类MybatisUtils,实现获得sqlSession 对象功能
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
// 你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
4、编写实体类User,与数据库绑定
package com.lizheng.pojo;
/**
* User实体类:数据库连接对象
* @author 李争
*/
public class User {
private int id ;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
5、创建UserMapper,操作数据库对象实体
import com.lizheng.pojo.User;
import java.util.List;
/**
* 操作对象数据库的实体
*/
public interface UserMapper {
//只写方法名,不写方法体,花括号也不行
public List<User> getUserList();
}
6、编写UserMapper.xml 执行SQL语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace对应全路径下的mapper-->
<mapper namespace="com.lizheng.dao.UserMapper">
<!-- id 对应 重写方法名称-->
<!-- resultType 对应返回类型,list 只写范型-->
<select id="getUserList" resultType="com.lizheng.pojo.User">
select * from mybatis.user
</select>
</mapper>
7、编写测试类UserMapperTest
import java.util.List;
//测试
public class UserMapperTest {
@Test
public void test(){
//第一步:获取sqlsession对象
SqlSession session = MybatisUtils.getSqlSession();
//执行SQL语句
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭sqlSession
session.close();
}
}
8、运行结果
注意点:
1、 mybatis-config.xml中要又mapper标签,每一个mapper绑定一个mapper.xml
2、pom.xml文件中设置资源过滤,防止出现资源导不出的问题
3、mapper.xml中namespace、id等要编写正确
本文地址:https://blog.csdn.net/qq_38723455/article/details/112212119