IDEA steps to create a Mybatis Maven project (graphic)

This article mainly introduces the method steps (graphics) of IDEA to create a Mybatis Maven project. The article introduces it in detail through the sample code. It has certain reference learning value for everyone's study or work. Let's study together

Step 1: Create a new project first

Insert picture description here

Just go directly to next, no need to select the content, otherwise he will download the skeleton and get stuck for a long time

Insert picture description here

The first arrow is the project name, and the second arrow is the project path, just modify it according to your needs. The newly-built project looks like this.

Insert picture description here

Step 2: We need to add a web framework support

Right-click on your project and select Add Framework.

Insert picture description here

Select Web Application and click ok.

Insert picture description here

In this way, the project structure is there, and the point is that it is convenient to adjust nothing.

Insert picture description here

The third step: to configure Tomcat

Insert picture description here

Choose Local under Tomcat Server,
choose where your Tomcat is here

Insert picture description here

Insert picture description here

Follow the arrow steps to add the project to Tomcat from top to bottom. Then Apply OK will do.

Step 4: Configure maven dependencies

This step is recommended to go directly to the Internet to find the ready-made ones, so that there will be no version incompatibility problems.

<dependencies>
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.3.0</version>
  </dependency>
  <!-- mysql驱动包 
  	这个驱动包要根据自己MySQL版本来配置不然就会出bug-->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.11</version>
  </dependency>
  <!-- junit测试包 -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.11</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

then click Insert picture description here

This waits for him to configure, then start. Successful startup proves that the Tomcat configuration is OK.

Step 5: Create a database and write pojo classes

Create pojo package Insert picture description here

Write user class according to your own database.

Step 6: Configure mybatis.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="mysql">
  <!--配置mysql的环境-->
  <environment id="mysql">
   <!--配置事务的类型-->
   <transactionManager type="JDBC"></transactionManager>
   <!--配置连接池-->
   <dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/testuser?serverTimezone=Asia/Shanghai" />
    <property name="username" value="root" />
    <property name="password" value="li4918458" />
   </dataSource>
  </environment>
 </environments>
 <!-- mapping文件路径配置 -->
 <mappers>
  <mapper resource="mapper/UserMapper.xml"/>
 </mappers>

</configuration>

Then configure UserMapper.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="pojo.User">
 <!--id ="接口中的方法名"
   parameterType="传入的参数类型"
   resultType = "返回实体类对象,使用包.类名"-->

 <select id="findById" parameterType="int" resultType="pojo.User"> select * from user where id = #{
    
    id}

 </select> 
 </mapper>

Step 7: Test
Create the Test class for testing. There is a key session.selectOne("findById",1); the findById in it is written according to your SQL statement id in UserMapper.xml, you can't write both of them. Be consistent.

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

public class test {
    
    
@Test
public void testSearchById() throws IOException {
    
    
 //1.读取配置文件
 InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
 //2.创建SqlSessionFactory工厂
 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
 //3.使用工厂生产SqlSession对象
 SqlSession session = sqlSessionFactory.openSession();
 //4.执行Sql语句
 User user = session.selectOne("test.findUserById", 1);
 //5. 打印结果
 System.out.println(user);
 //6.释放资源
 session.close();
 in.close();
}

 //根据用户名模糊查询用户列表
 @Test
 public void testFindUserByUsername() throws IOException {
    
    
  //定义读取文件名
  String resources = "mybatis-config.xml";
  //创建流
  Reader reader=null;
  try {
    
    
   //读取mybatis-config.xml文件到reader对象中
   reader= Resources.getResourceAsReader(resources);
  } catch (IOException e) {
    
    
   e.printStackTrace();
  }
  //初始化mybatis,创建SqlSessionFactory类的实例
  SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader);
  //创建session实例
  SqlSession session=sqlMapper.openSession();
  //传入参数查询,返回结果
  User user=session.selectOne("findById",1);
  //输出结果
  System.out.println(user.getName());
  //关闭session
  session.close();
 }
}

Execute the program and you will get

Insert picture description here

That's it~

So far, this article about IDEA's method steps (graphics) for creating a Mybatis Maven project is here. The above is all the content of this article. I hope it will be helpful to everyone's learning, and I hope you can support me.

The latest high-frequency interview questions collected in 2021 (all organized into documents), there are a lot of dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations. There are also detailed learning plans and interviews. Questions, etc., friends who need to obtain these content, please add Q Junyang: 547998459

Guess you like

Origin blog.csdn.net/p1830095583/article/details/114744998