- CREATE TABLE `mayikt_user` (
- `id` int NOT NULL AUTO_INCREMENT,
- `username` varchar(20) DEFAULT NULL,
- `userpwd` varchar(20) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb3
没下载安装或者不会创建的小伙伴可以看这里:maven的下载安装与配置环境变量!!!(全网最详细)_明天更新的博客-CSDN博客
创建完成后配置pom.xml文件:
- <dependencies>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.13version>
- dependency>
-
- <dependency>
- <groupId>com.mysqlgroupId>
- <artifactId>mysql-connector-jartifactId>
- <version>8.1.0version>
- dependency>
-
- dependencies>
- "1.0" encoding="UTF-8" ?>
- 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://127.0.0.1:3306/mayikt?serverTimezone=GMT%2B8"/>
- <property name="username" value="root"/>
- <property name="password" value=""/>
- dataSource>
- environment>
- environments>
- <mappers>
- <mapper resource="mapper/userMapper.xml"/>
- mappers>
- configuration>
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="userMapper">
- <select id="getByUsers" resultType="com.entity.UserEntity">
- select * from mayikt_user
- select>
- mapper>
- /*
- * Copyright (c) 2020, 2023, All rights reserved.
- *
- */
- package com.entity;
-
- /**
- *
Project: mybatis-dome - UserEntity
- *
Powered by scl On 2023-09-08 10:18:39
- *
描述:
- *
- * @author 孙臣龙 [1846080280@qq.com]
- * @version 1.0
- * @since 17
- */
- public class UserEntity {
- private Integer id;
- private String username;
- private String userpwd;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getUserpwd() {
- return userpwd;
- }
-
- public void setUserpwd(String userpwd) {
- this.userpwd = userpwd;
- }
-
- @Override
- public String toString() {
- return "UserEntity{" +
- "id=" + id +
- ", username='" + username + '\'' +
- ", userpwd='" + userpwd + '\'' +
- '}';
- }
- }
- /*
- * Copyright (c) 2020, 2023, All rights reserved.
- *
- */
- package com.test;
-
- import com.entity.UserEntity;
- 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 java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
-
- /**
- *
Project: mybatis-dome - Test01
- *
Powered by scl On 2023-09-08 10:29:37
- *
描述:
- *
- * @author 孙臣龙 [1846080280@qq.com]
- * @version 1.0
- * @since 17
- */
- public class Test01 {
- public static void main(String[] args) throws IOException {
- // 1.读取加载mybatis-config.xml
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- // 2.获取到获取到
- SqlSession sqlSession = sqlSessionFactory.openSession();
- // 3.根据 mapper id=getByUsers 执行该s ql 语句 通过 sql语句得到我们的对象 orm
- List
userEntitys = sqlSession.selectList("getByUsers", UserEntity.class); - System.out.println(userEntitys);
- sqlSession.close();
- }
- }