mybatis本地连接数据库报错

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 726 milliseconds ago.  The last packet sent successfully to the server was 721 milliseconds ago.
### The error may exist in com/kuang/dao/UserMapper.xml
### The error may involve com.kuang.dao.UserDao.getUserList
### The error occurred while executing a query
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure


解决方法:

<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>

把mybatis-config.xml中的
useSSL=false 得以解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一款优秀的ORM框架,它的数据库连接是通过配置文件来实现的。具体步骤如下: 1. 引入MyBatis依赖 在Maven项目中,在pom.xml文件中引入MyBatis依赖: ``` <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency> ``` 其中,x.x.x表示MyBatis的版本号。 2. 配置数据源 在src/main/resources目录下创建一个名为mybatis-config.xml的配置文件,并在其中配置数据源。例如,使用MySQL数据库,可以按照如下方式配置: ``` <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/mydatabase" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> </configuration> ``` 其中,`<dataSource>`标签中的`type`属性值必须为`POOLED`,表示使用连接池。 3. 编写Mapper接口和Mapper.xml文件 Mapper接口和Mapper.xml文件是MyBatis的核心部分,它们用于定义SQL语句和映射关系。例如,定义一个UserMapper接口和对应的Mapper.xml文件,可以按照如下方式编写: UserMapper.java ``` public interface UserMapper { User getUserById(int id); } ``` UserMapper.xml ``` <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" parameterType="int" resultType="com.example.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 4. 创建SqlSessionFactory和SqlSession对象 在Java代码中,创建SqlSessionFactory和SqlSession对象,并通过SqlSession对象执行SQL语句。例如,可以按照如下方式创建: ``` String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUserById(1); System.out.println(user.toString()); sqlSession.close(); ``` 以上是MyBatis连接数据库的基本步骤。需要注意的是,MyBatis支持多种数据源,如JNDI、Spring等,具体使用方式可以参考MyBatis官方文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值