数据库 DDL
CREATE DATABASE `mybatis_demo01`;
-- mybatis_demo01.`user` definition
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
pom
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
</dependency>
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="default">
<environment id="default">
<!-- 事务类型 -->
<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/mybatis_demo01?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 映射配置文件位置 -->
<mappers>
<!-- 注解 OR 包路径的 XML -->
<!--<mapper class=""></mapper>-->
<!-- XML 路径 -->
<!-- 这样不行, 那么 Mybatis 支持哪些形式的呢,只能配置一个吗?通配符呢 -->
<!--<mapper resource="classpath:mapper/*"/>-->
<mapper resource="mapper/UserMapper.xml"/>
<!-- XML 路径 -->
<!--<mapper url=""></mapper>-->
<!-- 注解 PR 包路径的 XML, 包下的都扫描 -->
<!--<package name=""/>-->
</mappers>
</configuration>
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 namespace="cn.lemon.dao.UserDao">
<resultMap id="BaseResultMap" type="cn.lemon.domain.User">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="username" column="username" jdbcType="VARCHAR"/>
<result property="birthday" column="birthday" jdbcType="TIMESTAMP"/>
<result property="sex" column="sex" jdbcType="CHAR"/>
<result property="address" column="address" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,username,birthday,
sex,address
</sql>
<!-- 返回类型必须写(非JavaBean也必须写吗),JavaBean 一般要使用 resultMap(阿里巴巴的规范) -->
<select id="findAll" resultType="cn.lemon.domain.User">
select *
from user;
</select>
</mapper>
Java 代码
User
package cn.lemon.domain;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class User implements Serializable {
/**
*
*/
private Integer id;
/**
* 用户名称
*/
private String username;
/**
* 生日
*/
private Date birthday;
/**
* 性别
*/
private String sex;
/**
* 地址
*/
private String address;
private static final long serialVersionUID = 1L;
}
UserDao
package cn.lemon.dao;
import cn.lemon.domain.User;
import java.util.List;
public interface UserDao {
/**
* 查询所有用户
*
* @return 用户列表,无则返回空列表
*/
List<User> findAll();
}
测试
package cn.lemon.dao;
import cn.lemon.domain.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 java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
public class UserDaoTest {
public static void main(String[] args) throws IOException {
// 使用流程
// 1. 读取配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
// 2. 创建 SqlSessionFactory 工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 3. 生产 SqlSession 对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4. 获取 代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
// 5. 执行方法
List<User> users = userDao.findAll();
System.out.println(Arrays.toString(users.toArray()));
// 6. 释放资源
sqlSession.close();
inputStream.close();
}
}
标签:01,java,--,Demo,private,mybatis,import,MyBatis,org
From: https://www.cnblogs.com/chenxingyang/p/18122935