美文网首页
MyBatis —— HelloWold

MyBatis —— HelloWold

作者: 桑鱼nicoo | 来源:发表于2020-03-11 21:11 被阅读0次

    1、先准备例子中的表结构

    create table `tbl_employee`( 
        `id` int(11) primary key auto_increment, 
        `last_name` varchar(255) not null ,
        `gender` char(1) not null,
        `email` varchar(255) not null
    );
    

    2、创建maven+spring+springmvc项目,pom文件中需要注入mybatis-spring的依赖,mybatis-spring2.0与Spring 框架5.0兼容

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.4</version>
    </dependency>
    
     <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.5.4</version>
    </dependency>
    

    3、创建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>
        <!-- mybatis 可以使用properties来引入外部peoperties配置文件的内容
            resource:引入类路径下的资源
            url:除此之外还可以使用url引入网络路径或者磁盘下的资源-->
        <properties resource="dbconfig.properties"/>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url" value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                </dataSource>
            </environment>
        </environments>
        <!-- 将EmployeeMapper.xml注册到全局配置文件中-->
        <mappers>
            <mapper resource="mapper/EmployeeMapper.xml"/>
        </mappers>
    </configuration>
    

    4、创建数据源文件

    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&useSSL=false
    jdbc.username = root
    jdbc.password = abcd123456
    

    5、创建javabean

    package com.sangyu.bean;
    
    public class Employee {
        private Integer id;
        private String lastName;
        private String email;
        private String gender;
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getEmail() {
            return email;
        }
    
        public Integer getId() {
            return id;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public String getGender() {
            return gender;
        }
    
        @Override
        public String toString() {
            return "Employee{" +
                    "id=" + id +
                    ", lastName='" + lastName + '\'' +
                    ", email='" + email + '\'' +
                    ", gender='" + gender + '\'' +
                    '}';
        }
    
        public Employee(Integer id,String lastName,String email,String gender){
            this.email = email;
            this.lastName = lastName;
            this.id = id;
            this.gender = gender;
        }
        public Employee(){
        }
    }
    

    6、创建 mapper 接口

    public interface EmployeeMapper {
        Employee getEmpId(int id);
    }
    
    

    7、创建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">
    <!--namespace:因为没有对应mapper这里的名字可以任意起-->
    <mapper namespace="com.sangyu.mapper.EmployeeMapper">
        <!--
        id:唯一标识
        resultType:返回值类型,对应的Bean类型
        {#id}:从传递过来的参数中取出id值-->
        <select id="getEmpId" resultType="com.sangyu.bean.Employee">
        select * from tbl_employee where id = #{id}
      </select>
    </mapper>
    

    8、创建test

    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); // 根据xml创建sqlSessionFactory
            SqlSession session = sqlSessionFactory.openSession(); // 2. 从 SqlSessionFactory 中获取 SqlSession 的实例,SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
            Employee employee = session.selectOne("com.sangyu.mapper.EmployeeMapper.getEmpId",1); // selectOne需要传递2个参数,第一个参数是mapper.xml中的namespace+select中的id,第二是需要sql中需要查询的参数
            System.out.println(employee);
        }
    }
    

    相关文章

      网友评论

          本文标题:MyBatis —— HelloWold

          本文链接:https://www.haomeiwen.com/subject/avxkjhtx.html