建议自己写一下
实体类,用于封装数据库数据
package com.example.pojo;
import java.util.Date;
public class Emp {
private Integer id;
private String username;
private String password;
private String name;
private Integer gender;
private String image;
private Integer job;
private Date entrydate;
private Date create_time;
private Date update_time;
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Integer getJob() {
return job;
}
public void setJob(Integer job) {
this.job = job;
}
public Date getEntrydate() {
return entrydate;
}
public void setEntrydate(Date entrydate) {
this.entrydate = entrydate;
}
public Date getCreate_time() {
return create_time;
}
public void setCreate_time(Date create_time) {
this.create_time = create_time;
}
public Date getUpdate_time() {
return update_time;
}
public void setUpdate_time(Date update_time) {
this.update_time = update_time;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", gender=" + gender +
", image='" + image + '\'' +
", job=" + job +
", entrydate=" + entrydate +
", create_time=" + create_time +
", update_time=" + update_time +
'}';
}
}
Mapper接口类,定义对于数据库的操作
package com.example.Mapper;
import com.example.pojo.Emp;
import java.util.List;
public interface EmpMapper {
List<Emp> findAll();
}
Mapper接口类对应的xml配置文件,用于实行Mapper接口类中对于数据库的操作
<?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">
<mapper namespace="com.example.Mapper.EmpMapper">
<select id="findAll" resultType="com.example.pojo.Emp">
select *
from tb_emp;
</select>
</mapper>
MyBatis配置文件,定义和配置MyBatis框架的各种参数和属性,以及映射文件的路径和数据库连接信息。
<?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"/>
<property name="url" value="jdbc:mysql://localhost:3306/db02"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--指定需要扫描的Mapper接口的包路径-->
<mappers>
<package name="com.example.Mapper"/>
</mappers>
</configuration>
测试类代码,实现MyBatis功能
package com.example.Test;
import com.example.Mapper.EmpMapper;
import com.example.pojo.Emp;
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.InputStream;
import java.util.List;
public class MyBatisTest {
public static void main(String[] args) throws Exception {
InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
List<Emp> all = mapper.findAll();
for (Emp emp : all) {
System.out.println(emp);
}
}
}
运行结果如下