主类:MyBatisDemo
public class MyBatisDemo { public static void main(String[] args) { // 配置数据源 PooledDataSource dataSource = new PooledDataSource(); dataSource.setDriver("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/rogueq?serverTimezone=UTC"); dataSource.setUsername("root"); dataSource.setPassword("root"); // 创建事务处理器 TransactionFactory transactionFactory = new JdbcTransactionFactory(); // 创建环境对象 并命名为 development Environment environment = new Environment("development", transactionFactory, dataSource); // 构建Configuration对象 Configuration configuration = new Configuration(environment); // 注册一个mybatis上下文别名 configuration.getTypeAliasRegistry().registerAlias("user", User.class); // 加入一个映射器 configuration.addMapper(UserMapper.class); // 使用SqlSessionFactoryBuilder构建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); // 使用SqlSessionFactory创建会话 SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.selectById(10000L); System.out.println(user); } }
Mapper接口: UserMapper
import org.apache.ibatis.annotations.Param; public interface UserMapper { User selectById(@Param("id") Long id); }
映射文件: UserMapper.xml
<?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="cn.rogueq.mybatis.UserMapper"> <select id="selectById" resultType="user"> select * from user where id = #{id} </select> </mapper>
实体类:
package cn.rogueq.entity; import java.util.Date; public class User { private Long id; private String username; private String nickname; private String password; private String phone; private String email; private String identity; private Date createTime; private Date updateTime; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getIdentity() { return identity; } public void setIdentity(String identity) { this.identity = identity; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", phone='" + phone + '\'' + '}'; } }
执行结果:
User{id=10000, username='admin', phone='null'}