初学者使用MyBatis开发步骤详解

MyBatis开发步骤

1.建表

create table t_users(

id int primary key auto_increment ,
name varchar(50) ,
password varchar(50) ,
sex varchar(1) ,
birthday datetime ,
registerTime datetime ,

) default charset = utf-8

2.定义实体类

定义所需CURD操作的实体类


import java.util.Date;

public class User {
    private  Integer uid;
    private String uname;
    private String pwd;
    private Boolean sex;
    private Date birth;

    public User() {
    }

    public User(Integer uid, String uname, String pwd, Boolean sex, Date birth) {
        this.uid = uid;
        this.uname = uname;
        this.pwd = pwd;
        this.sex = sex;
        this.birth = birth;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public Boolean getSex() {
        return sex;
    }

    public void setSex(Boolean sex) {
        this.sex = sex;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }


    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", pwd='" + pwd + '\'' +
                ", sex=" + sex +
                ", birth=" + birth +
                '}';
    }
}

3.定义DAO接口

import com.qf.entity.User;
import org.apache.ibatis.annotations.Param;

import java.util.Map;

public interface UserDao {
    User queryUserById(Integer uid);
    User queryUserByIdAndUsername (Integer id , String username);
  
}

4.编写Mapper.xml

在线resource目录中创建Mapper.xml文件

<mapper namespace="com.qf.Dao.UserDao">


    <select id="queryUserById" resultType="USER">
        select uid,uname,pwd,sex,age,birth from t_user where uid=#{arg0}
    </select>

    <select id="queryUserByIdAndUsername" resultType="User">
        select uid,uname,pwd,sex,age,birth
        from t_user
        where uid=#{arg0} and uname=#{arg1}
    </select>
</mapper>

5.注册Mapper

将Mapper.xml注册到mybatis-config.xml中去
<!--Mapper文件注册位置-->
<mappers>
    
    <!--注册Mapper文件-->
    <mapper resource="USerDaoMapper.xml"/>
</mappers>

6.测试


import com.qf.Dao.UserDao;
import com.qf.entity.User;
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.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TestMyBayis {
    public static void main(String[] args) throws  IOException {
        //1.加载配置文件
        InputStream inputStream = Resources.getResourceAsStream("Mybatis-config.xml");
        //2.构建 sqlSessionFactory
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        //3.通过sqlSerssionFactory  创建sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.通过SqlSession 获得 DAO实现类的对象
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //5.测试查询方法
        User user1 = mapper.queryUserById(2);
        User user2 = mapper.queryUserByIdAndUsername(1, "admin");
        System.out.println(user1);
          System.out.println(user2);

    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值