Mybatis連線資料庫

2023-02-26 21:01:20

從零開始Mybatis連線資料庫

建立Maven檔案

File-->new-->project-->maven,點選next

設定

在出現的pom.xml檔案中<project>標籤之間加入以下內容

 <!--打包方式-->
<packaging>jar</packaging>

    <dependencies>
        <!--載入Mybatis核心-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>

        <!--Junit測試-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!--mysql驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.3</version>
        </dependency>

    </dependencies>

在src-->main-->resources下建立檔案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/test"/>
                <property name="username" value="自己的使用者名稱"/>
                <property name="password" value="自己的密碼"/>
            </dataSource>
        </environment>
    </environments>

</configuration>


注意將username和password的value值改成自己的,且在自己的資料庫中建立test庫

建立實體類

在main的java下建立我們需要與表中對應的實體類,這裡我們寫user類


public class User {
    //實體類
    
    //屬性與表中的各項對應,加入相應的get,set方法
        int  userid;
        String username;
        String userpass;
        String usertel;
        int usercarad;

        public User() {
        }

        public User(int userid, String username, String userpass, String usertel, int usercarad) {
            this.userid = userid;
            this.username = username;
            this.userpass = userpass;
            this.usertel = usertel;
            this.usercarad = usercarad;
        }

        public int getUserid() {
            return userid;
        }

        public void setUserid(int userid) {
            this.userid = userid;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getUserpass() {
            return userpass;
        }

        public void setUserpass(String userpass) {
            this.userpass = userpass;
        }

        public String getUsertel() {
            return usertel;
        }

        public void setUsertel(String usertel) {
            this.usertel = usertel;
        }

        public int getUsercarad() {
            return usercarad;
        }

        public void setUsercarad(int usercarad) {
            this.usercarad = usercarad;
        }
    }

    


建立com.mapper包,並在包下建立UserMapper介面

這裡我們簡單做一個新增內容範例


package com.mapper;

public interface UserMapper {
    int insertUser();//返回值為改變的行數
}


建立UserMapper.xml對映檔案

在resources資料夾下建立mapper資料夾(包)並在其中建立UserMapper.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對應UserMapper介面全類名-->
<mapper namespace="com.mapper.UserMapper">

    <!--第二個一致,id對應方法名   int  insertUser(); -->
   <insert id="insertUser">
       insert into user values (null,"tom","1234","111",22)
   </insert>

</mapper>


在mybatis-config.xml下<configuration>標籤中最下面加入


<!--引入對映檔案-->
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>

建立測試類

建立Test類進行測試


import com.mapper.UserMapper;
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 Test {
    public static void main(String[] args) throws IOException {
        //獲取組態檔
       Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
       // 獲取SqlSessionFactoryBuilder獲取SqlSessionFactory工廠
       SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        //獲取SqlSessionFactory工廠
        SqlSessionFactory sqlSessionFactory= sqlSessionFactoryBuilder.build(reader);
        //利用工廠獲取sqlSession
        SqlSession sqlSession=sqlSessionFactory.openSession();//openSession()加入引數true,即可實現自動提交事務
        //利用sqlSession的方法獲取介面的物件
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        //呼叫介面方法,從而呼叫對映檔案的sql語句
       int num= userMapper.insertUser();
       //提交事務
        sqlSession.commit();
        System.out.println("改變行數"+num);


    }
}


執行結果: