java使用mybatis3.5连接MySQL8.0出现关于时区以及驱动上的一些小问题

最近在学习mybatis,初学想仿照示例写个“helloworld”式的简单程序,结果第一次运行就出现一堆错误,但是代码我全是照着示例敲的,也检查过了,并没有出现代码上的错误。无奈只得一个一个百度。

错误1:

Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
其实这个不能算是错误,只是MySQL8.0的一个改变,如提示所说不推荐使用

解决办法:

将数据库驱动变更为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 utilize time zone support.
从英文上翻译过来的意思就是时区出现问题了,好吧,网上百度了一下,解决办法如下。

解决办法:

在连接数据库的URL后面加上?serverTimezone=UTC
例如"jdbc:mysql://127.0.0.1:3306/edu?serverTimezone=UTC"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是关于使用MyBatis连接MySQL 8.0数据库的一些步骤: 1. 在Maven项目中添加MyBatisMySQL驱动的依赖 ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> ``` 2. 配置MyBatis的数据源 在src/main/resources目录下创建一个名为`mybatis-config.xml`的配置文件,并在其中添加以下内容: ```xml <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/your_database_name?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai" /> <property name="username" value="your_username" /> <property name="password" value="your_password" /> </dataSource> </environment> </environments> </configuration> ``` 注意将`your_database_name`、`your_username`和`your_password`替换为你自己的数据库名、用户名和密码。 3. 创建Mapper接口 创建一个Java接口,用于定义SQL语句的映射。例如,假设你要查询用户表中所有的用户数据,可以创建一个名为`UserMapper`的接口,定义如下方法: ```java public interface UserMapper { List<User> getAllUsers(); } ``` 其中,`User`是一个POJO类,代表一个用户对象。 4. 创建Mapper XML文件 在`src/main/resources`目录下创建一个名为`UserMapper.xml`的文件,并在其中添加以下内容: ```xml <mapper namespace="com.example.UserMapper"> <select id="getAllUsers" resultType="com.example.User"> SELECT * FROM user </select> </mapper> ``` 其中,`namespace`属性指定Mapper接口的完全限定名,`select`元素定义了查询所有用户数据的SQL语句。 5. 测试查询操作 在测试类中创建`SqlSessionFactory`对象,然后创建`SqlSession`对象,并获取Mapper接口的代理对象,最后调用`getAllUsers`方法查询用户数据。例如: ```java SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getAllUsers(); ``` 以上就是使用MyBatis连接MySQL 8.0数据库的基本步骤。注意在配置数据源时要使用MySQL 8.0的驱动,并且在URL中指定时区。 ### 回答2: MyBatis 是一种开源的持久层框架,它可以很好地与许多不同的数据库进行交互,其中就包括 MySQL8.0 数据库。如果想要使用 MyBatis 连接 MySQL8.0 数据库,可以按照以下步骤进行操作: 1. 首先,需要在项目中引入 MySQL JDBC 驱动程序。可以在项目的 pom.xml 文件中添加以下依赖项: ``` <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 2. 在 MyBatis 的配置文件中,需要指定使用的数据库类型为 MySQL,并设置连接的 URL、用户名和密码。配置文件的样例如下: ``` <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb8?useSSL=false&serverTimezone=UTC" /> <property name="username" value="root" /> <property name="password" value="password" /> </dataSource> </environment> </environments> <mappers> <!-- 在这里添加映射文件 --> </mappers> </configuration> ``` 注意,连接 URL 中需要指定使用的数据库名,并且需要添加 serverTimezone=UTC 的参数,这是因为 MySQL8.0 使用了新的默认时区 UTC,如果不设置会导致连接错误。 3. 在 MyBatis 中编写 SQL 映射文件,并使用 JDBC 的标准语法进行查询和更新操作。例如: ``` <select id="getUserById" parameterType="int" resultType="User"> SELECT * FROM users WHERE id = #{id} </select> ``` 其中,id 属性为查询语句的唯一标识符,parameterType 指定了输入参数的类型,resultType 指定了返回结果的类型,#{id} 表示使用输入参数的 id 值进行查询。 4. 在 Java 代码中,使用 SqlSessionFactoryBuilder 插件创建 SqlSessionFactory 对象,然后通过它创建 SqlSession 对象,最终调用它的 select 方法执行查询操作。例如: ``` SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")); SqlSession sqlSession = sessionFactory.openSession(); User user = sqlSession.selectOne("getUserById", 1); ``` 其中,"mybatis-config.xml" 是 MyBatis 的配置文件名,getUserById 是 SQL 映射文件中定义的查询语句的 id 值,1 是查询参数的值。 以上是使用 MyBatis 连接 MySQL8.0 数据库的基本步骤,需要根据具体项目的需要进行调整和优化。在实际应用中,还需要根据业务需求编写更复杂的 SQL 映射文件,并加入事务、缓存等特性,以保证系统的性能和稳定性。 ### 回答3: MybatisJava语言的一种持久层框架,它提供了对关系型数据库的访问和操作。在使用Mybatis连接MySQL 8.0数据库时,需要按照以下步骤: 第一步,添加MySQL JDBC驱动Mybatis使用JDBC连接数据库,需要添加MySQL JDBC驱动包。可以在官网下载MySQL JDBC驱动器,或者在pom.xml中添加以下依赖项: ``` <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> ``` 第二步,配置数据源。在Mybatis中,数据源的配置是通过配置文件来完成。在配置文件中,需要指定MySQL数据库的地址、用户名、密码等信息。 例如,在application.yml中添加如下配置: ``` spring: datasource: url: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver ``` 其中,url表示MySQL数据库的连接地址,localhost表示服务器地址,3306表示端口号,mydb表示数据库名称,useUnicode、characterEncoding和serverTimezone表示数据库字符集和时区。username和password表示连接MySQL数据库的用户名和密码。 第三步,创建SqlSessionFactory。在Mybatis中,SqlSessionFactory是用于创建SqlSession的工厂接口。在创建SqlSessionFactory时,需要传入数据源的配置。 例如,在Mybatis的配置文件mybatis.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"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${spring.datasource.driver-class-name}" /> <property name="url" value="${spring.datasource.url}" /> <property name="username" value="${spring.datasource.username}" /> <property name="password" value="${spring.datasource.password}" /> </dataSource> </environment> </environments> </configuration> ``` 其中,environments用于配置环境,transactionManager用于配置事务管理器,dataSource用于配置数据源,driver、url、username和password与application.yml中的配置保持一致。 第四步,创建SqlSession。在Mybatis中,SqlSession是用于与数据库进行交互的会话接口。在创建SqlSession时,需要传入SqlSessionFactory。 例如,在Java代码中创建SqlSession: ``` @Configuration @MapperScan(basePackages = "com.example.mapper", sqlSessionFactoryRef = "sqlSessionFactory") public class MybatisConfig { @Autowired private DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); Resource resource = new PathMatchingResourcePatternResolver().getResource("classpath:mybatis.xml"); sqlSessionFactoryBean.setConfigLocation(resource); return sqlSessionFactoryBean.getObject(); } @Bean(name = "sqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 其中,@MapperScan用于扫描Mapper接口,sqlSessionFactoryRef指定SqlSessionFactory的名称,dataSource表示数据源,sqlSessionFactoryBean用于创建SqlSessionFactory,getResource用于加载Mybatis的配置文件mybatis.xml,getObject用于返回SqlSessionFactory。 第五步,编写Mapper接口和Mapper映射文件。在Mybatis中,Mapper接口用于定义SQL语句的方法,Mapper映射文件则用于定义SQL语句的具体实现。 例如,编写UserMapper接口: ``` public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User selectUserById(@Param("id") Long id); @Select("SELECT * FROM user") List<User> selectAllUsers(); @Insert("INSERT INTO user (name, age) VALUES (#{name}, #{age})") void addUser(User user); @Update("UPDATE user SET age = #{age} WHERE id = #{id}") void updateUserAge(@Param("id") Long id, @Param("age") Integer age); @Delete("DELETE FROM user WHERE id = #{id}") void deleteUserById(@Param("id") Long id); } ``` 其中,@Select、@Insert、@Update和@Delete用于定义SQL语句,#{id}、#{name}和#{age}是占位符,@Param用于指定占位符的值。 编写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="com.example.mapper.UserMapper"> <resultMap id="UserResultMap" type="com.example.entity.User"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> </resultMap> <select id="selectUserById" resultMap="UserResultMap"> SELECT * FROM user WHERE id = #{id} </select> <select id="selectAllUsers" resultMap="UserResultMap"> SELECT * FROM user </select> <insert id="addUser"> INSERT INTO user (name, age) VALUES (#{name}, #{age}) </insert> <update id="updateUserAge"> UPDATE user SET age = #{age} WHERE id = #{id} </update> <delete id="deleteUserById"> DELETE FROM user WHERE id = #{id} </delete> </mapper> ``` 其中,resultMap用于定义查询结果的映射关系,select、insert、update和delete用于定义SQL语句。 第六步,编写业务逻辑代码。在业务逻辑代码中,需要注入UserMapper接口,并调用其方法。 例如,在Java代码中编写UserController: ``` @RestController @RequestMapping("/user") public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userMapper.selectUserById(id); } @GetMapping("/") public List<User> getAllUsers() { return userMapper.selectAllUsers(); } @PostMapping("/") public void addUser(User user) { userMapper.addUser(user); } @PutMapping("/{id}/{age}") public void updateUserAge(@PathVariable Long id, @PathVariable Integer age) { userMapper.updateUserAge(id, age); } @DeleteMapping("/{id}") public void deleteUserById(@PathVariable Long id) { userMapper.deleteUserById(id); } } ``` 其中,@Autowired用于注入UserMapper,@GetMapping、@PostMapping、@PutMapping和@DeleteMapping用于定义HTTP请求的类型和路径。 最后,启动应用程序,并测试Mybatis连接MySQL 8.0数据库的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值