Mybatis中遇到的坑

Mybatis的基本配置

1. 首先使用Maven添加对应的依赖

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.5.0</version>
</dependency>

2. 然后新建配置文件mybatis-config.xml

按照如下配置:

  1. driver:连接数据库的驱动(MySQL驱动是com.mysql.jdbc.Driver)
  2. url:数据库所在地址(本地数据库地址是jdbc:mysql://localhost:3306/mybatis)
  3. username:用户名
  4. password:密码
<?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">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    </mappers>
</configuration>

3. 从 XML 中构建 SqlSessionFactory

使用Resource工具类构建SqlSessionFactory实例

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

4. 不使用 XML 构建 SqlSessionFactory

如果你更愿意直接从 Java 程序而不是 XML 文件中创建 configuration,或者创建你自己的 configuration 构建器,MyBatis 也提供了完整的配置类,提供所有和 XML 文件相同功能的配置项

DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

注意该例中,configuration 添加了一个映射器类(mapper class)。映射器类是 Java 类,它们包含 SQL 映射语句的注解从而避免了 XML 文件的依赖。不过,由于 Java 注解的一些限制加之某些 MyBatis 映射的复杂性,XML 映射对于大多数高级映射(比如:嵌套 Join 映射)来说仍然是必须的。有鉴于此,如果存在一个对等的 XML 配置文件的话,MyBatis 会自动查找并加载它(这种情况下, BlogMapper.xml 将会基于类路径和 BlogMapper.class 的类名被加载进来)。

5.运行测试类报错

1、Loading class

com.mysql.jdbc.Driver'. This is deprecated. The new driver class is

com.mysql.cj.jdb

新版mysql连接驱动需要将

com.mysql.dbc.Driver修改为

com.mysql.cj.jdbc.Driver

2、 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 util…

这是因为时区的错误,因此只要将时区设置为你当前系统时区即可,

因此使用root用户登录mysql,按照下列测试

set global time_zone=’+8:00′;

3、 java.io.IOException: Could not find resource mybatis-config.xml

由于使用idea新建项目,因此xml文件不能放在src的Java文件夹下,而是要单独放在resources目录下。

 

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

推荐一个java教程网站:how2j,教程从ssh、ssm、spring mvc 、spring boot、spring cloud都有,简单易懂,方便入门。

前端练手项目,模仿天猫前端 :http://how2j.cn/k/tmall-front/tmall-front-790/790.html?p=13028

JAVA WEB J2EE 练手项目,模仿天猫整站: http://how2j.cn/k/tmall-j2ee/tmall-j2ee-894/894.html?p=13028

JAVA 桌面软件练手项目,一本糊涂账:http://how2j.cn/k/hutubill/hutubill-tutorials/710.html?p=13028

JAVA 自学网站:http://how2j.cn?p=13028

JAVA 练习题:http://how2j.cn/frontshowJavaPractises?p=13028

Hibernate 教程:http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?p=13028

Struts 教程:http://how2j.cn/k/struts/struts-tutorial/54.html?p=13028

SSH整合教程:http://how2j.cn/k/ssh/ssh-integrationtest/100.html?p=13028

SSM教程:http://how2j.cn/k/ssm/ssm-tutorial/1137.html?p=13028

Mybatis 教程:http://how2j.cn/k/mybatis/mybatis-tutorial/1087.html?p=13028

Spring MVC 教程:http://how2j.cn/k/springmvc/springmvc-springmvc/615.html?p=13028

Spring boot教程:http://how2j.cn/k/tmall_springboot/tmall_springboot-1799/1799.html?p=13028

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值