创建user表,添加数据,Mysql:
1 create database mybatis; 2 use mybatis; 3 drop table if exists tb_user; 4 create table tb_user( 5 id int primary key auto_increment, 6 username varchar(20), 7 password varchar(20), 8 gender char(1), 9 addr varchar(30) 10 ); 11 INSERT INTO tb_user VALUES (1, 'zhangsan', '123', '男', '北京'); 12 INSERT INTO tb_user VALUES (2, '李四', '234', '女', '天津'); 13 INSERT INTO tb_user VALUES (3, '王五', '11', '男', '西安');
创建模块,导入坐标 在创建好的模块中的 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>org.example</groupId>
<artifactId>mybatis-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- 添加slf4j日志api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.20</version>
</dependency>
<!-- 添加logback-classic依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 添加logback-core依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
编写 MyBatis 核心配置文件 -- > 替换连接信息 解决硬编码问题 在模块下的 resources 目录下创建mybatis的配置文件 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>
<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:///mybatis?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载sql的映射文件 , -->
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
编写 SQL 映射文件 --> 统一管理sql语句,解决硬编码问题 在模块的 resources 目录下创建映射配置文件 UserMapper.xml ,内容如下:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- 6 namespace:名称空间 7 --> 8 9 <mapper namespace="test"> 10 <select id="selectAll" resultType="com.itheima.pojo.User"> 11 select * from tb_user; 12 </select> 13 </mapper>
在 com.itheima.pojo 包下创建 User,在 com.itheima 包下编写 MybatisDemo 测试:
注意:mybatis-config.xml 文件 的 相关配置,如添加UTC和数据库账号密码,且pom.xml的数据库最好和已安装的数据库版本号相同,并且将插件Maven Helper关闭。
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
标签:xml,入门,配置文件,mybatis,user,Mybatis,org,tb,黑马 From: https://www.cnblogs.com/MasterYang/p/16612543.html