Mybatis通過Mapper代理連接數(shù)據(jù)庫的方法
1.在數(shù)據(jù)庫中創(chuàng)建表和相應(yīng)字段,如下圖我創(chuàng)建了三個字段分別為fromname,message,toname,類型為varchar

2.創(chuàng)建對應(yīng)的pojo實體類,注意類型要和數(shù)據(jù)庫創(chuàng)建類型一致,如varchar()對應(yīng)的是java.lang.String

3.在resource路徑下配置config.xml,配置Mybatis的運行環(huán)境3306/后面加上自己的數(shù)據(jù)庫schema名字,數(shù)據(jù)庫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運行環(huán)境-->
<environments default="development"><!--default命名-->
<environment id="development"><!--和上面的一致-->
<transactionManager type="JDBC"></transactionManager><!--事務(wù)管理 交給JDBC-->
<!-- 數(shù)據(jù)源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bjpower?
useUnicode=true&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中實現(xiàn)即可

5.在repository中建立對應(yīng)的xml文件,如我取的名稱為MsgdatanewReposiotory,名字可自定義,注意mapper注冊和改名字一樣即可,然后編寫sql語句
statement 的 id 為接⼝中對應(yīng)的⽅法名
<?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為傳入的參數(shù)類型,是剛剛設(shè)置的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.調(diào)用測試類即可,注意要添加commit事務(wù)才能提交成功,數(shù)據(jù)庫才有變化
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.效果展示
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驅(qū)動-->
<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>
<!-- <!– ehcache –>-->
<!-- <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>
到此這篇關(guān)于Mybatis通過Mapper代理連接數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)Mybatis Mapper代理連接數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea2020.1.3 手把手教你創(chuàng)建web項目的方法步驟
這篇文章主要介紹了idea 2020.1.3 手把手教你創(chuàng)建web項目的方法步驟,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java Fluent Mybatis 項目工程化與常規(guī)操作詳解流程篇 下
Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國內(nèi)又以Mybatis用的多,基于mybatis上的增強框架,又有mybatis plus和TK mybatis等。今天我們介紹一個新的mybatis增強框架 fluent mybatis關(guān)于項目工程化與常規(guī)操作流程2021-10-10
springboot使用國產(chǎn)加密算法方式,sm2和sm3加解密demo
這篇文章主要介紹了springboot使用國產(chǎn)加密算法方式,sm2和sm3加解密demo,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Mybatis Mapper XML文件-插入,更新,刪除詳解(insert, updat
這篇文章主要介紹了MyBatis的Mapper XML文件中用于插入、更新和刪除數(shù)據(jù)的語句,包括這些語句的屬性和子元素的使用方法2025-02-02
Android中比較常見的Java super關(guān)鍵字
這篇文章主要為大家介紹了Android中比較常見的Java super關(guān)鍵字,具有一定的學(xué)習(xí)參考價值,感興趣的小伙伴們可以參考一下2016-01-01
Spring?Cloud微服務(wù)架構(gòu)Sentinel數(shù)據(jù)雙向同步
這篇文章主要為大家介紹了Spring?Cloud微服務(wù)架構(gòu)Sentinel數(shù)據(jù)雙向同步示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Java數(shù)據(jù)結(jié)構(gòu)之散列表詳解
散列表(Hash table,也叫哈希表),是根據(jù)關(guān)鍵碼值(Key value)而直接進行訪問的數(shù)據(jù)結(jié)構(gòu)。本文將為大家具體介紹一下散列表的原理及其代碼實現(xiàn)2022-01-01

