Mybatis-使用代码建立连接

主类: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'}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值