mybatis入门

1、加入依赖:

<!-- mybatis依赖 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.0</version>
    </dependency>
    
    <!-- MySQL数据库依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

 2、配置数据源:

<?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>
    <!-- 和spring整合后 environments配置将废除 -->
    <environments default="development">
        <environment id="development">
            <!-- 使用jdbc事务管理 -->
            <transactionManager type="JDBC" />
            <!-- 数据库连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url"
                          value="jdbc:mysql://localhost:3306/test ?characterEncoding=utf-8" />
                <property name="username" value="root" />
                <property name="password" value="111111" />
            </dataSource>
        </environment>
    </environments>
    <!-- 加载mapper.xml -->
     <mappers>
         <mapper resource="mapper/DemoMapper.xml"></mapper>
     </mappers>
</configuration>

 3、新建Mapper接口:

package com;
import java.util.List;
import java.util.Map;

public interface DemoMapper {
  public List<Map<String,Object>>  selectAll(Map<String,Object> map);
}

 4、创建一个User实体类:

package com;

public class User {
  private String name;
  private String password;
  private String address;
  private Integer id;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }
}

 5、新建Mapper配置文件:

<?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="com.DemoMapper">
  <select id="selectAll" parameterType="Map" resultType="Map">
    select * from user WHERE id =#{id}
  </select>
  <insert id="inserUser" parameterType="com.User">
      insert into user (id,name,password,address)
      values (#{id},#{name},#{password},#{address})
  </insert>
</mapper>

 6、上测试类:

package com;

import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.jdbc.Driver;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class Test {

  public static void main(String[] args) throws Exception {
    String resource = "mybatis.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    DemoMapper mapper = sqlSession.getMapper(DemoMapper.class);
    Map<String,Object> map = new HashMap<>();
    map.put("id","1");
    System.out.println(mapper.selectAll(map));
    sqlSession.close();
  }
}

 测试结果:

[{password=333333, address=北京, name=update后的name, id=1}]

 

测试insert:

public static void main(String[] args) throws Exception {
  String resource = "mybatis.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  DemoMapper mapper = sqlSession.getMapper(DemoMapper.class);
  User user = new User();
  user.setId(1);
  user.setName("123");
  user.setPassword("111111");
  user.setAddress("南京");
  mapper.inserUser(user);
  sqlSession.commit();//一定要commit,要不然数据库没数据
  sqlSession.close();
}

 

    原文作者:MyBatis
    原文地址: https://www.cnblogs.com/ychuang/p/11567798.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞