关于mybatis:整合Mybatis

46次阅读

共计 3213 个字符,预计需要花费 9 分钟才能阅读完成。

步骤:
1. 导入相干 jar 包

  • junit
  • mybatis
  • mysql 数据库
  • spring 相干的
  • aop 织入
  • mybatis-spring

1、新建一个 maven 我的项目

<dependencies>
    <!-- 单元测试包 -->
    <denpendency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    
    <!--mysql-->
    <denpendency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connerctor-java</artifactId>
        <version>5.1.47</version>
    </dependency>

    <!--mybatis-->
    <denpendency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
    
    <!--spring-->
    <denpendency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
    
    <!--Spring 操作数据库的话,还须要一个 spring-jdbc-->
    <denpendency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
    
    <!--aop 织入 -->
    <denpendency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>


    <!--mybatis-spring 整合 -->
    <denpendency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.2</version>
    </dependency>
    
</dependencies>
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

2. 编写配置文件

3. 测试

1.1、回顾 Mybatis

1. 编写实体类

package com.jialidun.pojo;

import lombok .Data;

@Data
public class User{
    private int id;
    private String name;
    private String pwd;
}

2. 编写外围配置文件 mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration 外围配置文件 -->
<configuration>
    <!-- 别名 -->
    <typeAliases>
        <package name="com.jialidun.pojo"/>
    </typeAliases>
    <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=Asia/Shanghai"/>
              <property name="username" value="root"/>
              <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>


    <mappers>
        <mapper class="com.jialidun.mapper.UserMapper"/>
    </mappers>
</configuration>

3. 编写接口

package com.jialidun.mapper;

public interface UserMapper{public List<User> selectUser();

}

4. 编写 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">
<!--configuration 外围配置文件 -->
<mapper namespace="com.jialidun.mapper.UserMapper">
    <select id="selectUser" resultType="user">
        select * from mybatis.user;
    </select>
</mapper>

5. 测试

public class TestDemo{
    @Test
    public void test01{
        String resources="mybatis-config.xml";
        InputStream in = Resources.getResourceAsStream(resources);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.selectUser();

        for(User user:userList){System.out.println(user);
        }
    }

}

正文完
 0