温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Mybatis如何通过Mapper代理连接数据库

发布时间:2021-11-17 11:08:29 来源:亿速云 阅读:270 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“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