首页 > 其他分享 >mybatis环境搭建

mybatis环境搭建

时间:2023-04-17 22:16:17浏览次数:28  
标签:xml UserMapper 环境 org mybatis import public 搭建

1.使用工具

IDEA,MySQL数据库,maven

2.开始搭建

2.1数据表的创建

CREATE TABLE `user`(
`id` INT(20) NOT NULL PRIMARY KEY,
`name` VARCHAR(30) DEFAULT NULL,
`password` VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;

点击并拖拽以移动

2.2依赖的导入

需要导入的依赖有

  <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.31</version>
        </dependency>
    </dependencies>

点击并拖拽以移动

2.3核心配置文件的编写(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>
    <properties resource="db.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=GMT"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

点击并拖拽以移动

2.4对应实体类的创建

public class User {
    private int id;
    private String name;
    private String password;
//此处省略get和set方法以及构造方法和toString方法
}

点击并拖拽以移动

这里有一个注意点就是对象的属性名和数据库的列名要保持一致。

2.5工具类的编写

package com.fan.utils;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {
    public static SqlSessionFactory sqlSessionFactory;

    static {

        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession(){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        return sqlSession;
    }


}

点击并拖拽以移动

2.6Dao层代码的编写

Dao层的接口(UserMapper.java):

package com.fan.dao;

import com.fan.pojo.User;

import java.util.List;

public interface UserMapper {
    //查询所有用户
    List<User> getUserList();
}

点击并拖拽以移动

Dao层的xml文件(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">
<mapper namespace="com.fan.dao.UserMapper">
    <select id="getUserList" resultType="com.fan.pojo.User">
        select * from mybatis.user;
    </select>
</mapper>

点击并拖拽以移动

在核心配置文件中注册mapper

<mappers>
    <mapper resource="com/fan/dao/UserMapper.xml"/>
</mappers>

点击并拖拽以移动

解决静态资源导出问题:在pom.xml文件中加入以下代码:

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

点击并拖拽以移动

2.7测试

    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }


>  以上就是mybatis的环境搭建的整个过程了。

标签:xml,UserMapper,环境,org,mybatis,import,public,搭建
From: https://www.cnblogs.com/fan129/p/17327679.html

相关文章

  • Mybatis的"#"符号和"$"符号
    #是一个占位符,$是拼接符。 #{}和${}的区别是什么?1. #{} 是预编译处理,${}是字符串替换。2. Mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值;3. Mybatis在处理${}时,就是把${}替换成变量的值。4. 使用#{}可以有效的防止SQL注入,提高系统安全......
  • 基于空间矢量控制的永磁同步电机状态反馈控制转速系统设计及仿真,仿真平台基于MATLAB S
    基于空间矢量控制的永磁同步电机状态反馈控制转速系统设计及仿真,仿真平台基于MATLABSimulink搭建。联系默认发仿真系统文件。另外包含设计文档,高清仿真结果示意图,出图程序设计文档包括,建模、各部分仿真模块设计,控制算法详解ID:85200676106734132......
  • 基于MPC的三相变流器设计及仿真,仿真平台基于MATLAB Simulink搭建
    基于MPC的三相变流器设计及仿真,仿真平台基于MATLABSimulink搭建。内含仿真文件,源代码,设计文档,仿真图。设计文档包括建模,各部分仿真模块设计,控制算法详解。ID:65335674963764486......
  • 在linux系统下搭建STM32单片机开发环境
    在linux系统下搭建STM32单片机开发环境的记录目录在linux系统下搭建STM32单片机开发环境的记录前言准备安装交叉编译工具链vscode的配置编码问题include问题关于其他报错处理makefile前言懒得说了,过后再补准备ubuntu系统,vscode,交叉编译工具链gcc-arm-none-eabi,还需要一个下载......
  • Mybatis学习总结(转载)
    前言众所周知,MyBatis是对JDBC进行封装而成的产品,所以,聊MyBatis源码之前我们得先了解JDBC。推荐:JDBC这个问题,问的小伙伴一脸懵逼JDBCJDBC案例:publicclassJdbcDemo{publicstaticfinalStringURL="jdbc:mysql://localhost:3306/mblog";publicstaticfinalSt......
  • 使用egg-socket.io 搭建socket 服务
    1.后台安装egg-socket.ionpmiegg-socket.io--save开启插件//{app_root}/config/plugin.jsexports.io={enable:true,package:'egg-socket.io',};配置//{app_root}/config/config.${env}.jsexports.io={init:{},//passedtoengine.io......
  • Mybatis源码深度解析(转载)
    我们从一个简单案例入手,接着就是一步一步的剥开Mybatis的源码,大量的图文结合。Mybatis使用案例添加mybatis和mysql相关pom依赖。<!--Mybatis依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</......
  • Centos7安装web环境
    1、安装Apache[root@localhost~]#yum-yinstallhttpd#开机自启动[root@localhost~]#chkconfighttpdon#启动httpd服务[root@localhost~]#servicehttpdstart现在直接在浏览器键入http://localhost 或http://本机IP ,应该会看到Apache的测试页面这里需要注意关闭防......
  • 通过github搭建简单的网站
    正常搭建个网站可能需要服务器和域名,但是有没有不花钱的方法呢,github可以。首先新建个工程,名称随意然后新建个index.html文件做为首页之后点击设定最下面有个GitHubPages选择选择下面的内容,你的网站地址就会生成出来了打开网址https://huyunan.github.io/haha/ ......
  • vc环境对应vs版本
    https://learn.microsoft.com/zh-cn/cpp/windows/latest-supported-vc-redist?view=msvc-170留待后查,同时方便他人......