码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis_HelloWord

时间:2018-01-06 21:07:59      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:nal   puts   host   空间   构建   enc   cal   实体类   class   

1.项目结构

技术分享图片

2.java类代码

①Employee.java

package com.atguigu.mybatis.bean;

public class Employee {

    private Integer id;
    private String lastName;
    private String email;
    private String gender;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]";
    }
    
}

②MybatisTest.java

package com.atguigu.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.atguigu.mybatis.bean.Employee;

public class MybatisTest {
    
    
    
    @Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        SqlSession openSession =sqlSessionFactory.openSession();
        try {
            Employee employee= openSession.selectOne("com.atguigu.mybatis.EmployeeMapper.selectEmp", 1);
            System.out.println(employee);
        } finally {
            openSession.close();
        }
        
        
    }

}
a.resource: mybatis的配置文件。

 b.使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)

 c.构建sqlSession的工厂

    d.创建sqlSession对象

    e.com.atguigu.mybatis.EmployeeMapper是EmployeeMapper.xml文件中mapper标签的namespace属性的值,selectEmp是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL

3.配置文件

①EmployeeMapper.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">
<mapper namespace="com.atguigu.mybatis.EmployeeMapper">
<!-- 
namespace:名称空间
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值 
-->
  <select id="selectEmp" resultType="com.atguigu.mybatis.bean.Employee">
    select id,last_name lastName,email,gender from tbl_employee where id = #{id}
  </select>
</mapper>

把实体类放在配置文件中,并完成实体类与sql(方法)映射

注:数据库字段与实体类字段 一 一 对应

②mybatis-config.xml

<?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/mybatis" />
                <property name="username" value="root" />
                <property name="password" value="111111" />
            </dataSource>
        </environment>
    </environments>
    
       <mappers>
           <!-- 注册EmployeeMapper.xml文件, -->
        <mapper resource="EmployeeMapper.xml"/>
       </mappers> 
   
</configuration>

配置数据库连接,包括事物管理,并完成实体类映射文件(EmployeeMapper.xml)注册

 

Mybatis_HelloWord

标签:nal   puts   host   空间   构建   enc   cal   实体类   class   

原文地址:https://www.cnblogs.com/2016024291-/p/8215129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!