MyBatis入门实例教程之创建一个简单的程序
作者:emanlee
准备:
(1) IDEA 2021
(2)Java 1.8
(3)数据库 MySQL 5.7 (SQLyog 或 Navicat)
在 MySQL 中创建数据库 mybatisdemo,编码为 utf8
新建表:
USE mybatisdemo CREATE TABLE users( uid INT PRIMARY KEY AUTO_INCREMENT, uname VARCHAR(20) NOT NULL, uage INT NOT NULL ); INSERT INTO users(uid,uname,uage) VALUES(NULL,'张##',25),(NULL,'李##',23),(NULL,'王##',23);
IDEA 创建 maven工程
完成之后如下图所示:
在 src - main - java 下新建 package, 名字为 com.mybatisdemo.pojo
在 com.mybatisdemo.pojo 中新建类 User.java
package com.mybatisdemo.pojo; public class User { private int uid; //用户id private String uname; //用户姓名 private int uage; //用户年龄 public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; public String getUname() { return uname; public void setUname(String uname) { this.uname = uname; public int getUage() { return uage; public void setUage(int uage) { this.uage = uage; }
在 resources 下新建文件夹 mapper
在 mapper 下建文件 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为映射的根节点--> <!-- mapper为映射的根节点,namespace指定Dao接口的完整类名 mybatis会依据这个接口动态创建一个实现类去实现这个接口, 而这个实现类是一个Mapper对象--> <mapper namespace="com.mybatisdemo.pojo.User"> <!--id ="接口中的方法名" parameterType="传入的参数类型" resultType = "返回实体类对象,使用包.类名"--> <select id="findById" parameterType="int" resultType="com.mybatisdemo.pojo.User"> select * from users where uid = #{id} </select> </mapper>
在 resources 下新建 db.properties
mysql.driver=com.mysql.cj.jdbc.Driver mysql.url=jdbc:mysql://127.0.0.1:3306/mybatisdemo?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false mysql.username=root mysql.password=123456
在 resources 下新建 mybatis-config.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> <!-- 环境配置 --> <!-- 加载类路径下的属性文件 --> <properties resource="db.properties"/> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <!-- 数据库连接相关配置 ,db.properties文件中的内容--> <dataSource type="POOLED"> <property name="driver" value="${mysql.driver}"/> <property name="url" value="${mysql.url}"/> <property name="username" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> </dataSource> </environment> </environments> <!-- mapping文件路径配置 --> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
test - java - 下建 package, 名字为Test
test - java - Test 下建立文件 UserTest.java
package Test; import com.mybatisdemo.pojo.User; 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 java.io.IOException; import java.io.Reader; public class UserTest { @Test public void userFindByIdTest() { //读取文件名 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); //创建SqlSession实例 SqlSession session = sqlMapper.openSession(); //传入参数查询,返回结果 User user = session.selectOne("findById", 1); //输出结果 System.out.println(user.getUname()); //关闭session session.close(); } }
修改 pom.xml 文件
<?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.mybatisdemo</groupId> <artifactId>mybatis_demo01</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <!-- <scope>test</scope>--> <scope>compile</scope> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project>
最终的文件:
在 UserTest.java 的userFindByIdTest 上右键 :
REF
https://www.cnblogs.com/benjieqiang/p/11183580.html
https://blog.csdn.net/qq_32166627/article/details/70741729
https://mybatis.org/mybatis-3/zh/getting-started.html
https://www.w3cschool.cn/mybatis/mybatis-dyr53b5w.html
到此这篇关于MyBatis入门实例教程之创建一个简单的程序的文章就介绍到这了,更多相关mybatis创建程序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!