作为一个java菜鸟,早就从慕课网中学到一些基本的mybatis的用法,但是一直不成体系,懵懵懂懂,既然正式入了java这个坑,就打算好好学学,所以买了本《MyBatis从入门到精通》,在此记录下学习的点滴。
数据库版本:Server version: 8.0.15 MySQL Community Server - GPL
数据准备:
先写一个最简单的查询
1、新建一个空白的maven项目,引入相关依赖包
<dependencies> <!--测试需要--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version></version> </dependency> <!--数据库连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version></version> </dependency> </dependencies>
2、在resources中添加mybatis的配置文件mybatis-config.xml
这里选用最基本的jdbc事务管理器,不用数据库连接池。此处因为mysql的版本原因,要禁用SSL。同时配置数据库实体的位置和mapper文件路径。
<?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> <typeAliases> <package name="com.forest.owl.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value=""/> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/forest?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserMapper.xml" /> </mappers> </configuration>
2、编辑数据库实体User
package com.forest.owl.entity; import java.util.Date; public class User { private int id; private String account; private String passwd; private String nickname; private String phone; private String email; private Date createTime; private Date updateTime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }
3、在resources中添加mapper文件夹,在该文件夹中添加UserMapper.xml文件
mapper文件的命名空间是必须的,这里是随便命名,暂且还不清楚该属性的具体作用。
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mapper"> <select id="selectUsers" resultType="com.forest.owl.entity.User"> SELECT id, account, passwd, nickname, phone, email, create_time as createTime, update_time as updateTime FROM user </select> </mapper>
4、编写测试类
import com.forest.owl.entity.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.BeforeClass; import org.junit.Test; import java.io.IOException; import java.io.Reader; import java.util.List; public class TestUserMapper { private static SqlSessionFactory sqlSessionFactory; @BeforeClass public static void init(){ try { Reader reader = Resources.getResourceAsReader("mybatis.config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } @Test public void TestMybatis(){ SqlSession sqlSession = sqlSessionFactory.openSession(); List<User> userList = sqlSession.selectList("selectUsers"); System.out.println(userList.size()); System.out.println(userList.get(0).getNickname()); System.out.println(userList.get(0).getCreateTime()); } }
测试结果: