Mybatis02__Mybatis搭建

整个Demo结构如下:

  

  1、导包,我这里是Maven工程,只需要导入相应依赖即可

1
2
3
4
5
6
7
8
9
10
11
12
// 导入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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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映射配置文件

1
2
3
4
5
6
7
8
9
<?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实体类,用来封装查询的记录

1
2
3
4
5
6
// 实体类,忽略set/get/toString方法
public class User {
    private String id;
    private String userName;
    private Integer age;
}

 

  5、创建UserMapper接口

1
2
3
4
5
6
7
8
public interface UserMapper {
    /**
     * 根据用户id,查询出符合要求的用户记录
     * @param id 传入的用户唯一标志
     * @return 用户类型的对象
     */
    public abstract User queryUserById(String id);
}

 

  6、测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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、测试结果

1
User{id='1', userName='xiaomao', age=22}

 

  8、其它问题点

    1、如果出现下列报错,则代表数据库的驱动过时了,需要更换新的数据库驱动

1
2
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.

    新的数据库驱动

1
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>

    

    2、如果出现如下报错则是因为时区不对

1
2
3
4
5
6
7
8
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

1
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>

  

 

posted @   变体精灵  阅读(134)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示