IDEA creates the first MyBatis

I recently learned about MyBatis. I have gained some inspiration and encountered some difficulties in the process of learning. Posting this blog allows beginners to avoid detours and at the same time make their memories more profound.

1. First File→New→Project
Insert picture description here2. Next, click next→next→finish directly (you can start with any name)
Insert picture description here3 Create according to the directory (MyBatis only needs to import three packages, if you are a 5.0 database, you need to import a 5.0 database Package)
Insert picture description hereIf you are creating an xml file for the first time and there is no corresponding template in IDEA, you just need to click new→file and write .xml as the suffix
Insert picture description here

4.City

package sc.domain;

public class City {
    
    
    private int cid;
    private String cname;
    private int pid;

    public City() {
    
    
    }

    public City(int cid, String cname, int pid) {
    
    

        this.cid = cid;
        this.cname = cname;
        this.pid = pid;
    }

    public int getCid() {
    
    

        return cid;
    }

    public void setCid(int cid) {
    
    
        this.cid = cid;
    }

    public String getCname() {
    
    
        return cname;
    }

    public void setCname(String cname) {
    
    
        this.cname = cname;
    }

    public int getPid() {
    
    
        return pid;
    }

    public void setPid(int pid) {
    
    
        this.pid = pid;
    }
}

5.CityMapper

package sc.mapper;

import sc.domain.City;

import java.util.List;

public interface CityMapper {
    
    
    public List<City> findAllUser();
}

6CityMapper.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="sc.mapper.CityMapper">
    <select id="findAllUser"  resultType="sc.domain.City">
        select * from city
    </select>
</mapper>

7Test

package sc.Test;

import org.apache.ibatis.session.SqlSession;
import sc.domain.City;
import sc.mapper.CityMapper;
import sc.utils.MybatisUtil;

import java.util.List;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        SqlSession session =  MybatisUtil.getSession();
        List<City> list= session.getMapper(CityMapper.class).findAllUser();
        for (City city : list) {
    
    
            System.out.println(city.getCname());
        }
    }
}

8.MybatisUtil

package sc.utils;

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;

public class MybatisUtil {
    
    

    //加载mybatis配置文件
    //创建sqlsessionfactory对象
    //创建sqlsession连接对象
    //创建关闭sqlsession连接对象
    private static SqlSessionFactory sessionFactory;


    static{
    
    

        String resource="mybatis.xml";
        try {
    
    
            InputStream is=Resources.getResourceAsStream(resource);
            sessionFactory=new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    public static SqlSession getSession(){
    
    
        return sessionFactory.openSession();
    }


    public static void closeSession(SqlSession session){
    
    
        if(session!=null){
    
    
            session.close();
        }
    }




}


9.mybatis.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="sc">
    <environment id="sc">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
            <!--配置源文件-->
            <property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
            <!--接口大部分都是3306,(我这里是3308),如果你数据库是5.0或者以下的这不需要加characterEncoding = utf8&amp;&amp;useSSL=false&amp;serverTimezone=UTC-->
            <property name="url" value="jdbc:mysql://localhost:3308/db_shopping?characterEncoding = utf8&amp;&amp;useSSL=false&amp;serverTimezone=UTC"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
        </dataSource>
    </environment>
</environments>
    <mappers>
        <mapper resource="sc/mapper/CityMapper.xml"></mapper>
    </mappers>
</configuration>

10 database table
Insert picture description here
11 running results
Insert picture description here

Hope it will be useful for beginners.

Guess you like

Origin blog.csdn.net/s001125/article/details/114676985