1. 程式人生 > 其它 >mybatis的核心用法詳解 (新手自學開車)

mybatis的核心用法詳解 (新手自學開車)

1. 建立專案 (一般是建立的web專案, 因為實際開發中, web專案佔九成九)

2. 既然要連結資料庫, 肯定少不了導包 ( 記得把lib 新增為庫 )

  mysql-connector-java

  mybatis

  log4j

3. 新增mybatis的配置檔案, 我是直接放在src下

  注意改成自己的 propertty 裡面的連結項 mapper 裡面的對映地址

<?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.cj.jdbc.Driver"/> <
property name="url" value="jdbc:mysql://localhost:3306/fmg?serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="24118028"/> </dataSource> </environment> </environments> <mappers
> <mapper resource="com/fmg/mapper/studentMapper.xml"/> </mappers> </configuration>

4. 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">
<mapper namespace="test1">
    <select id="selectById" parameterType="java.lang.String" resultType="com.fmg.domain.Student">
        select id, name, age from tbl_student where id = #{id}
    </select>
</mapper>

5. log4j的配置檔案

# Global logging configuration
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

6. 實體類

package com.fmg.domain;

public class Student {
    private String id;
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

7. 測試檔案

package com.fmg.test;

import com.fmg.domain.Student;
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 java.io.IOException;
import java.io.InputStream;

public class TestMain1 {
    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();
        Student stu = session.selectOne("test1.selectById", "A0001");
        System.out.println(stu);
    }
}

8. 專案結構

10. 執行結果

本想把生活活成一首詩, 時而優雅 , 時而豪放 , 結果活成了一首歌 , 時而不靠譜 , 時而不著調