Maven+mybatis does not integrate Spring framework

1. Use Maven to build a webapp project

Insert picture description here

2. Import mybatis and mysql dependencies in pom.xml

**

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>niceu</groupId>
  <artifactId>mybatis.demo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mybatis.demo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
	<dependency>
	    <groupId>org.mybatis</groupId>
	    <artifactId>mybatis</artifactId> 
	    <version>3.5.3</version>
	</dependency>
	   <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>8.0.16</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>mybatis.demo</finalName>
  </build>
</project>

**

3. Create a new db.pproperties (name can be customized)

**

-- 配置数据库的相关信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/****?serverTimezone=UTC 
jdbc.username=**
jdbc.password=**

**

4. Create a new mybatis-config.xml (name can be customized)

**

<?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"></properties>
    <typeAliases>
      <package name="com.niceu.mybatis.demo.pojo"></package>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"></property>
                <property name="url" value="${jdbc.url}"></property>
                <property name="username" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>
            </dataSource>
        </environment>
    </environments>
    <!-- 全局变量的配置 -->

    <!--加载映射文件-->
    <mappers>
        <mapper resource="xml/UserMapper.xml"></mapper>
    </mappers>
</configuration>

**
Configure data source information

5. Prepare the User table and data in advance in the mysql database

**

Insert picture description here

**

6. Write the mapping class User of the table

**

package com.niceu.mybatis.demo.pojo;

import org.apache.ibatis.type.Alias;

@Alias("UserInfo")
public class User {
	
	private int id;
	private String userName;
	private String password;
	
	

	public int getId() {
		return id;
	}


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


	public String getUserName() {
		return userName;
	}


	public void setUserName(String userName) {
		this.userName = userName;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("用户ID:").append(this.getId()).append("  ").
		append("用户名:").append(this.getUserName()).append("  ").
		append("密码:").append(this.getPassword());
		return sb.toString();
	}

}

Insert picture description here


Note the alias here.
**

7. New UserMapper interface

**

package com.niceu.mybatis.demo.dao;

import com.niceu.mybatis.demo.pojo.User;

public interface UserMapper {
	
	User getUserInfoByUserName(String userName);
	
}

**

**

<?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.niceu.mybatis.demo.dao.UserMapper">
    <resultMap id="myuser" type="UserInfo" >
        <id property="id" column="id"/>
        <result property="userName" column="username"/>
        <result property="password" column="password"/>
    </resultMap>
    
    <select id="getUserInfoByUserName" parameterType="String" resultMap="myuser">
      	select id,username,password from user where username=#{userName}
  	</select>
</mapper>
Insert picture description here


The alias mapping
id of the type and User class here corresponds to the method name in the UserMapper interface, and the namespace corresponds to the fully qualified name of the UserMapper interface.
This completes the mapping.

Insert picture description here


Insert picture description here