快速创建简单的mybatis应用

2022年4月8日 13点热度 0条评论

1.导包(配置pom.xml)
一定要用这个网站:https://mvnrepository.com/

点击查看代码

<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.5.4</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.47</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.9</version>
		</dependency>
		
</dependencies>

2.创建mybatis核心配置文件(mybatis-config.xml)
去官网中文文档入门里面复制也ok

点击查看代码

<?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="jdbc.properties">
  </properties>
  <typeAliases>
      <package name="com.mhys.bean" />
  </typeAliases>
  
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driverClassName}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <package name="com.mhys.mapper"/>
  </mappers>
</configuration>

3.创建实体类(注意字段要和表里字段对应)

这是我的数据库字段

4.创建jdbc和logo4j配置文档

5.创建mapper接口(接口命名和映射文件一致)

6.创建测试类

点击查看代码

@Test
	public void testSelect() {
		try {
			InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(is);
			SqlSession session=sf.openSession();//创建一次对话(类似于mysql的对话=connection)
			AdminMapper am=session.getMapper(AdminMapper.class);
			List<Admin> list=am.findAdmin();
			System.out.println(list.toString());
			session.commit();
			session.close();
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Test
	public void testDelete() {
		try {
			InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(is);
			SqlSession session=sf.openSession();//创建一次对话(类似于mysql的对话=connection)
			AdminMapper am=session.getMapper(AdminMapper.class);
			int num=am.deleteAdmin("4");
			System.out.println(num);
			session.commit();
			session.close();
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Test
	public void testAdd() {
		try {
			InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(is);
			SqlSession session=sf.openSession();//创建一次对话(类似于mysql的对话=connection)
			AdminMapper am=session.getMapper(AdminMapper.class);
			int num=am.addAdmin(new Admin(null, "增加", "李四", "超级管理员", 0));
			System.out.println(num);
			session.commit();
			session.close();
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Test
	public void testupdate() {
		try {
			InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(is);
			SqlSession session=sf.openSession();//创建一次对话(类似于mysql的对话=connection)
			AdminMapper am=session.getMapper(AdminMapper.class);
			int num= am.updateAdmin(new Admin(14, "呆瓜", "呆瓜", "老六", 0));
			System.out.println(num);
			session.commit();
			session.close();
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

7.调用前数据库

8.调用

9.控制台

10.调用后数据库

后续将会更新使用xml映射使用