MyBatis

MyBatis 持久层开发

MyBatis 配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

MyBatis 导入项目

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>x.x.x</version>
</dependency>

MyBatis 加载配置

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

MyBatis 基本结构

普通开发模式

//数据库实体类
public class User {
    private Integer id;
    private String username;
    private String password;
    private String gender;
  	private String address;

    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 getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
<!--mapper sql语句配置-->
<mapper namespace="test">
  <select id="selectAll" resultType="User">
    select * from user;
  </select>
</mapper>
//MyBatis使用
public class MyBatisDemo {
  public static void main(String[] args) {
    //加载MyBatisd的核心配置文件
    String resource = "org/mybatis/example/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
    //获取数据库操作对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    
    //执行查询的sql语句
    List<User> users = sqlSession.selectList("test.seletcAll");
    
    //释放连接
    sqlSession.close();
  }
}

代理开发模式

//mapper代理接口
public interface UserMapper {
  List<User> selectAll();
}
//MyBatis使用
public class MyBatisDemo {
  public static void main(String[] args) {
    //加载MyBatisd的核心配置文件
    String resource = "org/mybatis/example/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
    //获取数据库操作对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    
    //执行查询的sql语句
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List<User> users = userMapper.selectAll();
    
    //释放连接
    sqlSession.close();
  }
}

MyBatis 注解开发

@Select:查询注解(将sql语句写入注解中使用即可)
@Insert:插入注解(简单的数据库操作功能建议使用注解方式实现)
@Update:修改注解(复杂的数据库操作功能建议使用配置文件方式实现)
@Delete:删除注解
posted @ 2022-09-08 01:06  (应鑫)  阅读(32)  评论(0)    收藏  举报