直接使用maven搭建一个mybatis的运行环境

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

    <groupId>qinfeng.zheng</groupId>
    <artifactId>learn-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>

</project>

 

2. mybatis主配置文件: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="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://120.79.xx.yy:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="mapper/FemaleMapper.xml"/>
  </mappers>
</configuration>

3. 数据库实体类Female.java

import lombok.Data;

@Data
public class Female {
    private Integer id;
    private String name;
}

4. mybatis接口类 FemaleMapper.java

import qinfeng.zheng.entity.Female;

public interface FemaleMapper {

    Female getFemaleById(int id);

//    int addFemale(FemaleMapper female);

}

 

5. Mybatis接口对应的xml文件,  FemaleMapper.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="qinfeng.zheng.FemaleMapper">
    <select id="getFemaleById" resultType="qinfeng.zheng.entity.Female" parameterType="int">
      SELECT id,name FROM tb_female where id = #{id};
    </select>

</mapper>

 

至此,mybatis的运行环境,就算搭建成功了,下面写一段测试代码 

6. 测试代码

public class V1Test {
    public static void main(String[] args) {
        try (InputStream is = Resources.getResourceAsStream("mybatis-config.xml")) {
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            FemaleMapper femaleMapper = sqlSession.getMapper(FemaleMapper.class);
            Female female = femaleMapper.getFemaleById(1);
            System.out.println(female);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

 

运行代码 , 打印结果如下:

 

 

好,环境没问题,测试也成功了,下面可以开始阅读mybatis的源码了!

posted on 2019-11-17 20:22  显示账号  阅读(328)  评论(0编辑  收藏  举报