MyBatis_Environment Construction_hehe.employment.over.29.2

29.4 mybatis environment construction

29.4.1 Create a maven project

29.4.2 Import of pom.xml file coordinates in the project

<?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.xww</groupId>
    <artifactId>day29_mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>


</project>

29.4.3 Create user table

-- 创建数据库
CREATE DATABASE mybatis;
-- 使用数据库
USE mybatis;

CREATE TABLE `user` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(32) NOT NULL COMMENT '用户名称',
  `birthday` DATETIME DEFAULT NULL COMMENT '生日',
  `sex` CHAR(1) DEFAULT NULL COMMENT '性别',
  `address` VARCHAR(256) DEFAULT NULL COMMENT '地址',
  PRIMARY KEY  (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;


INSERT  INTO `user`(`id`,`username`,`birthday`,`sex`,`address`) VALUES (41,'老王','2018-02-27 17:47:08','男','北京'),(42,'小二王','2018-03-02 15:09:37','女','北京金燕龙'),(43,'小二王','2018-03-04 11:34:34','女','北京金燕龙'),(45,'传智播客','2018-03-04 12:04:06','男','北京金燕龙'),(46,'老王','2018-03-07 17:37:26','男','北京'),(48,'小马宝莉','2018-03-08 11:44:00','女','北京修正');

29.4.4 Create an entity class User corresponding to the table in the project

package com.xww.domain;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable{
    
    

    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    public Integer getId() {
    
    
        return id;
    }

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

    public String getUsername() {
    
    
        return username;
    }

    public void setUsername(String username) {
    
    
        this.username = username;
    }

    public Date getBirthday() {
    
    
        return birthday;
    }

    public void setBirthday(Date birthday) {
    
    
        this.birthday = birthday;
    }

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

    public String getAddress() {
    
    
        return address;
    }

    public void setAddress(String address) {
    
    
        this.address = address;
    }

    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

29.4.5 Write the persistence layer interface IUserDao

  • Provide query all methods findAll
package com.xww.dao;

import com.xww.domain.User;

import java.util.List;

/**
 * 用户的持久层接口
 */
public interface IUserDao {
    
    

    /**
     * 查询所有操作
     * @return
     */
    List<User> findAll();
}

29.4.6 Write the mapping file IUserDao.xml of the persistence layer interface

  • Claim:
    • Creation location : Must be in the same package as the persistence layer interface.
    • Name : must start withPersistence layer interface nameName the file name, the extension is .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.itheima.dao.IUserDao">
    <!-- 配置查询所有操作 -->
    <select id="findAll" resultType="com.itheima.domain.User">
        select * from user
    </select>
</mapper>

29.4.7 Create the core configuration file SqlMapConfig.xml of mybatis in the resource

  • Add constraints:
<?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">
  • Configure the mysql environment:
<?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">
<!-- mybatis的主配置文件 -->
<configuration>
    <!-- 配置环境 -->
    <environments default="mysql">
        <!-- 配置mysql的环境-->
        <environment id="mysql">
            <!-- 配置事务的类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源(连接池) -->
            <dataSource type="POOLED">
                <!-- 配置连接数据库的4个基本信息 -->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件
        如果是用注解来配置的话,此处应该使用class属性指定被注解的dao全限定类名
    -->
    <mappers>
    <mapper class="com/itheima/dao/IUserDao.xml"/>
    </mappers>
</configuration>

29.5 Precautions for setting up the environment

  • the first,
    • The names of IUserDao.xml and IUserDao.java were created to be consistent with our previous knowledge.
    • In Mybatis, it also calls the operation interface name and mapping file of the persistence layer: Mapper
    • So: IUserDao and IUserMapper are the same.
  • second,
    • When creating a directory in idea, it is not the same as a package.
      • When the package is created: com.xww.dao is a three-level structure
      • When the directory is created: com.xww.dao is the first-level directory
  • Third, the location of the mapping configuration file of mybatis must be the same as the package structure of the dao interface.
  • Fourth, the value of the namespace attribute of the mapper tag of the mapping configuration file must be the fully qualified class name of the dao interface.
  • Fifth, the operation configuration (select) of the mapping configuration file, the value of the id attribute must be the method name of the dao interface.
  • Note : When we comply with the third, fourth, and fifth, there is no need to write the implementation class of dao in the development.

Guess you like

Origin blog.csdn.net/qq_44686266/article/details/113987389