小吴会变成老吴 2020-08-10 22:21 采纳率: 100%
浏览 166
已采纳

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条回答 默认 最新

  • 豆北儿 2020-08-14 09:53
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!