Steps: 1. Import the relevant JAR package

  • junit
  • mybatis
  • The mysql database
  • Related to the spring
  • Aop weave
  • mybatis-spring

1. Create a new Maven project

<dependencies> <! > < Denpendency > <groupId> jUnit </groupId> <artifactId> jUnit </artifactId> <version>4.12</version> </dependency> <! --mysql--> <denpendency> <groupId>mysql</groupId> <artifactId>mysql-connerctor-java</artifactId> The < 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 > <! -- If Spring operates on a database, Spring Framework </ grouppid > <artifactId>spring-jdbc</artifactId>. Spring Framework </ grouppid > <artifactId>spring-jdbc</artifactId> < version > 5.1.9. RELEASE < / version > < / dependency > <! AspectJ </groupId> </artifactId> AspectJweaver </artifactId> 1.8.13</version>  </dependency> <! - mybatis -- spring integration -- -- > < denpendency > < groupId > org. Mybatis < / groupId > < artifactId > mybatis - spring < / artifactId > <version>2.0.2</version> </ 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. Write configuration files

3. The test

1.1. Recall MyBatis

1. Write an entity class

package com.jialidun.pojo;

import lombok .Data;

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

2. Wrote the core configuration file mybatis-config.xml

<? The 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 core configuration file --> <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. Write interfaces

package com.jialidun.mapper;

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

}

4. Write the Mapper. XML

<? The 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" > <! - the configuration of the core configuration file - > < mapper namespace = "com. Jialidun. Mapper. UserMapper" > < select id = "selectUser resultType =" user ">" select * from mybatis.user; </select> </mapper>

5. Test

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); }}}