1. 程式人生 > >Mybatis HelloWorld程式(二)

Mybatis HelloWorld程式(二)

HelloWorld程式流程如下:

一、準備工作

(1)建立一張測試表 

表結構

表資料

 

(2)建立對應的javaBean 

package atguigu.mybatis;

public class Employee {

    private String name;
    private int age;
    private String email;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                '}';
    }
}

二、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="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/test" />
                <property name="username" value="root" />
                <property name="password" value="mysql" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="Mapper/EmployeeMapper.xml"/>
    </mappers>
</configuration>

 

<?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="atguigu.mybatis.Mapper.EmployeeMapper">
    <select id="getEmployeeById" resultType="atguigu.mybatis.Employee">
      select * from employee where id = #{id}
  </select>
</mapper>

 三、建立EmployeeMapper介面類

package atguigu.mybatis.Mapper;

import atguigu.mybatis.Employee;

public interface EmployeeMapper {

    public Employee getEmployeeById(Integer id);
}

四、測試

@Test
public void testSetUser() throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    // 1、獲取sqlSession例項,能直接執行已經對映的sql語句
    // sql的唯一標識:statement Unique identifier matching the statement to use.
    // 執行sql要用的引數:parameter A parameter object to pass to the statement.    
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    // 2、獲取sqlSession物件
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        // 3、獲取介面的實現類物件
        //會為介面自動的建立一個代理物件,代理物件去執行增刪改查方法
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

        Employee employeeById = mapper.getEmployeeById(1);
        System.out.println(employeeById);


    }finally {
        sqlSession.close();
    }


} 

五、介面式程式設計總結