Mybatis总结

Mybatis的使用
1.添加mybatis配置文件conf.xml

<?xml version="1.0" encoding="UTF-8"?> 2.定义表对应的实体类 package cn.easybuy.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable{

private Integer id;
private String loginName;//登录名称
private String userName;//用户名
private String password;//密码
private Integer sex;//性别
private String identityCode;
private String email;//电子邮箱
private String mobile;//电话
private Integer type;//用户类别
public Integer getId() {
	return id;
}
public void setId(Integer id) {
	this.id = id;
}
public String getLoginName() {
	return loginName;
}
public void setLoginName(String loginName) {
	this.loginName = loginName;
}
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 Integer getSex() {
	return sex;
}
public void setSex(Integer sex) {
	this.sex = sex;
}
public String getIdentityCode() {
	return identityCode;
}
public void setIdentityCode(String identityCode) {
	this.identityCode = identityCode;
}
public String getEmail() {
	return email;
}
public void setEmail(String email) {
	this.email = email;
}
public String getMobile() {
	return mobile;
}
public void setMobile(String mobile) {
	this.mobile = mobile;
}
public Integer getType() {
	return type;
}
public void setType(Integer type) {
	this.type = type;
}

}
3.定义操作映射文件userMapper.xml

<?xml version="1.0" encoding="UTF-8"?> id, loginName, userName, password, sex, identityCode, email, mobile, type select from easybuy_user where id = #{id,jdbcType=INTEGER} 4.编写测试类 package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

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 entity.User;

public class Test {

public static void main(String[] args) throws IOException {
    // 使用类加载器加载conf.xml文件
    InputStream is= Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
    // 创建SqlSessionFactory
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);

    /*
     * 使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) Reader reader =
     * Resources.getResourceAsReader("mybatis-config.xml"); SqlSessionFactory
     * sessionFactory = new SqlSessionFactoryBuilder().build(reader);
     */
    SqlSession session = sessionFactory.openSession();
    User user = session.selectOne("mapper.userMapper.getUser", 1);
    System.out.println(user);
}

}
Mybatis优点:
MyBatis 简单易学灵活;sql与代码解耦;可以修改SQL;
Mybatis缺点:
SQL编写复杂;SQL语句依赖对应版本数据库,不利于移植;框架还是比较简陋,底层数据库查询实际还是要自己写的,工作量也比较大,而且不太容易适应快速数据库修改;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值