欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis通过Mapper代理连接数据库的方法

程序员文章站 2023-12-29 20:51:28
1.在数据库中创建表和相应字段,如下图我创建了三个字段分别为fromname,message,toname,类型为varchar2.创建对应的pojo实体类,注意类型要和数据库创建类型一致,如varc...

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代理连接数据库的文章就介绍到这了,更多相关mybatis mapper代理连接数据库内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

上一篇:

下一篇: