中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Mybatis如何通過Mapper代理連接數據庫

發布時間:2021-11-17 11:08:29 來源:億速云 閱讀:268 作者:小新 欄目:開發技術

這篇文章主要為大家展示了“Mybatis如何通過Mapper代理連接數據庫”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Mybatis如何通過Mapper代理連接數據庫”這篇文章吧。

1.在數據庫中創建表和相應字段,如下圖我創建了三個字段分別為fromname,message,toname,類型為varchar

Mybatis如何通過Mapper代理連接數據庫

2.創建對應的pojo實體類,注意類型要和數據庫創建類型一致,如varchar()對應的是java.lang.String 

Mybatis如何通過Mapper代理連接數據庫

 3.在resource路徑下配置config.xml,配置Mybatis的運行環境3306/后面加上自己的數據庫schema名字,數據庫username和password輸入自己的賬號和密碼,而在下方mapper則是用于注冊我們待會要寫的xml文件,路徑用/ 寫上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>
    <!--配置Mybatis運行環境-->
    <environments default="development"><!--default命名-->
        <environment id="development"><!--和上面的一致-->
            <transactionManager type="JDBC"></transactionManager><!--事務管理 交給JDBC-->
            <!-- 數據源 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/bjpower?
        useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="xxxx"/>
            </dataSource>
        </environment>
    </environments>
 
    <mappers>
<!--        <mapper resource="com/yyj/repository/msgdataRepository.xml"></mapper>-->
        <mapper resource="com/yyj/repository/msgdatanewRepository.xml"></mapper>
    </mappers>
</configuration>

4.自定義接口,在repository包下自定義接口,待會在xml中實現即可

Mybatis如何通過Mapper代理連接數據庫

5.在repository中建立對應的xml文件,如我取的名稱為MsgdatanewReposiotory,名字可自定義,注意mapper注冊和改名字一樣即可,然后編寫sql語句          

statement 的 id 為接?中對應的?法名

<?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="com.yyj.repository.MsgdatanewRepository"><!--接口全類名-->
    <insert id="save1" parameterType="com.yyj.pojo.MessageData">
        <!--parameterType為傳入的參數類型,是剛剛設置的pojo類-->
        insert into msgdatanew(fromname,message,toname) values (#{fromName},#{message},#{toName})
    </insert>
    
    <!--resultType為返回值類型-->
    <select id="findByName1" parameterType="java.lang.String"
            resultType="com.yyj.pojo.MessageData">
        select * from msgdatanew where toname = #{toName}
    </select>
</mapper>

6.調用測試類即可,注意要添加commit事務才能提交成功,數據庫才有變化

package com.yyj.Test;
 
import com.yyj.pojo.MessageData;
import com.yyj.repository.MsgdatanewRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
import java.io.InputStream;
 
public class Test2 {
//    public void add(String toName,String message,String fromName){
public static void main(String[] args) {
    InputStream inputStream = Test2.class.getClassLoader().getResourceAsStream("config.xml");
    SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    MsgdatanewRepository msgdatanewRepository = sqlSession.getMapper(MsgdatanewRepository.class);
    MessageData messageData=new MessageData();
    messageData.setMessage("nihaoyayay");
    messageData.setFromName("lisi");
    int save = msgdatanewRepository.save1(messageData);
    System.out.println(save);
    sqlSession.commit();
}
}

 7.效果展示 

Mybatis如何通過Mapper代理連接數據庫 

8.附上pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>websocket-chatroom</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>websocket-chatroom</name>
    <description>WebSocket示例工程</description>
 
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>
 
    <dependencies>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!--mysql驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
<!--            &lt;!&ndash; ehcache &ndash;&gt;-->
<!--            <dependency>-->
<!--                <groupId>net.sf.ehcache</groupId>-->
<!--                <artifactId>ehcache-core</artifactId>-->
<!--                <version>2.4.3</version>-->
<!--            </dependency> <dependency>-->
<!--            <groupId>org.mybatis</groupId>-->
<!--            <artifactId>mybatis-ehcache</artifactId>-->
<!--            <version>1.0.0</version>-->
<!--        </dependency>-->
        </dependencies>
 
 
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.yyj.WebsocketChatroomApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
 
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
 
 
</project>

以上是“Mybatis如何通過Mapper代理連接數據庫”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

专栏| 洮南市| 西安市| 修文县| 麻江县| 榆林市| 乌兰县| 南华县| 榆中县| 邛崃市| 阳谷县| 高碑店市| 五河县| 会宁县| 紫阳县| 崇仁县| 南投市| 桐柏县| 苗栗市| 神农架林区| 盐城市| 沙湾县| 乾安县| 登封市| 彩票| 永州市| 麻栗坡县| 正定县| 新乡市| 长宁县| 丰宁| 清涧县| 中卫市| 乐都县| 湾仔区| 门源| 涿鹿县| 金门县| 肃宁县| 庄浪县| 海南省|