MyBatis1-HelloWorld

一.HelloWorld

1.1MyBatis操作数据库

1.1.1 创建数据库数据和bean

①.在本地创建test数据库和表格

CREATE TABLE tbl_employee(
  id INT(11) PRIMARY KEY AUTO_INCREMENT,
  last_name VARCHAR(255),
  gender CHAR(1),
  email VARCHAR(255)
)

②创建bean

package com.itlc.mybatis.HelloWorld.Old.bean;

public class Employee {
    private Integer id;
    //与数据库名称不匹配,使用时可以使用名称修改
    private String lastName;
    private String gender;
    private String email;

    public Integer getId() {
        return id;
    }

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

    public String getLastName() {
        return lastName;
    }

    public Employee setLastName(String lastName) {
        this.lastName = lastName;
        return this;
    }

    public String getGender() {
        return gender;
    }

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

    public String getEmail() {
        return email;
    }

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

    public Employee() { }

    @Override
    public String toString() {
        return "employee{" + "id=" + id + ", lastName='" + lastName + '\'' + ", gender='" + gender + '\'' + ", email='" + email + '\'' + '}';
    }

    public Employee(Integer id, String lastName, String gender, String email) {

        this.id = id;
        this.lastName = lastName;
        this.gender = gender;
        this.email = email;
    }
}
1.1.2 创建配置文件

①创建MyBatis全局配置文件
– MyBatis 的全局配置文件包含了影响 MyBatis 行为甚深
的设置(settings)和属性(properties)信息、如数据
库连接池信息等。指导着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/mybatis1" />
                <property name="username" value="root" />
                <property name="password" value="123321" />
            </dataSource>
        </environment>
    </environments>

    <!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
    <mappers>
        <mapper resource="EmployeeMapper.xml" />
    </mappers>
</configuration>

前面为基础信息,后面是绑定的sql映射文件

②创建SQL映射文件
– 映射文件的作用就相当于是定义Dao接口的实现类如何
工作。这也是我们使用MyBatis时编写的最多的文件。

<?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.itlc.mybatis.EmployeeMapper">
<!-- 
namespace:名称空间
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
 -->
    <select id="Employee" resultType="com.itlc.mybatis.HelloWorld.Old.bean.Employee">
        Select id,last_name lastName,email,gender FROM tbl_employee WHERE id=#{id}
    </select>
</mapper>
1.1.3 实际代码操作

使用步骤:
1.根据全局配置文件得到SqlSessionFactory;
2.使用sqlSession工厂,获取到sqlSession对象使用他来进行增删改查
一个sqlSession对应一次与数据库的会话,用完需要关闭
3.使用sql的唯一标志(namespace.id)来告诉MyBatis执行那个sql,sql都保存
在sql映射文件中

 @Test
    public void test() throws IOException {

        //1.根据全局配置文件得到SqlSessionFactory;
        String resource= "helloworld/old/mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);


        //2.获取sqlSession实例,能直接执行已经映射的sql语句
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            //参数为:唯一标志(namespace.id)和sql参数
            Employee employee= openSession.selectOne("com.itlc.mybatis.EmployeeMapper.getEmpById",1);
            System.out.println(employee);
        }finally {
            //必须关闭
            openSession.close();
        }
    }

1.2 接口式编程

相对于1.1的方法,修改的步骤

1.2.1 创建一个Dao接口
import com.itlc.mybatis.HelloWorld.New.bean.Employee;

public interface EmployeeMapper {
    public Employee getEmpById(Integer id);
}

Dao接口中返回一个需要查询得到的对象

1.2.2 修改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">
 <!--此处进行修改,namespace绑定到dao接口的位置-->
<mapper namespace="com.itlc.mybatis.HelloWorld.New.dao.EmployeeMapper">
<!-- 
namespace:名称空间;指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值

public Employee getEmpById(Integer id);
 -->
  <!--此处进行修改,id为接口下的对应返回对象的方法-->
    <select id="getEmpById" resultType="com.itlc.mybatis.HelloWorld.New.bean.Employee">
        Select id,last_name lastName,email,gender FROM tbl_employee WHERE id=#{id}
    </select>
</mapper>

注:全局配置文件不需要进行修改

1.2.3 实际代码操作
 @Test
   public class MyBatisTest {

    @Test
    public void test() throws IOException {
        //1.获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("helloworld/new/mybatis-config.xml"));

        //2.获取sqlSession对象
        SqlSession openSession=sqlSessionFactory.openSession();

        try{
            //3.获得接口的实现类对象
            //会为接口自动的创建一个代理对象,代理对象去执行增删改查方法
            EmployeeMapper mapper=openSession.getMapper(EmployeeMapper.class);

            Employee employee=mapper.getEmpById(1);
            System.out.println(employee);
        }finally {
            openSession.close();
        }
    }

1.接口式编程
原生: Dao ===》 DaoImpl
MyBatis Mapper ===》 xxMapper.xml
2.SqlSession代表和数据库的一次对话,用完必须关闭
3.SqlSession和connection一样都是非线程安全的,每次使用都应该去获取新的对象。
4.mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象(将接口和xml进行绑定)
EmployeeMapper mapper=openSession.getMapper(EmployeeMapper.class);
5.两个重要得多配置文件
1)mybatis配置文件(mybatis-config.xml)
包含数据库连接池信息,事务管理器信息等那个,系统运行环境信息
2)sql映射文件(EmployeeMapper.xml):
保存了每一个sql语句的映射信息,将sql抽取出来

猜你喜欢

转载自blog.csdn.net/maniacxx/article/details/79930349