weixin_45882825
weixin_45882825
2020-08-10 22:21
采纳率: 100%
浏览 135
已采纳

MyBatis和Spring的整合:传统Dao方式的整合

我的mysql是6.0
报错如下

Error querying database.

Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (Unknown system variable 'lower_case_table_names')

The error may exist in com/wyy/po/CustomerMapper.xml

The error may involve com.wyy.po.CustomerMapper.findcustomerbyid

The error occurred while executing a query

Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (Unknown system variable 'lower_case_table_names')

at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy2.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
at com.wyy.dao.impl.CustomerDaoimpl.findCustomerByid(CustomerDaoimpl.java:13)
at com.wyy.test.TestDao.main(TestDao.java:17)

以下是项目图片
图片说明

代码如下:
CustomerDao.java:
package com.wyy.dao;

import com.wyy.po.Customer;

public interface CustomerDao {
//通过id查询用户
public Customer findCustomerByid(Integer id);
}


CustomerDaoimpl.java

package com.wyy.dao.impl;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.wyy.dao.CustomerDao;
import com.wyy.po.Customer;

public class CustomerDaoimpl extends SqlSessionDaoSupport implements CustomerDao {

@Override
public Customer findCustomerByid(Integer id) {

return this.getSqlSession().selectOne("com.wyy.po."+"CustomerMapper.findcustomerbyid",id);

}
}

Customer.java:
package com.wyy.po;

import java.io.Serializable;

//实体类
public class Customer implements Serializable {

private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
}

}

CustomerMapper.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">


select * from t_customer where id=#{id}

TestDao.java:
package com.wyy.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wyy.dao.CustomerDao;
import com.wyy.po.Customer;

public class TestDao {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
//applicationContext.xml里的实例化Dao的bean的id
//CustomerDao cus=(CustomerDao)app.getBean("customerDao");
//获取bean的另一种方式
CustomerDao cus=app.getBean(CustomerDao.class);
Customer customer=cus.findCustomerByid(2);
System.out.println(customer);
}

applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">


<!-- 数据库驱动 -->

<!-- 连接数据库的url -->

<!-- 连接数据库的用户名 -->

<!-- 连接数据库的密码 -->

<!-- 配置最大连接数 -->

<!-- 最大空闲连接 -->

<!-- 初始化来连接数 -->

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">


<!-- 注入数据源 -->

<!-- 指定核心配置文件位置 -->



db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/account
jdbc.username=root
jdbc.password=121
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

log4j.properties:

Global logging configuration
log4j.rootLogger=ERROR, stdout

MyBatis logging configuration...
log4j.logger.com.wyy=DEBUG

Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

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">






  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • luo_mu_hpu
    豆北儿 2020-08-14 09:53
    已采纳

    mysql6.0.11对应的jdbc驱动版本mysql-connector-java-5.1.6.jar

    点赞 评论
  • storm1979
    storm_huang 2020-08-11 09:08

    估计是你的mysql数据库驱动jar包版本太高了,找一个低版本一点的

    点赞 评论
  • weixin_45882825
    weixin_45882825 2020-08-13 13:47

    5.0.8还高吗?感觉找不到更小的了

    点赞 评论

相关推荐