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

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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?