崇之他和她

导航

day031mybatis

mybaties

数据库映射框架 mybatis 包 mysql 包 junit测试包

全局配置文件 mybatis-config.xml 放在rescrous问件夹下

mapper 层接口 *mapper.class与对映配置文件名相同 *mapper.xml 并且每一个mapper.xml需要在全局配置文件中注册

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>
    <environments default="development1">
        <environment id="development1">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <!--<property name="driver" value="com.mysql.jdbc.Driver"/>-->
                <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true&amp;serverTimezone=UTC"/>
                <property name="username" value="dtht"/>
                <property name="password" value="55456"/>
            </dataSource>
        </environment>
        <environment id="development2">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mbt?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true"/>
                <property name="username" value="rrrr"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/mbt/mapper/MbtMapper.xml"/>
    </mappers>
</configuration>

mapper层java类

public interface MbtMapper {
    public List<Mbt> getMbt();

    public int insertMbt(Mbt mbt);
}

mapper对应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.mbt.mapper.MbtMapper">
    <select id="getMbt" resultType="com.mbt.bean.Mbt">
    select * from mbt
  </select>
    <insert id="insertMbt" parameterType="com.mbt.bean.Mbt">
        insert into mbt (name,pwd)values (#{name},#{pwd})
    </insert>
</mapper>

实体类bean

package com.mbt.bean;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 12:34 2021/2/19
 * @ Description:mbt表实体类
 * @ Modified By:
 * @Version: $version$
 */
public class Mbt {
    private int id;
    private String name;
    private String pwd;

    public Mbt(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }
    public Mbt( String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "mbt{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

工具类获取连接

package com.mbt.utils;

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

import java.io.InputStream;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 12:33 2021/2/19
 * @ Description:
 * @ Modified By:
 * @Version: $version$
 */
public class MybatiseUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try{
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }

}

测试类

package com.mbt.service;

import com.mbt.bean.Mbt;
import com.mbt.mapper.MbtMapper;
import com.mbt.utils.MybatiseUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 13:09 2021/2/19
 * @ Description:
 * @ Modified By:
 * @Version: $version$
 */
public class MbtServiceTest {
    @Test
    public void test(){
        SqlSession session =MybatiseUtil.getSqlSession();
        MbtMapper mm =session.getMapper(MbtMapper.class);
//        List<Mbt> li =mm.getMbt();
//        System.out.println(li.get(0).toString());

        int su = mm.insertMbt(new Mbt("ccc","sdfdf"));
        System.out.println(su);
        session.close();

    }
}

pom文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com</groupId>
  <artifactId>zhq</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--<packaging>jar</packaging>-->
  <!--<name>zhq</name>-->
  <!-- FIXME change it to the project's website -->
  <!--<url>http://www.example.com</url>-->

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.1</version>
    </dependency>
    <!--<dependency>-->
      <!--<groupId>MySQL</groupId>-->
      <!--<artifactId>mysql-connector-java</artifactId>-->
      <!--<version>5.1.6</version>-->
    <!--</dependency>-->

    <dependency>
      <groupId>MySQL</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.16</version>
    </dependency>

  </dependencies>

  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <!--<resource>-->
        <!--<directory>src/main/java</directory>-->
        <!--<includes>-->
          <!--<include>**/*.properties</include>-->
          <!--<include>**/*.xml</include>-->
        <!--</includes>-->
        <!--<filtering>false</filtering>-->
      <!--</resource>-->
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>
</project>

posted on 2021-02-19 18:44  崇之他和她  阅读(95)  评论(0编辑  收藏  举报