整个Demo结构如下:
1、导包,我这里是Maven工程,只需要导入相应依赖即可
// 导入Mybatis的jar包 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> // 导入数据库驱动,我这里的数据库版本是Mysql 8.x版本的 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
2、创建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">
<!-- 配置事务管理,采用JDBC管理事务-->
<transactionManager type="JDBC"/>
<!-- POOLED是mybatis的数据源 -->
<!-- JNDI是基于tomcat的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- pojo的映射文件UserMapper引入到配入到配置文件中 -->
<mappers>
<!-- resource要写成路径 -->
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
3、创建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">
<!--UserMapper接口的包类全路径-->
<mapper namespace="com.mybatis.dao.UserMapper">
// 与UserMapper中的具体查询方法相同,实现绑定,告知具体调用那一条SQL语句
<select id="queryUserById" resultType="com.mybatis.entity.User">
select id,user_name as userName,age from user where id = #{id}
</select>
</mapper>
4、创建User实体类,用来封装查询的记录
// 实体类,忽略set/get/toString方法
public class User {
private String id;
private String userName;
private Integer age;
}
5、创建UserMapper接口
public interface UserMapper {
/**
* 根据用户id,查询出符合要求的用户记录
* @param id 传入的用户唯一标志
* @return 用户类型的对象
*/
public abstract User queryUserById(String id);
}
6、测试类
public class TestMybatis {
@Test
public void testMybatis() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.queryUserById("1");
System.out.println(user);
}
}
7、测试结果
User{id='1', userName='xiaomao', age=22}
8、其它问题点
1、如果出现下列报错,则代表数据库的驱动过时了,需要更换新的数据库驱动
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
新的数据库驱动
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
2、如果出现如下报错则是因为时区不对
com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���ʱ��' is
unrecognized or represents more than one time zone. You must configure either the server or JDBC driver
(via the 'serverTimezone' configuration property) to
use a more specifc time zone value if you want to utilize time zone support.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
加上如下配置即可 ?serverTimezone=UTC
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>