MyBatis环境配置问题汇总及文件示例
一、配置文件的编写
这是最简单第一段配置,这里有一个坑就是配置文件标签的顺序是有严格要求的,官网有说明比如configuration下面按顺序是properties、settings、typeAliases 、typeHandlers、objectFactory 、plugins 、environments 、mappers 大概是这么个顺序,如果顺序不对的话会提示,根据提示调整就没问题了。
<?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="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/Jewelry"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.jewelry.mapper"/>
</mappers>
</configuration>
二、mysql请求失败,提示Cannot find class: com.mysql.jdbc.Driver
这是由于缺少mysql连接的jar包的原因,去mysql官网下载个mysql-connector-java-5.1.45-bin.jar引入到项目中就可以。我这里因为使用的数据库是最新版本的所以jar包也下载的是最新的,不知道如果版本低了的话有没有问题,建议都使用最新版本的毕竟一些更新也是有用的。
下面贴一段我自己测试成功的代码,包括配置文件和映射文件等,作为以后自己回忆分析的依据(大神勿喷,都是最简单的代码)。
配置文件mybatis-config.xml(这个文件我是放在src下面的config包里,mybatis官网推荐放到代码包里,最好不要放到web-inf下)
<?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="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/Jewelry"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.jewelry.mapper"/>
</mappers>
</configuration>
model类文件 JewelryVO.java
package com.jewelry.entity;
public class JewelryVO {
private String id;
private String name;
private String fullprice;
private String img;
private String description;
private String currentprice;
private String issale;
private String remain;
private String isshow;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFullprice() {
return fullprice;
}
public void setFullprice(String fullprice) {
this.fullprice = fullprice;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCurrentprice() {
return currentprice;
}
public void setCurrentprice(String currentprice) {
this.currentprice = currentprice;
}
public String getIssale() {
return issale;
}
public void setIssale(String issale) {
this.issale = issale;
}
public String getRemain() {
return remain;
}
public void setRemain(String remain) {
this.remain = remain;
}
public String getIsshow() {
return isshow;
}
public void setIsshow(String isshow) {
this.isshow = isshow;
}
}
映射文件 JewelryMapper.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.jewelry.mapper.JewelryMapper">
<select id="getJewelryList" resultType="com.jewelry.entity.JewelryVO">
select * from jewelry
</select>
</mapper>
调用类文件 JewelryGridController.java(这里只贴出了一种通过selectone调用查询,还有另外一种直接调用请求方法的稍后会补充上)
try {
Reader reader = Resources.getResourceAsReader("config/mybatis-config.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = ssf.openSession();
JewelryVO jewelryvo=sqlSession.selectOne("com.jewelry.mapper.JewelryMapper.getJewelryList");
if(jewelryvo!=null){
System.out.println(jewelryvo.getId());
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}