IDEA initial use MyBatis

Learning Link: https://www.bilibili.com/video/BV1gs411j7kA?p=2

1. Establish java file

2. Load class path. mybatis.jar and database driver jar

File-> Project Structure-> Modules-> Dependencies- > plus right -> 1.jar
3. establishing a table id name age person in the database
to establish a person class in the src
4. By the configuration files and the class association table, in which the class where the person
to create a file personMapper.xml
1) where there is no path personMapper.xml .xml
return type then the type of the id value passed parameters which need
5. built directly below src conf.xml file

Here we need to use new xml file: Reference links  https://blog.csdn.net/nba_linshuhao/article/details/82735770?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant. none-task-blog-BlogCommendFromBaidu- 1

Inside the url username password mapper mapping the file path which is
here necessary to import a database link incoming packet
4. test class file TestMyBatis
loading conf.xml file to access the database profile

Then proceed to visit the id

testMyBatis
package org.student.entity;

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.Reader;

public class testMyBatis {
    public static void main(String[] args) throws IOException {
        //加载MyBatis配置文件(为了访问数据库)
        Reader reader = Resources.getResourceAsReader("conf.xml") ;
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader) ;
        //session - connection


        SqlSession session = sessionFactory.openSession() ;
        String statement = "org.student.entity.personMapper.queryPersonById" ;
        Person person = session.selectOne( statement,4 ) ;
        System.out.println(person);
        session.close();

    }
}
配置文件
conf.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.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:MySQL://localhost:3306/hotel?&amp;useSSL=false&amp;serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- 加载映射文件 -->
        <mapper resource="org/student/entity/personMapper.xml"/>
    </mappers>
</configuration>
personMapper.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="org.student.entity.personMapper">
    <select id="queryPersonById" resultType="org.student.entity.Person"  parameterType="int">
        select * from person where  id = #{id} 
    </select>
</mapper>
person类
package org.student.entity;

public class Person {
    private int id;
    private String name;
    private int age;
    public Person() {

    }
    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

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

    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;
    }
    @Override
    public String toString() {
        return this.id+","+this.name+","+this.age ;
    }

}

 

Guess you like

Origin www.cnblogs.com/ejwbytshooting/p/12638178.html