mybatis学习之路(一)IDE中mybatis环境的搭建并显示数据库中一个表中的所有信息

①在IDE中创建Maven web项目

导入mybatis jar包

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

③在项目的resources目录下新建mybatis-config.xml配置文件

配置文件的内容可以参考mybatis源码包中的配置文件

mybatis源码包的下载地址:

https://github.com/mybatis/mybatis-3/releases

具体路径为:


配置数据库的连接信息:
<?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="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/javajdbctest"/>
<property name="username" value=""/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
</configuration>

④获取数据库最最核心的类SqlSession

public SqlSession getSqlSession() throws IOException {
//通过配置文件获取数据库连接信息
Reader reader = org.apache.ibatis.io.Resources.getResourceAsReader("mybatis-config.xml");
//通过配置信息构建一个SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//通过sqlSessionFactory打开一个数据库会话
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;}

⑤编写customer简单java类

public class Customer {
    private int id;
    private String name;
    private String address;
    private String phone;
    
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Customer() {
    }

    public Customer(int id, String name, String address, String phone) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

⑥数据库的中customer表

命名不是很规范(用于测试,忽略不计)


⑦建立数据库与Javabean关联的配置文件

同样可以参考在步骤③中的路径下的User.xml文件

新建CustomerMapper.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">
<!--namespace相当于包的含义-->
<mapper namespace="Customer">
    <!--type是customer类所在包的路径-->
    <resultMap type="algorithm.offer.pojo.Customer" id="CustomerResult">
        <id column="id" jdbcType="INTEGER" property="id"/><!--主键用id-->
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
    </resultMap>
    <!--id为此SQL语句的唯一标识,resultMap与resultMap相对应-->
    <select id="findCustomerList" parameterType="long" resultMap="CustomerResult">
        SELECT id,name,address,phone FROM customer
    </select>

</mapper>

⑧mybatis-config.xml与CustomerMapper.xml的关联

将以下代码写在mybatis-config.xml文件中:

<mappers>
        <mapper resource="MybatisMapper/CustomerMapper.xml"/>
    </mappers>

⑨编写DAO层Service层

DAO层

/**
 * Customer的DAO层
 */
public class CustomerDAO {
    DBAccess dbAccess = new DBAccess();

    /**
     * 查询所有Customer
     * @return
     */
    public List<Customer> findCustomerList(){
        List<Customer> customerList = new ArrayList<>();
        SqlSession sqlSession = null;
        try {
            sqlSession = dbAccess.getSqlSession();
            customerList = sqlSession.selectList("Customer.findCustomerList");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }

        return customerList;
    }
}

service层

public class CustomerService {
    /**
     * 查询所有消费者信息
     * @return
     */
    public List<Customer> showCustomer(){
        CustomerDAO customerDAO = new CustomerDAO();
        return customerDAO.findCustomerList();
    }


}

⑩测试

 @Test
    public void showCustomer() throws Exception {
        CustomerService customerService = new CustomerService();
        for (Customer customer:customerService.showCustomer()) {
            System.out.println(customer.toString());
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42228338/article/details/80382873